如何阻止垃圾收集器 [英] How to stop garbage collector

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

问题描述

C#中是否有办法阻止垃圾收集器自动杀死我们的对象

。我想自己杀死它?


有没有这样做?

谢谢

解决方案

我忘了问这个场景中发生了什么。


ArrayList first = new ArrayList();


ArrayList second = first


first.Dispose( )


剂量这三行代码使得第二个无用的变量没有

在哪里?


谢谢


" Ray5531" <太阳神***** @ microsoft.com>在留言中写道

新闻:O3 ************** @ TK2MSFTNGP09.phx.gbl ...

有没有办法C#阻止垃圾收集器自动杀死我们的对象。我想自己杀死它?

有没有这样做?
谢谢


这里有几点需要注意。


1.你在结束时错过了一个分号第二行和第三行,所以

你不会编译=)

2. ArrayList对象没有Dispose方法。

3.假设你把它设置为null,first那么确实会是

null,但是第二是仍然是ArrayList实例。基本上,

你正在从第一个删除指针。对象。该对象不会被破坏/收集/无论如何,因为第二现在有一个参考

它。


此外,垃圾收集器不被称为对象抢夺者。有一个原因。
它只会收集垃圾(你不会使用的空间),而不是你还在使用的东西。

。 >

希望这会有所帮助,


Dan Cox

免责声明:我没有写它,只是想帮忙! =)


" Ray5531" <太阳神***** @ microsoft.com>在留言中写道

新闻:uQ ************* @ TK2MSFTNGP15.phx.gbl ...

我忘了问发生了什么事在这种情况下。

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()
<这三行代码的剂量使得第二行成为无用的变量,而不是在哪里?

谢谢

Ray5531 <太阳神***** @ microsoft.com>在消息中写道
新闻:O3 ************** @ TK2MSFTNGP09.phx.gbl ...

在C#中有没有办法阻止垃圾收藏家自动杀死我们的对象
我想自己杀死它?

有没有这样做?
谢谢



有没有办法手动抓取收集对象?

谢谢

" CSharper" <做** @ botherme.com>在消息中写道

新闻:OL ******************** @ bignews4.bellsouth.net ...

这里有几点需要注意。

1.你在第二行和第三行的末尾错过了一个分号,所以
你不会编译=)
2. ArrayList对象没有Dispose方法。
3.假设您将其设置为null,first那么确实会是空的,但是第二是仍然是ArrayList实例。从本质上讲,你要从第一次删除指针。对象。物体不会被摧毁/收集/无论如何,因为第二物体不会被摧毁/收集。现在提到它了。

此外,垃圾收集器并没有被称为对象抢夺器。原因。它只会收集垃圾(你不使用的空间),而不是你还在使用的东西。

希望这有帮助,
Dan Cox
免责声明:我没有写它,只是想帮忙! =)

Ray5531 <太阳神***** @ microsoft.com>在消息中写道
新闻:uQ ************* @ TK2MSFTNGP15.phx.gbl ...

我忘了问这个场景中发生了什么。

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()

剂量这三行代码使得第二行成为无用的变量,而不是在哪里?

谢谢

Ray5531 <太阳神***** @ microsoft.com>在消息中写道
新闻:O3 ************** @ TK2MSFTNGP09.phx.gbl ...

在C#中有没有办法阻止垃圾收藏家自动杀死我们的对象
我想自己杀死它?

有没有这样做?
谢谢




Is there a way in C# to stop garbage collector from killing our object
automatically.I''d like to kill it myself??

Is there away to to do so?
Thanks

解决方案

I forgot to ask what happenes in this scenario.

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()

Dose these three lines of code makes second a useless variable poiting to no
where?

Thanks

"Ray5531" <Ra*****@microsoft.com> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...

Is there a way in C# to stop garbage collector from killing our object
automatically.I''d like to kill it myself??

Is there away to to do so?
Thanks



A couple of things to note here.

1. You''re missing a semicolon at the end of the second and third lines, so
you won''t compile =)
2. The ArrayList object doesn''t have a Dispose method.
3. Assuming instead that you set it to null, "first" would then indeed be
null, but "second" would still be the ArrayList instance. Essentially,
you''re removing the pointer from "first" to the object. The object doesn''t
get destroyed/collected/whatever, because "second" now has a reference to
it.

Also, the garbage collector isn''t called "the object snatcher" for a reason.
It''s only going to collect garbage (space you aren''t using), not stuff that
you are still using.

Hope this helps,

Dan Cox
DISCLAIMER: I didn''t write it, just trying to help! =)

"Ray5531" <Ra*****@microsoft.com> wrote in message
news:uQ*************@TK2MSFTNGP15.phx.gbl...

I forgot to ask what happenes in this scenario.

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()

Dose these three lines of code makes second a useless variable poiting to
no where?

Thanks

"Ray5531" <Ra*****@microsoft.com> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...

Is there a way in C# to stop garbage collector from killing our object
automatically.I''d like to kill it myself??

Is there away to to do so?
Thanks




Is there a way to manually grage collect an object?
Thanks
"CSharper" <do**@botherme.com> wrote in message
news:OL********************@bignews4.bellsouth.net ...

A couple of things to note here.

1. You''re missing a semicolon at the end of the second and third lines, so
you won''t compile =)
2. The ArrayList object doesn''t have a Dispose method.
3. Assuming instead that you set it to null, "first" would then indeed be
null, but "second" would still be the ArrayList instance. Essentially,
you''re removing the pointer from "first" to the object. The object doesn''t
get destroyed/collected/whatever, because "second" now has a reference to
it.

Also, the garbage collector isn''t called "the object snatcher" for a
reason. It''s only going to collect garbage (space you aren''t using), not
stuff that you are still using.

Hope this helps,

Dan Cox
DISCLAIMER: I didn''t write it, just trying to help! =)

"Ray5531" <Ra*****@microsoft.com> wrote in message
news:uQ*************@TK2MSFTNGP15.phx.gbl...

I forgot to ask what happenes in this scenario.

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()

Dose these three lines of code makes second a useless variable poiting to
no where?

Thanks

"Ray5531" <Ra*****@microsoft.com> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...

Is there a way in C# to stop garbage collector from killing our object
automatically.I''d like to kill it myself??

Is there away to to do so?
Thanks





这篇关于如何阻止垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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