How to determine how much memory is currently in my program?

Related to my previous question:
Preventing memory issues when processing large amounts of text

Is there a way to determine how much memory my program takes? I end up processing a large amount of text file and usually store processed objects in memory. There are times when there will be too much information and I will run out of memory. I have a solution to prevent a memory allocation problem, but I want to use it when necessary to avoid paging, which will ultimately reduce my performance when it is not needed. Is there any way to find out how much memory I occupy so that I can compose my information only when necessary?

NOTE: I am looking for a solution that my program can use to start paging when necessary.

+6
memory-management c #
source share
4 answers

You can try GC.GetTotalMemory :

Gets the number of bytes currently considered allocated. the parameter indicates the method can wait a short time before returning to allow the system to collect garbage and complete objects.

It is important to note this part: "Retrieves the number of bytes that are currently considered allocated." This means that this method may not be 100% accurate - if you know that this is happening, you should be able to get a general idea about the use of your virtual memory at the moment your application is running.

Edit: Let me suggest another solution that is likely to be more productive: use perfmon and CLR performance counters .

+5
source share
  long bytes = System.Diagnostics.Process.GetCurrentProcess (). WorkingSet64; 
+7
source share

You really need to use a code profiler. They will tell you what happens, where memory is used, etc.

FYI: It's rare where you think.

+3
source share

long bytes = System.Diagnostics.Process.GetCurrentProcess (). WorkSet64 for more . See here.

+3
source share

All Articles