"迫使"垃圾收集 [英] "forcing" garbage collection

查看:65
本文介绍了"迫使"垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕C#/ CLR垃圾收集。在一个

ASP.NET项目中,我遇到了问题,因为在我的代码完成很久之后,对象析构函数被另一个线程调用了。我想要

来知道我是否可以立即强制它(当对象超出范围时)
,而不需要返回并添加所有必需的

Dispose()调用..


public方法()

{

CustomClass o = new CustomClass(..);

..

..

..

};


有没有办法强迫当对象超出范围时,与方法相同的线程调用CustomClass析构函数




谢谢。

I''m trying to wrap my ming around C#/CLR garbage collection. In an
ASP.NET project, I''m having a problem because an object destructor is
being called by another thread, long after my code is done. I''m wanting
to know if I can force it to happen immediately (when the object goes
out of scope) without going back and adding all of the required
Dispose() calls..

public Method()
{
CustomClass o = new CustomClass(..);
..
..
..
};

Is there a way to "force" the CustomClass destructor to be called right
when the object goes out of scope, by the same thread as the Method?

Thanks.

推荐答案

2008年5月26日星期一16:31:20 -0700,Jon Mcleod< jo *********** @ yahoo.com> ;

写道:
On Mon, 26 May 2008 16:31:20 -0700, Jon Mcleod <jo***********@yahoo.com>
wrote:

我正试图将我的事情包裹在C#/ CLR垃圾收集中。在一个

ASP.NET项目中,我遇到了问题,因为在我的代码完成很久之后,对象析构函数被另一个线程调用了。我想要

来知道我是否可以立即强制它(当对象超出范围时)
,而不需要返回并添加所有必需的

Dispose()调用..
I''m trying to wrap my ming around C#/CLR garbage collection. In an
ASP.NET project, I''m having a problem because an object destructor is
being called by another thread, long after my code is done. I''m wanting
to know if I can force it to happen immediately (when the object goes
out of scope) without going back and adding all of the required
Dispose() calls..



有办法。


但正确的方法解决这个问题就是调用Dispose()。使用

使用声明(首选),或明确调用Dispose()。


不要通过添加更多错误来修复错误代码。如果这意味着必须返回

并重写一堆没有正确处理的代码,那么就是



Pete

There is a way.

But the right way to solve this issue is to call Dispose(). Either use
the "using" statement (preferable), or call Dispose() explicitly.

Don''t fix buggy code by adding more bugs. If that means having to go back
and rewrite a bunch of code that wasn''t dispoing of things properly, so be
it.

Pete


Jon Mcleod写道:
Jon Mcleod wrote:

我正试图包裹我的ming围绕C#/ CLR垃圾收集。在一个

ASP.NET项目中,我遇到了问题,因为对象析构函数
I''m trying to wrap my ming around C#/CLR garbage collection. In an
ASP.NET project, I''m having a problem because an object destructor



destructor = finalizer?

destructor = finalizer ?




由另一个线程调用,
is
being called by another thread,



有些代码显式调用终结器吗?

Some code explicit calling a finalizer ?


我的代码完成后很久。我想要

来知道我是否可以立即强制它(当对象超出范围时)
long after my code is done. I''m wanting
to know if I can force it to happen immediately (when the object goes
out of scope)



拨打电话?还是一个隐含的电话?那另一个帖子中的

引用怎么样?

Making an explicit call ? Or an implicit call ? And what about the
reference in the other thread ?


没有返回并添加所有必需的

Dispose()调用..


public方法()

{

CustomClass o = new CustomClass(..);

..

..

..

};


有没有办法强迫当对象超出范围时,与方法相同的线程调用CustomClass析构函数


without going back and adding all of the required
Dispose() calls..

public Method()
{
CustomClass o = new CustomClass(..);
..
..
..
};

Is there a way to "force" the CustomClass destructor to be called right
when the object goes out of scope, by the same thread as the Method?



Dispose用于非托管资源。


垃圾收集用于托管资源。


如果对象只保存托管资源,那么让GC做它想要做的事情时它会做什么。


如果对象拥有非托管资源然后使用

Dispose模式。


Arne

Dispose is for unmanaged resources.

Garbage collection is for managed resources.

If the object only holds managed resources then let the GC do
what it wants to do when it want to do it.

If the object holds unmanaged resources then use the
Dispose pattern.

Arne


" ; Arne Vajh?j < ar ** @ vajhoej.dkwrote in message

news:48 *********************** @ news.sunsite。 dk ...
"Arne Vajh?j" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...

Jon Mcleod写道:
Jon Mcleod wrote:

>我正在试图包裹我的围绕C#/ CLR垃圾收集。在一个ASP.NET项目中,我遇到了问题,因为对象析构函数
>I''m trying to wrap my ming around C#/CLR garbage collection. In an
ASP.NET project, I''m having a problem because an object destructor



析构函数=终结符?


destructor = finalizer ?


>
被另一个线程调用,
> is
being called by another thread,



有些代码显式调用终结器吗?


Some code explicit calling a finalizer ?


>我的代码完成很久之后。我想要知道我是否可以立即强制它(当物体熄灭时范围内)
> long after my code is done. I''m wanting
to know if I can force it to happen immediately (when the object goes out
of scope)



制作一个明确的电话?还是一个隐含的电话?那另一个帖子中的

引用怎么样?


Making an explicit call ? Or an implicit call ? And what about the
reference in the other thread ?


>无需返回并添加所有必需的
Dispose()调用..

公共方法()
{
CustomClass o = new CustomClass(..);
..
..
..
};

有没有办法强迫当对象超出范围时,要通过与方法相同的线程调用CustomClass析构函数吗?
> without going back and adding all of the required
Dispose() calls..

public Method()
{
CustomClass o = new CustomClass(..);
..
..
..
};

Is there a way to "force" the CustomClass destructor to be called right
when the object goes out of scope, by the same thread as the Method?



Dispose用于非托管资源。


垃圾收集用于托管资源。


如果对象只保存托管资源,那么让GC做它想要做的事情时它会做什么。


如果对象拥有非托管资源然后使用

Dispose模式。


Arne


Dispose is for unmanaged resources.

Garbage collection is for managed resources.

If the object only holds managed resources then let the GC do
what it wants to do when it want to do it.

If the object holds unmanaged resources then use the
Dispose pattern.

Arne



Dispose也很有用当管理时资源实际上是一个OS

资源,例如套接字。在这种情况下,明确地处理插座

可以在两台不同的机器上释放操作系统资源。


Mike。

Dispose is also useful when the "managed" resource is actually an OS
resource such as a socket. In this case explicitly disposing the socket
frees up OS resources on two different machines.

Mike.


这篇关于&QUOT;迫使&QUOT;垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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