0

Is there a way to retrieve the total amount of native memory usage from a running C# application (either from within C# or an external CLI tool)? Something equivalent to the output of the non heap sections in jcmd's VM.native_memory?

I know about GCMemoryInfo.PinnedObjectsCount but that is only about the object count, not the size.

The intent is to publish it as a metric that can be tracked (i.e. not using complex debugging like Visual Studio, WinDbg or equivalents).

EDIT: I would be happy with the total amount of unmanaged memory, but something more granular like jcmd's output would be even better. Main goal is to track down native memory allocations via calls like Marshal.AllocHGlobal.

1
  • Although this is not targeted at C#, the information given will apply, you just need to translate some of it. stackoverflow.com/questions/1476018/… Commented Aug 15, 2023 at 10:12

2 Answers 2

0

There isn't really a single value for "native memory". It is a bit more complex than that. A good starting point is this series: https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-virtual-memory/ba-p/723750

The Process class exposes the different Windows memory stats. See https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0

Sign up to request clarification or add additional context in comments.

2 Comments

I would be happy with the total amount of unmanaged memory, but something more granular like jcmd's output would be even better. Main goal is to track down native memory allocations via calls like Marshal.AllocHGlobal. I will update my answer to make it clearer.
@andresp I am not aware of a direct way of doing that. Perhaps you could setup a hook using iCorProfiler and monitor the allocations that way.
-1

Take a look into PerformanceCounter class.

Please, notice that It only works on Windows OS. In the previous project we needed to implement it on MacOs, but looks like because of different OS it throws OSException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.