Click here to Skip to main content
15,917,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am testing for memory leaks. I am going to let my program post(adds string to cache)/read(delete string from cache) for a long period of time to see if the memory accumulates or becomes study at a certain amount.

So I am checking the totalmemory from the System.GC.GetTotalMemory() to see how much memory I am using.

But I don't know if I should set it to true or false to allow the method to wait for garbage collection.

Which should I do? Also is there other good ways to test for memory leaks, or system resource leaks?
Posted
Updated 14-Jul-11 13:49pm
v2

What you're doing will not give you accurate results. You have to use a profiler to do that and/or PerfMon (Performance Monitor) and the .NET Framework memory counters to get this data. You can find PerfMon by clicking on Start -> Run -> type Perfmon.msc.
 
Share this answer
 
Comments
Member 8015046 15-Jul-11 11:53am    
When I do performance check, if things climb up linearly. Does that mean it is bad? Like for example both Gen 0 and Gen 1 climb up consistantly.
Dave Kreskowiak 15-Jul-11 18:05pm    
If it increases by 2000 bytes, which is NOTHING, every 1,000 method calls, you might want to test it considerably more and see what happens when you call the methods in question over 1,000,000 times. 2,000 bytes is a very tiny drop in the bucket that can be attributed to .NET CLR house keeping. You need a much larger sample size to determine if there actually a leak.
Member 8015046 15-Jul-11 18:53pm    
Using the performance monitor. Gen 0 Collection and Gen 1 Collection seems to be increasing linearly by 3 per sec. I think it's 3 byte. Over 90,000 methods.
But still, if you want to use GetTotalMemory method. I think you should allow full garbage collection, since you are want to know whether you forgot to free some objects (it maybe freed by you, but not by GC, since it waits for appropriate moment), and not whether garbage collector freed it to this moment.
 
Share this answer
 
Comments
johannesnestler 15-Jul-11 5:47am    
I agree. But as Dave Kreskowiak said, this numbers won't be very useful...
skv_lviv 15-Jul-11 6:11am    
Generally, I agree with what Dave Kreskowiak wrote. And, yes, this isn't accurate number, and it shouldn't be treated like something accurate, even MSDN says "Retrieves the number of bytes currently THOUGHT to be allocated". However who said that this number cannot be used as some indicator? If this number grows significantly when it is not supposed to do, this means that you have memory leak without using other more accurate methods. When this number is stable, you may expect that your application works fine and may try to use other methods to additionally verify this.
Member 8015046 15-Jul-11 11:09am    
I think it does have a memory leak. when I put it on true, every 1000 methods it increases by 2000 bytes >.<

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900