#new to Jobs, need advice about correct memory handling

1 messages · Page 1 of 1 (latest)

final talon
#

i made a jobs system that runs in total 2 IJobParallelFor and 1 IJob every frame. I also have 2 computebuffers that are set every frame. The system is pretty performant on my devices, even on my device without a dedicated GPU (the frame drop on that is mostly from RenderMeshInstanced i think). On the script that manages it, I call Dispose on all of the unmanaged objects (nativearrays and nativelists) and computebuffers I have in OnDisable (previously had it in OnDestroy, saw no difference), also making sure to wait until all three jobs are definitely completed. But still, I memory profiled and i got ~3GB more memory usage after I had run my scene then stopped playing. The editor also crashes. I'm probably missing something, but ive looked at some of the unity examples and they don't do anything else other than disposing everything.

viscid saddle
final talon
#

it was malloc or somethign let me double check

#

Ok I ran a bit of a better test

#

here is the scene after running it with the job system enabled and doing its thing

#

same scene but i ran it for sliughtly less time and the job manager gameobject is disabled

#

dunno how significant this is im not superknowledgeable about memory managament and the memory profiler

viscid saddle
#

Malloc is basically an internal allocator used by Unity. It pulls memory from the system dynamically when it needs more memory. However, I don't think it can return any memory back. So perhaps you had peak usage during the run and it just kept the memory reserved afterwards.

final talon
#

im assuming the larger allocatred memory for the former is normal, but it has 2gb more resident memory than the latter test

viscid saddle
#

Wait a moment. I confused with mimalloc

final talon
#

and there is 100% a leak cuz unity does crash after a while

viscid saddle
#

Malloc is the C function for allocating.

final talon
#

oh yeah

viscid saddle
final talon
#

memory addresses no?

viscid saddle
#

Hmm... Yeah.

#

I guess we'll have to look at your code.

final talon
#

oh and every domain reload unity gives the "leak detected" message in console and i turned on stacktrace and if i recall correctly it just pointed to my job classes

final talon
#

let me test that again actually

final talon
viscid saddle
#

To be honest, people over at #1062393052863414313 would probably be able to provide better help. I barely used jobs myself. I can look from a general programmer perspective and if I have no clue, maybe ask there.

final talon
#

the fire propagation job, inside the switch it does some AddNoResize operations and sets sections in the Out nativearrays but thats really it

#

smoke behaviour parallel job (weakest part of my system i think, there are probably better ways to do the smoke lol) also lifetimes and matrices are regenerated every frame

#

oh wait do you think the natvielist being recreated every frame is doing it

#

im pretty sure it gets cleared with a new() call

#

cuz i got an error calling clear() once

viscid saddle
#

I don't see you creating a native list in the code.
Also in the future share code properly:

#

!code

agile folioBOT
viscid saddle
#

I can't really see anything that could be allocating memory in the shared snippets.

#

It's more likely where you start the job and pass data into it that is the problem.

final talon
#

im thinking thats the issue let me test

#

ObjectDisposedException: The UNKNOWN_OBJECT_TYPE has been deallocated, it is not allowed to access it i get this exception when I clear it, is it an issue with the Allocator.Persistent or smt

viscid saddle
#

Is there a callstack to this error? It sounds like you're trying to access it after clearing.

final talon
#

it does, but no the error is called on the line where i clear it

#

anyway i never read these nativelists i only AddNoResize to them with parallelwriter

#

i guess I could Dispose() then recreate it but that feels wrong

viscid saddle
#

Persistent should just live for the lifetime of the program.

#

You can add and remove stuff from it, but don't dispose of it(until the object is destroyed).

final talon
#

yeah if i did that I would have to change the allocator to temp or tempjob or something, not sure what the correct one for that situation would be

#

but i dont really get why it complains when i clear it, that should keep capacity unchanged which is what i want

viscid saddle
#

Well, I don't see that code, so I can't really reason about it very well.

#

As well as errors.

final talon
#

error points to fireMatrices.Clear();

var job = new FirePropagationJob
{
    fieldWidth = width,
    fieldHeight = height,
    startX = fieldBounds.min.x,
    startZ = fieldBounds.min.z,
    gridSize = gridSize,
    fireMatrices = fireMatrices.AsParallelWriter(),
    deltaTime = Time.deltaTime,
    chance = BurnChance,
    patchesIn = currInstances,
    patchesOut = newInstances,
    burnAmountsOut = burnAmounts,
    burnLength = BurnLength,
    randoms = randoms,
    burnThreshold = BurnThreshold,
    smokecd = smokeCooldown,
    frame = Time.frameCount,
    smokeList = smokeInstances.AsParallelWriter(),
    smokeChance = smokeChance
};

fireHandle = job.Schedule(currInstances.Length, 64);
fireHandle.Complete();```
theres not really much
#

all in update by the way

#

wait ... silly me i didnt create it first

#

yep memory issue fixed