Application memory keeps increasing
I have an application whose memory usage keeps increasing. The obvious thought is that I have a memory leak. But it turns out, that if i force GC via GC.Collect(), the memory usage is stable. So I'm not use what to do now? Should I just regularly call GC.Collect()? But this seems a bit off, and I haven't seen another application needing to do this.
15 Replies
Regularly calling
GC.Collect()
is often times a red flag
Do know which code is causing the increase in memory?My whole Application, it has a main loop which executes every 16ms and that allocates a bit of memory. But this memory should obviously be freed again.
Can you share what the loop is doing?
What is it allocating memory for?
Well, it's the enire UI loop, it renderes a frame 60 times a second.
Are you sure the allocated memory for the frame is managed correctly?
what do you mean by "managed correctly", I know that there are no references for this memory that are kept beyond the current frame, because when I run the GC, it gets freed. When I run dotmemory i see what objects are allocated each frame, and it seems fine.
Like freed or reused by the next frame. Hard to tell what's wrong without actual code at this point
https://github.com/FlurinBruehwiler/flamui This is the code, to test i run the Sample.ComponentGallery sample
GitHub
GitHub - FlurinBruehwiler/flamui: A desktop UI framework
A desktop UI framework. Contribute to FlurinBruehwiler/flamui development by creating an account on GitHub.
it's not a tiny codebase, so not sure how usefull this is
maybe i can try creating a minimal repro
Oh yeah true. It's based on SDL, that's good to know
You can probably figure out what's wrong by commenting stuff out
Since this is an immediate mode UI
//todo wtf is happening grrrr it makes 0 sense:when:
hahha
yeah, i can try, but do you have any idea what "concept" could be causing this behaviour, like I don't really understand when the GC decided to collect. MS just lists these points:
is the memory not hitting a "threshold", and therefor the GC doesn't run?
The increasing memory makes it seem that it is an actual memory leak
You can check if the GC is doing stuff by using a profiler for example
I think VS is also displaying when the GC kicks in (while debugging)
But: Because
GC.Collect()
helps -> It might not kick in often enough for that 60fps. Using a profiler will tell you everything you need to knowit's not a memory leak if GC.Collect() fixes the issue. the GC is just not running because it decides that it has no reason to run
it will run later