My attempt to track a memory leak, is it doing what I want?

I have an application consisting of several components, each of which is compiled into one DLL, each of which executes several threads inside itself. I have a shell program that runs these components.

I run this application in a CE 6 window environment, and this is the only program (except the system one) on it. However, over time, I see that the allocated memory usage is slowly increasing when I look at the task manager window. I suspect that my program may have a memory leak. I do the following.

I go to a specific component and create a Timer object that runs every 30 minutes, which calls the following code:

long memByte = GC.GetTotalMemory(false); Console.Write("Heap Memory: " + (memByte/1000).ToString() + "KB"); 

From reading on the internet, I think GetTotalMemory basically gives me the total system management memory. If my program does not have a memory leak problem, I suspect that overtime I get some type of flat line when I calculate the result. Otherwise, I will see a slow increase.

My question is: does GetTotalMemory really give me the shared memory of the heap of the entire application, or gives only the specific heap that is used by the current component, where does this code work?

Thanks,

+4
source share
2 answers

Task Manager shows you different data that GC.GetTotalMEmory. It is possible to make an application that has GC.GetTotalMemory that has flat lines, but slowly deletes the device from memory and crashes.

GC.GetTotalMemory looks only at the GC heap, not the allocated virtual or physical memory. P / Invokes and native allocations outside of GCHeap will be transparent to calling GC.GetTotalMemory, so it is a pretty useless call to try to determine if you are really leaking.

GC Heap can grow and / or shrink from time to time without making any calls at all to the OS memory manager, in fact it is quite common for this. The GC captures large chunks at a time from the OS, and then divides it into smaller blocks, since the application requires GC heap memory.

P / Invoke GlobalMemoryStatus if you want to track the actual distribution of the OS that you see ovr on the device control panel.

+2
source

This method returns the managed memory usage of your process. Not in the system-wide area.

Your process may not even have permission to request memory for other processes.

+1
source

All Articles