问题内存使用的量度 [英] Problem with memory usage measurment

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

问题描述

我有一些C#的功能,我想衡量字节的内存使用情况。我用GC.GetTotalMemory如下:

I have some C# functions, and I want to measure their memory usage in bytes. I used GC.GetTotalMemory as follows:

long val1 = GC.GetTotalMemory(false);

// my code

long val2 = GC.GetTotalMemory(false);

long result = val2  - val1 ;



我试图传递真假两作为论据GC.GetTotalMemory,但我不知道为什么我得到阴性结果的价值,当我从val2中减去VAL1 ....另一个问题是,GC.GetTotalMemory每个excution给出不同的价值观,我认为这是不准确的。

I tried to pass both true and false as argument for GC.GetTotalMemory, but I do not know why I get negative result value, when I subtract val1 from val2 .... Another issue is that GC.GetTotalMemory give different values in each excution, I think it is not accurate

谁能知道为什么我得到阴性结果的价值?是不是因为argumenet的(true或false),但我想他们两个,并得到阴性结果值太大

Can anyone know why I get a negative result value ? is it because of the argumenet (true or false), although I tried them both and got negative result value too

谁能知道我为什么在每个excution得到不同的值C#的功能?

Can anyone know why I get different values in each excution of the C# function ?

请如果任何人有衡量在Windows C#的功能,请告诉我,非常感谢

Please if anyone has a better efficient way to measure memory usage in bytes for C# function in Windows, please tell me and thanks alot

推荐答案

您可以收到一个否定的结果,因为一个垃圾收集可以采取两个点之间发生,从而导致消耗以减少总存储器。事实上,当你通过真正到了 GC.GetTotalMemory 函数的您实际上是允许它等待垃圾回收发生,从而提高返回负值的情形产生。

You may be receiving a negative result, because a garbage collection could have taken place between the two points, causing the total memory consumed to decrease. In fact, when you pass true to the GC.GetTotalMemory function, you're actually allowing it to wait for a garbage collection to take place, thus increasing the likelyhood of a negative value being returned.

有一个更好的(更准确)的方式来做到这一点,假设你可以点暂停过程中,是看不同的(.NET和进程/内存)的性能监视器计数器,看之前和之后。

A better (and more accurate) way to do this, assuming you can suspend the process at points, is to look at various (.NET and Process/Memory) counters in Performance Monitor, to see the before and after.

这篇关于问题内存使用的量度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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