How to measure memory usage? [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Some of you might recall I had issues running my application (game) on a slower machine (WinXP SP2, .Net 2.0, 1.6gig cpu, 256 megs ram [I know this is really really low, but still most high-end games work fine]), each time it was launched it generated a "Out of Memory" Exception ("System.OutOfMemoryException: Out of memory.")

So to resolve the problem I did some "optimization" and was able to make a big difference - it crashes much later on in the Level Generation phase (where I create all my cells, objects, characters, etc... - this is the chunk that counts)...
Obviously this wasn't good enough but I spent an entire night looking through my code and was unable to find really anything I thought would make a huge difference (mind you - I am far from an expert on the subject)...

So the next step is to find out WHERE in my GenerateLevel() function all the memory is being eaten up - so I was wondering if there was a nice & easy way to get the Memory Usage of my program at different point in the GenerateLevel() function (that I would trace out to a text file) - this should enable me to "see" where in the code the most amount of memory is eaten and hopefully allow my to try and minimize it as much as possible...

Unless someone has some other suggestions? Profiling tools? Better methods of solving my problem, etc...?

Any ideas, hints, and help would be greatly appreciated, thanks
 
PlausiblyDamp: Thanks, I was actually playing with it now (recalled that you mentioned it in a previous post) and it is very interesting ... so now I have some data [shown below, not sure if it helps at all]:

Summary for Game.exe
Allocated bytes: 322,427
Relocated bytes: 0
Final Heap bytes: 322,427
Objects finalized: 0
Critical objects finalized: 0
Gen 0 collections: 0
Gen 1 collections: 0
Gen 2 collections: 0
Induced collections: 0
Gen 0 Heap bytes: Unknown
Gen 1 Heap bytes: Unknown
Gen 2 Heap bytes: Unknown
Large Object Heap bytes: Unknown
Handles created: 119
Handles destroyed: 16
Handles surviving: 103
Heap Dumps: 0
Comments: 0
(this is solely the GenerateLevel() function, I kill the game after as from that point on not "that much" changes)...

The problem is, I have never using any profiling tools (I am kind of new at this memory-management aspect, always expected C# to clean up after me eheheh) so I have no way to gage if these results are normal or not - nor am I sure on what exactly to expect?

- I assume my goal is to REDUCE "Allocated bytes" as much as possible?
- What about the "Handles surviving"? shouldn't that be 0 after an Application.Exit()?
- What am I expecting to see in the case where I am "not cleaning up properly"?
- Are these values unreasonable? (I assume you can't answer without seeing my code)

Now - for my last question - I let the game run for 15 seconds and this is the profiling results I got:

Summary for Game.exe
Allocated bytes: 1,206,813
Relocated bytes: 161,320
Final Heap bytes: 323,410
Objects finalized: 398
Critical objects finalized: 24
Gen 0 collections: 2
Gen 1 collections: 0
Gen 2 collections: 0
Induced collections: 0
Gen 0 Heap bytes: 524,504
Gen 1 Heap bytes: 145,948
Gen 2 Heap bytes: 12
Large Object Heap bytes: 25,608
Handles created: 568
Handles destroyed: 464
Handles surviving: 104
Heap Dumps: 0
Comments: 0

- Is it NORMAL to have increased that much? does it ever stop increasing? shouldn't it stay at a stable value?

Not sure if I have a big problem or not :)
Thanks for the help,

BTW - I am using CLR2.0 as I am using C# 2005 (2.0) - incase that makes a difference...
 
Also - not sure if this helps - but I added "System.GC.GetTotalMemory(true)" to my tracing to see the memory being eaten - this is what it gives me:

Generating Level - CellMap // Memory at: 471624
Generating Level - Borders // Memory at: 478332
Generating Level - Walls & Boulders // Memory at: 481352
Generating Level - Bricks & Trees // Memory at: 482280
Generating Level - Player // Memory at: 482760
Generating Level - Bots // Memory at: 484916
Generating Level - ArrayLists // Memory at: 487160
Generating Level - Start Background Music // Memory at: 487396

These are the result for the GenerateLevel() function, this is where the game always crashes for my friend (and there really isn't much done before that)...
Does this look like a normal increase? Is there something odd or execessive?

Also - should this value keep growing over time (isn't that considered a memory leak) or should it remain stable around a certain point?


Thanks,
 
One thing you can do with the CLRProfiler is add a reference to it from your app (remove it in release builds) and actually insert commetns into the log file - this can be a big help in seeing what is happening over time.

Without seeing the code itself it is difficult to know what the problem(s) is (are) if any. If possible could you attach a zipped version of the profiler log and I'll try to find time to have a look at it.
 
PlausiblyDamp -> I have attached (hopefully) a ZIP file that contains the following files:

Game_Actions.log >> The actual huge log file of me maxing the game (running, killing, exploding) for ~ 15 seconds
Game_Actions_Details.log >> The corresponding summary
Game_Actions_Details_longer.log >> A summary after 2:30minutes maxing out the game (look how much it increased)

Game_NoActions.log >> The actual huge log file of me not moving at all (bots walking around thats it) for ~ 15 seconds
Game_NoActions_Details.log >> The corresponding summary
Game_NoActions_Details_longer.log >> A summary after 2:30minutes of me not moving at all (still a pretty big increase I find)

From this are you able to tell anything? This is so much new data for me that I am having a hard time making heads or tails out of it....

I think there might be a leak somewhere :) That seems like a large increase (and it looks like it keeps going up .... I let it idle for 26 minutes and got a "Allocated bytes" size of 58,359,433 !!! (that is what I should be looking at right?)

Thank you for taking the time to help me out and look into this, any ideas will be much appreciated - or if you need anything else/more...
 

Attachments

Last edited:
Is there anything else I can provide to help you in your investigation? More CLR Profiler Logs? Different ones? etc...

I am currently formulating some code snipplets to post so that you can see how my code is working (should help a lot - just need to write it out) - I'll follow up soon enough...

In the meantime - anything I can do to make it easier for you, please do not hesitate...
 
For some additional information I used "System.GC.GetTotalMemory(true).ToString()" to get the Garbage Collector Total Memory... Thought this might indicate if I had a leak or something similar, minimally provide you with more details/information ...

So I let the game run for a few minutes (where I went and killed everything on the map) and I outputted the GC Total Memory at every TICK of my board timer - all this information was outputted to the .LOG file attached to this post, I thought this might help a litte (hopefully)...

I see the memory going UP and then DOWN a little also - could be a good sign ...?
If you need anything else please tell me and I'll attach more logs, etc...

Thanks for the help...
 

Attachments

Back
Top