这是.NET垃圾收集中的错误吗? [英] Is this a bug in .NET garbage collection?

查看:49
本文介绍了这是.NET垃圾收集中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写C#windows应用程序时,我发现了一种非常奇怪的行为。如果我通过一个数组分配一大块内存,那么.NET将永远不会释放内存。


问题可以说如下:


1.只需创建最简单的Windows窗体项目并添加一个按钮和

处理程序,如下所示:


private void button1_Click(object sender,System.EventArgs e)

{

byte [] aaa = new byte [300000000];

for(int i = 0; i< aaa.Length; i ++)

aaa [i] = 10;

}
<执行上述代码后,我观察了任务管理器中的内存。

提交费用总计跃升至300M,MEM Usage显示相同的

的事情。问题是无论你做什么,内存使用都无法回退。我已经尝试打开许多其他应用程序而且没有任何东西可以让内存释放

.


3.变量aaa已经超出了范围,因此.NET仍然保留内存没有任何意义

。现在整个系统的性能降低了b $ b降级。


4.我唯一可以将内存发布到Windows的是打电话

GC.Collect()。这是释放内存的唯一方法吗?我想这么做吗?


有谁可以确认这是否是.NET中的错误?即使没有人使用它,看起来也不会让内存永远保持不变。


谢谢,


Yang。

解决方案

该框架的版本是什么?如果内存服务,那么1.0

运行时中存在一个错误,在某些情况下不会释放大型对象。


Yang <雅** @ discussions.microsoft.com>在消息中写道

新闻:4E ********************************** @ microsof t.com ...

当我编写C#windows应用程序时,我发现了一个非常奇怪的行为。如果我通过数组分配大量内存,.NET将永远不会释放内存。

问题可以如下所示:

1.只需创建最简单的Windows窗体项目并添加一个按钮和
这样的处理程序:

