添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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.

GC.GetTotalMemory retrieves the amount of memory thought to be allocated. It only knows about memory allocated by the managed components, unless you call GC.AddMemoryPressure to tell it about other memory allocated.

You can get a better idea of 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.

That said, you're probably better off using the performance counters.

Unfortunately those process properties do not match task manager at all. Not sure why. The GC.GetTotalMemory call is much closer. jjxtra Aug 13, 2020 at 19:44

This isn't a direct answer to your question, but was prompted by your intent to keep your service running 24x7 for months.

I would be very aware of heap fragmentation. See this article for an explanation

+1 for Perf Counters, as they also allow you to monitor a million other things like CPU and I/O Usage that could become a bottleneck over time. Michael Stum Sep 17, 2011 at 15:17 @TheCodeKing - Normally I would, and actually have. However, this service is going to be running on an least 12 different servers. I don't really want to track PM on 12 different servers. I'm persisting all performance and memory usage information, from all instances, to a central DB to avoid this. Randy Minder Sep 17, 2011 at 15:17 You can connect to performance counters on remote machines, so in affect you can centralize administration, understand if it's still not appropriate. You can of course also read these in code. TheCodeKing Sep 17, 2011 at 15:23

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .