如何衡量代码的内存使用情况? [英] How to measure memory usage of a code?

查看:84
本文介绍了如何衡量代码的内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测量代码的内存使用情况,但是我不知道如何进行. 我不想使用"DotMemory"或任何我需要通过编程来完成的探查器".

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.

有什么方法可以知道分配了多少个对象? 像5个int(4个字节)+ 4个对象(16个字节)...

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

我用过

process.WorkingSet64

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 ?

推荐答案

如果您需要以编程方式测量内存消耗,则可以使用 dot内存单元 当前版本可以通过ReSharper单元测试运行器与单元测试一起使用,但是很快将推出具有独立运行器的下一个版本.

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;
});

博客文章.

更新:独立运行器使用EAP https://www.nuget.org/packages/JetBrains.DotMemoryUnit/2.0 .20150727.161305-EAP5

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

这篇关于如何衡量代码的内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