且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Environment.WorkingSet错误地报告内存使用情况

更新时间:2023-11-09 23:04:28

我认为这个问题是关系到什么是的这篇文章


  

2005年5月4日结果
  趣味的工作集和INT32 结果
  我终于找到一个诚实善良的bug在.NET框架。


  
  

在...
  工作集返回的内存量正在使用的过程
  一个整数(32位有符号整数)。 OK,这样的最大值
  整数是2,147,483,647 - 这是非常接近总
  内存量进程可在其工作集。


  
  

... ...有实际上是Windows中的一个开关,将允许一个进程使用
  3演出的记忆,而不是2演出。这个开关通常接通时
  处理分析服务 - 这个东西可以是一个记忆体猪。所以
  现在发生的事情是,当我轮询工作集,我得到一个负
  号,一个非常大的负数。通常,在境界
  -2,147,482,342。


  
  

...问题是溢出位。


  
  

工作集返回到一个二进制值.NET框架。该
  的整数的第一比特是符号位。 0为正,1为负。
  所以,当值从接通(二进制)
  1111111111111111111111111111111到(二进制)
  10000000000000000000000000000000值由2147483647进入
  -2147483647。


  
  

好了,我还是要解决这个问题。以下是我想出了(在C#):​​

 长lWorkingSet = 0;
如果(process.WorkingSet> = 0)
 lWorkingSet = processWorkingSet;
其他
 lWorkingSet =((长)* int.MaxValue 2)+ process.WorkingSet;


  
  

希望这解决了这个问题现在。


  
  

真正的问题会在下山的路。微软知道了解
  这个问题。我还是找出它们将如何解决这一问题的
  Win64的...其中这一招将不再工作。


块引用>

  

http://msdn2.microsoft.com/library /0aayt1d0(en-us,vs.80).aspx :结果
  还有的会是一个Process.WorkingSet64可变的,他们是
  德precating工作集。


  
  

在切线,不过,我认为这是不可能的,有管理的
  过程走近了3GB的限制,因为运行时拆分
  内存为多个堆。这是不是真的?


块引用>

Environment.WorkingSet incorrectly reports the memory usage for a web site that runs on Windows 2003 Server.(OS Vers: Microsoft Windows NT 5.2.3790 Service Pack 2, .NET Vers: 2.0.50727.3607)

It reports memory as Working Set(Physical Mem.): 1952 MB (2047468061).

Same web site runs locally on Windows Vista with a Working Set(Physical Mem.): 49 MB (51924992).

I have limited access to the server and support is so limited :(. so i have computed the total memory by traversing with VirtualQuery. Total of pages with state: MEM_FREE is 1300 MB. (I guess server have 4 GBs of RAM and PAE is not enabled, max user mode virtual address is 0x7fff0000.)

So, i know working set is not only about virtual memory. But, is it normal to have such a high working set while its very low on another machine?

I think the problem is related to what is described in this article:

MAY 04, 2005
Fun with the WorkingSet and int32
I finally found an honest to goodness bug in the .NET framework.

... the WorkingSet returns the amount of memory being used by the process as an integer (32 bit signed integer). OK, so the maximum value of an integer is 2,147,483,647 -- which is remarkably close to the total amount of memory that a process can have in its working set.

... There is actually a switch in Windows that will allow a process to use 3 gig of memory instead of 2 gig. This switch is often turned on when dealing with Analysis Services -- this thing can be a memory hog. So now what happens is that when I poll the WorkingSet I get a negative number, a really big negative number. Usually, in the realm of -2,147,482,342.

... The problem was the overflow bit.

Working set is returned to the .NET framework as a binary value. The first bit of an integer is the sign bit. 0 is positive, 1 is negative. So, when the value turned from (binary) 1111111111111111111111111111111 to (binary) 10000000000000000000000000000000 the value goes from 2147483647 to -2147483647.

OK, so I still have to fix this. Here is what I came up with (in C#):

long lWorkingSet = 0;
if (process.WorkingSet >= 0)
 lWorkingSet = processWorkingSet;
else
 lWorkingSet = ((long)int.MaxValue*2)+process.WorkingSet;

Hopefully that fixes the problem for now.

The real question will come in down the road. Microsoft knows about this problem. I still have find out how they are going to fix this for Win64...where this trick will no longer work.


http://msdn2.microsoft.com/library/0aayt1d0(en-us,vs.80).aspx:
There's gonna be a Process.WorkingSet64 variable, and they're deprecating WorkingSet.

On a tangent, though, I thought it was impossible for a managed process to come near the 3gb limit, because the runtime splits the memory into multiple heaps. Is this not true?