private void button1_Click(object sender,System.EventArgs e)
{
byte [] aaa = new byte [300000000];
for(int i = 0; i< aaa.Length; i ++)
aaa [i] = 10;
2.执行上面的代码后,我在任务管理器中观察了内存。
提交费用总计跃升至300M,MEM Usage显示相同的内容。问题是无论你做什么,内存使用都无法回落。我已经尝试打开许多其他应用程序而NOTHING可以让内存得到释放。

3.变量aaa已超出范围,所以它不会使任何感觉.NET仍然保留着内存。现在整个
系统的性能降级了。

4.我唯一可以将内存发布到Windows的就是调用GC.Collect( )。这是释放内存的唯一方法吗?我是否想要这样做?

任何人都可以确认这是否是.NET中的错误?即使没有人使用它,外观仍然没有意义。

谢谢,

杨。



垃圾收集器适用于.NET 1.0(SP 1+)和.NET 1.1,但是你不能总是预测它何时会回收你未使用的数组。我很漂亮

当然,如果你点击按钮两次,内存仍然是300M

而不是600M。

如果你愿意的话我真的很了解垃圾收集器是如何工作的,我建议您下载共享源
http://www.microsoft.com/downloads/d...displaylang=en

并逐步完成垃圾收集源代码(例如gcXXX.cpp)。

Aleksey Nudelman,

csharpcomputing.com

杨写道:

当我编写C#windows应用程序时,我发现了一个非常奇怪的行为。如果我通过数组分配大量内存,.NET将永远不会释放内存。

问题可以如下所示:

1.只需创建最简单的Windows窗体项目并添加一个按钮和
这样的处理程序:

private void button1_Click(object sender,System.EventArgs e)
{
byte [] aaa = new byte [300000000];
for(int i = 0; i< aaa.Length; i ++)
aaa [i] = 10;
2.执行上面的代码后,我在任务管理器中观察了内存。
提交费用总计跃升至300M,MEM Usage显示相同的内容。问题是无论你做什么,内存使用都无法回退。我已经尝试打开许多其他应用程序而NOTHING可以让内存得到释放。

3.变量aaa已超出范围,所以它不会使任何感觉.NET仍然保留着内存。现在整个系统的性能降级了。

4.我唯一可以将内存发布到Windows的就是调用GC.Collect()。这是释放内存的唯一方法吗?我想这样做吗?

任何人都可以确认这是否是.NET中的错误?即使没有人使用它,外观仍然没有意义。

谢谢,

杨。



1.1框架似乎工作正常。 GC执行收集并在阵列超出范围后很快(一秒钟)释放内存中的所有内容。如果你是使用1.0的
那么它很可能是丹尼尔提到的。


Imran。

杨 ; <雅** @ discussions.microsoft.com>在消息中写道

新闻:4E ********************************** @ microsof t.com ...

当我编写C#windows应用程序时,我发现了一个非常奇怪的行为。如果我通过数组分配大量内存,.NET将永远不会释放内存。

问题可以如下所示:

1.只需创建最简单的Windows窗体项目并添加一个按钮和
这样的处理程序:

private void button1_Click(object sender,System.EventArgs e)
{
byte [] aaa = new byte [300000000];
for(int i = 0; i< aaa.Length; i ++)
aaa [i] = 10;
2.执行上面的代码后,我在任务管理器中观察了内存。
提交费用总计跃升至300M,MEM Usage显示相同的内容。问题是无论你做什么,内存使用都无法回落。我已经尝试打开许多其他应用程序而NOTHING可以让内存得到释放。

3.变量aaa已超出范围,所以它不会使任何感觉.NET仍然保留着内存。现在整个
系统的性能降级了。

4.我唯一可以将内存发布到Windows的就是调用GC.Collect( )。这是释放内存的唯一方法吗?我是否想要这样做?

任何人都可以确认这是否是.NET中的错误?即使没有人使用它,外观仍然没有意义。

谢谢,

杨。



I found a very strange behavior when I write a C# windows application. If I
allocate a huge chunk of memory by an array, the memory will never be
released by .NET.

The problem can be demostrated as follows:

1. Just create the simplest windows form project and add a button and
handler like this:

private void button1_Click(object sender, System.EventArgs e)
{
byte[] aaa = new byte[300000000];
for(int i=0; i<aaa.Length; i++)
aaa[i] = 10;
}

2. After executing the above code, I observed the memory in Task Manager.
The Commit Charge Total jumped to 300M and the MEM Usage shows the same
thing. The problem is whatever you do the memory usage can never drop back. I
have tried to open many other applications and NOTHING can make the memory
get released.

3. The variable aaa has gone out of scope, so it does not make any sense
that the .NET still holds the memory. Now the performance of the whole system
is downgraded.

4. The only thing I can make the memory release to Windows is to call
GC.Collect(). Is this the only way to release that memory? Do I suppose to do
this?

Can anybody confirm if this is a bug in .NET? Appearantly it does not make
sense to hold the memory FOREVER even if nobody uses it.

Thanks,

Yang.

解决方案

What version of the framework? If memory serves there is a bug in the 1.0
runtime won''t free large objects in certain circumstances.

"Yang" <Ya**@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...

I found a very strange behavior when I write a C# windows application. If I
allocate a huge chunk of memory by an array, the memory will never be
released by .NET.

The problem can be demostrated as follows:

1. Just create the simplest windows form project and add a button and
handler like this:

private void button1_Click(object sender, System.EventArgs e)
{
byte[] aaa = new byte[300000000];
for(int i=0; i<aaa.Length; i++)
aaa[i] = 10;
}

2. After executing the above code, I observed the memory in Task Manager.
The Commit Charge Total jumped to 300M and the MEM Usage shows the same
thing. The problem is whatever you do the memory usage can never drop
back. I
have tried to open many other applications and NOTHING can make the memory
get released.

3. The variable aaa has gone out of scope, so it does not make any sense
that the .NET still holds the memory. Now the performance of the whole
system
is downgraded.

4. The only thing I can make the memory release to Windows is to call
GC.Collect(). Is this the only way to release that memory? Do I suppose to
do
this?

Can anybody confirm if this is a bug in .NET? Appearantly it does not make
sense to hold the memory FOREVER even if nobody uses it.

Thanks,

Yang.



Garbage Collector Works well for .NET 1.0 (SP 1+) and .NET 1.1, but you
cannot always predict when it will reclaim your unused array. I am pretty
sure though that if you click the button twice, the memory will still be 300M
rather than 600M.
If you would like to really understand how the garbage collector works, I
recommend that you download shared source
http://www.microsoft.com/downloads/d...displaylang=en
and step through the garbage collection source code (e.g. gcXXX.cpp).
Aleksey Nudelman,
csharpcomputing.com
"Yang" wrote:

I found a very strange behavior when I write a C# windows application. If I
allocate a huge chunk of memory by an array, the memory will never be
released by .NET.

The problem can be demostrated as follows:

1. Just create the simplest windows form project and add a button and
handler like this:

private void button1_Click(object sender, System.EventArgs e)
{
byte[] aaa = new byte[300000000];
for(int i=0; i<aaa.Length; i++)
aaa[i] = 10;
}

2. After executing the above code, I observed the memory in Task Manager.
The Commit Charge Total jumped to 300M and the MEM Usage shows the same
thing. The problem is whatever you do the memory usage can never drop back. I
have tried to open many other applications and NOTHING can make the memory
get released.

3. The variable aaa has gone out of scope, so it does not make any sense
that the .NET still holds the memory. Now the performance of the whole system
is downgraded.

4. The only thing I can make the memory release to Windows is to call
GC.Collect(). Is this the only way to release that memory? Do I suppose to do
this?

Can anybody confirm if this is a bug in .NET? Appearantly it does not make
sense to hold the memory FOREVER even if nobody uses it.

Thanks,

Yang.



1.1 framework seems to work fine. The GC performs a collect and releases all
the memory very soon (a second) after the array runs out of scope. If you''re
using 1.0 then it could very well be what Daniel mentioned.

Imran.
"Yang" <Ya**@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...

I found a very strange behavior when I write a C# windows application. If I
allocate a huge chunk of memory by an array, the memory will never be
released by .NET.

The problem can be demostrated as follows:

1. Just create the simplest windows form project and add a button and
handler like this:

private void button1_Click(object sender, System.EventArgs e)
{
byte[] aaa = new byte[300000000];
for(int i=0; i<aaa.Length; i++)
aaa[i] = 10;
}

2. After executing the above code, I observed the memory in Task Manager.
The Commit Charge Total jumped to 300M and the MEM Usage shows the same
thing. The problem is whatever you do the memory usage can never drop
back. I
have tried to open many other applications and NOTHING can make the memory
get released.

3. The variable aaa has gone out of scope, so it does not make any sense
that the .NET still holds the memory. Now the performance of the whole
system
is downgraded.

4. The only thing I can make the memory release to Windows is to call
GC.Collect(). Is this the only way to release that memory? Do I suppose to
do
this?

Can anybody confirm if this is a bug in .NET? Appearantly it does not make
sense to hold the memory FOREVER even if nobody uses it.

Thanks,

Yang.



这篇关于这是.NET垃圾收集中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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