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.
#new to Jobs, need advice about correct memory handling
1 messages · Page 1 of 1 (latest)
What exactly do these 3gb of memory come from in the profiler..? You'll need to provide some more details.
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
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.
im assuming the larger allocatred memory for the former is normal, but it has 2gb more resident memory than the latter test
Wait a moment. I confused with mimalloc
and there is 100% a leak cuz unity does crash after a while
Malloc is the C function for allocating.
oh yeah
What if you expand the persistent memory here?
its quite a lot of these
memory addresses no?
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
Ah, that's something
let me test that again actually
alr
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.
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
I don't see you creating a native list in the code.
Also in the future share code properly:
!code
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
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.
alright thanks
yeah before i run each job i recreate the nativelists
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
Is there a callstack to this error? It sounds like you're trying to access it after clearing.
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
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).
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
Well, I don't see that code, so I can't really reason about it very well.
As well as errors.
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