Sign in to follow this  

[.net] [c#] Memory not being released

This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a C# app that loads terrain as needed and [supposedly] deletes it when it's no longer needed. However, over time the memory usage (in the task manager) continues to increase, but when I run the CLR profiler, it says my app is only using 2.8 MB. Maybe I just don't understand what the profiler is saying, but still, I don't know why it isn't freeing the RAM - I've got a TerrainTile class which I create a bunch of instances of and store in a List. When the terrain goes off the screen, I kill the data in the tiles and remove them from the list, but the memory, according to the task manager, doesn't get released. Photo Sharing and Video Hosting at Photobucket Thanks!

Share this post


Link to post
Share on other sites
There are two possibilities.
1) Although you've let go of the memory, the garbage collector has not run. As a result, the memory has not been freed at the OS layer.
2) The memory has been cached or something at the OS layer. So even though your program is only resident in a couple megabytes of memory, the reported memory usage is bogus.

I suspect it's #1, but #2 is not to be discounted either. Take a look at CLR Profiler's listings of when GCs were triggered and for what generations. Your terrain tiles are almost certainly gen2, and if a gen2 collection hasn't been triggered, the OS won't be notified about that memory.

This sort of situation may be a legitimate time to call GC.Collect explicitly to hint a gen2 collection. Rico Mariani has some tips about this.

Share this post


Link to post
Share on other sites
I stuck in a call to GC.Collect() on gen2 (the tiles were in gen2) after I kill the invisible tiles, and I print the output from GC.GetTotalMemory() before and after the GC.Collect() call. It shows that memory is being freed; GC.GetTotalMemory() says memory usage goes down usually by around 1 MB after the Collect call (like from 5 MB to 4).. in the task manager, though, it still shows crazy amounts.

I'm quite confused about what this thing is doing... when the program first loads, the task manager says it's using ~60 MB, which is impossible - the terrain that it loads is not more than a few MB, and the profiler confirms this.

thanks again!

Share this post


Link to post
Share on other sites
Quote:
Original post by Thoughtcriminal90
No, everything is managed right now.

Are you sure? Is that a reference to Tao on your screen shot? My guess would be that you have some big memory allocation going on somewhere under OpenGL and it is not showing up in the CLR profiler because it is all unmanaged. Now, I am no expert in either Tao or OpenGL, but graphics libraries do have a habit of allocating lots of memory if you ask them to. I would not find this surprising at all if I was rendering something like an enormous number of terrain polygons. What I would question is: why it is in system memory and not graphics memory?

It is also not uncommon for wrappers like Tao to force the user to manage memory in the context of the API. Make sure there are not any Tao cleanup calls you might have missed.

Hope this helps.

Share this post


Link to post
Share on other sites
Also note that the working set will grow as your application uses memory, but does not always shrink. The usage displayed in taskmgr is the working set, which may not reflect the actual memory usage of your application.

Share this post


Link to post
Share on other sites

This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this