C # - GC.GetTotalMemory () Question

I am creating a C # based Windows service that will run 24x7 for several months in a row. I would like to be able to track the usage of the shared memory of my service. It does not have to be accurate down to the byte. The total amount will be sufficient. I will follow these trends in memory consumption. Is GC.GetTotalMemory () a suitable way to monitor this?

I know Performance Monitor and the use of counters. However, this service will run on at least 12 different servers. I do not want to track PM on 12 different servers. The service will save all performance and memory usage information from all instances to the central database to avoid this, and to simplify the analysis.

+6
source share
4 answers

Only if this is a purely managed memory application. If any of them responds to unmanaged code, the memory allocated there will never be registered in the garbage collector (unless someone remembers to call GC.AddMemoryPressure ).

+5
source

GC.GetTotalMemory retrieves the amount of memory that is supposed to be allocated. It only knows about the memory allocated by managed components, unless you name GC.AddMemoryPressure to talk about another allocated memory.

You can better understand the real amount of memory allocated by reading Process.WorkingSet64 , Process.VirtualMemory64 and other such properties of the Process class. Just call Process.GetCurrentProcess and then get what you need.

However, you are probably better off using performance counters.

+3
source

This is not a direct answer to your question, but it was caused by your intention to maintain 24x7 service for several months.

I would very well know the fragmentation of the heap. See this article for an explanation.

+3
source

Use performance counters. You can configure them to enter the file.

Alternatively, you can read performance counters in your code and store data in a central location.

+2
source

Source: https://habr.com/ru/post/897527/


All Articles