- Home /
Are memory leaks a problem if they only occur when closing?
I'm currently working on a project that requires quite heavy calculations. I ended up using Unity's job system and Burst compiler because of the great performance compared to other alternatives (I do not use ECS, just the job system and burst compiler). I use Coroutines to be able to await the job completion over multible frames.
yield return new WaitUntil(() => jobHandle.IsCompleted);
I made sure that no memory leaks could occur while the game is running. However, if the game is closed while a job is running, the Coroutine is not able to finish and the NativeArrays are not going to be disposed. Is this going to cause problems or is this irrelevant as the application is closing anyways?
Answer by Bunny83 · Jan 04, 2021 at 09:09 AM
This is irrelevant as all memory that get allocated by a process is freed by the operating system when the process is killed / removed. Though implementing an abort mechanism usually doesn't hurt. Usually if you have really long processes, having the ability to abort the job at user request is a good idea. Imagine Unity's own lightmap baking or occlusion culling data generation. This can take a very long time. Having the ability to abort such a task gracefully (for example when you started it too early because you forgot an important thing) is a good idea.
What @Bunny83 said +1. It's always a good idea to write your IJob
- scheduling classes in such a way so they clean up after themselves. Not because application is closing but because one day in the future your scene will reload eventually, for example, when game starts anew or game save file is loaded.
JobHandle _jobHandle;
NativeArray<int> _jobData;
void OnDestroy () => AbortJobExecution();
public void AbortJobExecution ()
{
_jobHandle.Complete();
if( _jobData.IsCreated ) _jobData.Dispose();
}