C# - GC.GetTotalMemory()问题问题、GC、GetTotalMemory

2023-09-05 23:02:46 作者:多此一爱℡

我创建一个基于C#的Windows服务,将全天候运行了数月之久。我希望能够跟踪我的服务的通用内存使用情况。它并不需要是精确的下到字节。分配就足够了一般的量。我将这种监测在内存消耗的趋势。是GC.GetTotalMemory()以适当的方式来监测呢?

I'm creating a C# based Windows service that will run 24x7 for months on end. I'd like to be able to track general memory usage of my service. It doesn't need to be exact down to the byte. A general amount allocated will suffice. I'll be monitoring this for trends in memory consumption. Is GC.GetTotalMemory() an appropriate way to monitor this?

我知道性能监视器和使用计数器的。然而,这种服务将运行在至少12个不同的服务器。我不想在12个不同的服务器跟踪PM。该服务将坚持所有的性能和存储器使用信息,从所有实例,到中央数据库,以避免这种情况,并且更容易分析

I'm aware of Performance Monitor and the use of counters. However, this service is going to be running on at least 12 different servers. I don't want to track PM on 12 different servers. The service will persist all performance and memory usage information, from all instances, to a central DB to avoid this, and for easier analysis.

推荐答案

只有当它是一个纯粹的管理内存的应用程序。如果它的任何呼吁关闭非托管code,内存分配也将永远不会被垃圾收集器(除非有人记得叫 GC.AddMemory pressure )。

Only if it's a purely managed-memory application. If any of it is calling off to unmanaged code, the memory allocated there will never be registered with the garbage collector (unless someone remembers to call GC.AddMemoryPressure).