How to measure memory usage of a code?

2020-03-06 15:06发布

I'm trying to measure memory usage of a code, but i don't know how to proceed. I don't want to use "DotMemory" or any "profiler" i need to do it by programming.

There is any way to know how much object was allocated ? Like 5 int(4 bytes) + 4 Object (16 bytes) ...

I used

process.WorkingSet64

but every time I run the same code I get different values.

So what is the best way to measure memory usage of a code ?

标签: c# .net memory
4条回答
2楼-- · 2020-03-06 15:16

.NET is a managed memory environment. This means that allocation and deallocation is handled transparently for you, but it also means that the memory usage patterns aren't entirely deterministic.

99.9% of the time, this isn't an issue at all. The rest of the time, you should focus your work on the area that matters - usually, it's pretty easy to handle all the critical load in one place.

Your question suggests you come from a C/Pascal background - the tradeoff of managed memory is that you shoudln't really care about memory - sure, you want to pay attention not to outright waste memory, but taking twice as much memory as strictly necessary usually isn't a thing to lose sleep about. "Memory before" and "memory after" is a question that really doesn't make much sense in a multi-threaded environment - your method isn't the only one that's running in the meantime.

查看更多
太酷不给撩
3楼-- · 2020-03-06 15:24

Simple and silly:

long total = GC.GetTotalMemory(true);
查看更多
聊天终结者
4楼-- · 2020-03-06 15:28

If you need to measure a memory consumption programmatically you can use dotMemory Unit Current version can be used with unit tests via ReSharper unit test runner, but soon the next version with standalone runner will be available.

var mcp1 = dotMemory.Check();
methodX();
dotMemory.Check(memory =>
{
  var newObjects = memory.GetDifference(mcp1).GetNewObjects();
  var createdObjectsCount = newObjects.ObjectsCount;
  var allocatedMemory = newObjects.SizeInBytes;
});

More details in the blog post.

UPDATE: standalone runner goes EAP https://www.nuget.org/packages/JetBrains.DotMemoryUnit/2.0.20150727.161305-EAP5

查看更多
够拽才男人
5楼-- · 2020-03-06 15:34

As described by the other postsyou cant just look at the number you see in the process explorer.

If you really want to measure MemoryUsage it is recommandable to use appropriate tools, like RedGate Ants Profler or dotMemory. There are a lot more, but with these two i have made good experiences. They also show you how many instances of each type an in ram and what do hold them alive

List of all instances

The main question is what is the reason you looking on this ? Do you have a RAM Issue ? Perhaps a leak ? When searching an issue the common approach is to make snapshots with tools like mentioned above and compare them. There you can see wether you have a growing number of instances.

To get approximations of objects and their size (for example to see wether you may get problems) you can try to calculate an estimation see this find size of object instanc in bytes in c sharp for example

查看更多
登录 后发表回答