C#中无用的垃圾收集? [英] Useless Garbage Collection in C#?

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

问题描述

以下是我对你的挑战:


1)假设对象A在死亡之前必须进行清理。另外假设有多个引用A的
。我怎样才能确定何时以及谁调用Dispose

(如果你使用Dispose进行清理)。


2)假设有一些其他来源通过句柄引用A.

也就是说,这些来源不直接引用A而是通过

中间手柄机构。目的不是干扰A的终身管理,而是在A存在的时候使用A,并在

时放弃它。它已经消失了。 />

如果没有GC,我可以很容易地做到这一点(例如,在C ++中)。但是

现在,恩......任何人都可以给我一个解决方案吗?

Here are my challenges to you:

1) Suppose object A has to do cleanup before it dies. Also suppose there are
multiple references to A. How can I determine when and who to call Dispose
(if you use Dispose for the cleanup).

2) Suppose there are some other sources who reference A through handles.
That is, those sources do not reference A directly but through an
intermediate handle mechanism. The purpose is not to interfere with the
lifetime management of A, but to use A when it exists and to give it up when
it''s gone.

Without GC, I can do them pretty easy and good (for example,in C++). But
now, en... Anybody could give me a solution?

推荐答案

Born写道:
Born wrote:

以下是我的挑战:


1)假设对象A在死亡之前必须进行清理。另外假设有多个引用A的
。我怎样才能确定何时以及谁调用Dispose

(如果你使用Dispose进行清理)。


2)假设有一些其他来源通过句柄引用A.

也就是说,这些来源不直接引用A而是通过

中间手柄机构。目的不是干扰A的终身管理,而是在A存在的时候使用A,并在

时放弃它。它已经消失了。 />

如果没有GC,我可以很容易地做到这一点(例如,在C ++中)。但现在,

,恩......任何人都可以给我一个解决方案吗?
Here are my challenges to you:

1) Suppose object A has to do cleanup before it dies. Also suppose there are
multiple references to A. How can I determine when and who to call Dispose
(if you use Dispose for the cleanup).

2) Suppose there are some other sources who reference A through handles.
That is, those sources do not reference A directly but through an
intermediate handle mechanism. The purpose is not to interfere with the
lifetime management of A, but to use A when it exists and to give it up when
it''s gone.

Without GC, I can do them pretty easy and good (for example,in C++). But
now, en... Anybody could give me a solution?



如果我们谈论.NET内部分配的内存,那么你就不会做b
做任何事情。 .NET将为您处理。你不做任何清理



如果你使用.NET之外的资源,那么你实现

IDisposable并调用处理对象。


Arne

If we talk about allocated memory inside .NET, then you don''t
do anything. .NET will handle that for you. You do not do
any cleanup.

If you use resources outside of .NET, then you implement
IDisposable and call Dispose on the object.

Arne


Born写道:
Born wrote:

1)假设对象A在死亡之前必须进行清理。
1) Suppose object A has to do cleanup before it dies.



这是一个资源,而不仅仅是内存。 GC用于内存,而不是
资源。

It is a resource then, and not just memory. GC is for memory, not
resources.


另外假设有多个引用的多个引用

。我可以确定何时以及向谁调用Dispose

(如果您使用Dispose进行清理)。
Also suppose there are
multiple references to A. How can I determine when and who to call Dispose
(if you use Dispose for the cleanup).



与C ++相同。

The same way you would in C++.


2)假设还有一些其他来源参考一个直通句柄。

也就是说,这些来源不是直接引用A而是通过一个

中间句柄机制。目的不是为了干扰A的终身管理,而是在A存在时使用A并在

时放弃它。
2) Suppose there are some other sources who reference A through handles.
That is, those sources do not reference A directly but through an
intermediate handle mechanism. The purpose is not to interfere with the
lifetime management of A, but to use A when it exists and to give it up when
it''s gone.



你通常会使用WeakReference(如果它只是你感兴趣的内存

in)并且可能订阅A的死亡(如果你也对资源使用情况感兴趣

)。

You''d usually use WeakReference (if it''s only memory you''re interested
in) and possibly subscribe to the death of A (if you''re also interested
in resource usage).


如果没有GC,我可以很容易地做到这一点和好(例如,在C ++中)。
Without GC, I can do them pretty easy and good (for example,in C++).



要记住的两件事:GC是添加剂 - 你仍然可以做手动内存

管理如果你必须(无论是对象池还是其他机制) ),和

GC用于记忆 - 即收集物品使用的内存,而不是
资源。


- 巴里


-
http:// barrkel .blogspot.com /




" Barry Kelly" < ba *********** @ gmail.com写信息

新闻:5s ******************* ************* @ 4ax.com ...

"Barry Kelly" <ba***********@gmail.comwrote in message
news:5s********************************@4ax.com...

>还假设有
多次引用A.如何确定何时以及应该给谁调用
处理
(如果使用Dispose进行清理)。
>Also suppose there are
multiple references to A. How can I determine when and who to call
Dispose
(if you use Dispose for the cleanup).



和C ++一样。


The same way you would in C++.



在C ++中,我使用引用计数和智能指针。由于堆栈

对象能力,整个过程非常自动化。我并不完全知道谁会触发清理工作。但是对于C#中的Dispose,用户必须明确触发它,这是问题。

In C++, I use reference counting and smart pointers. Because of the stack
object capability, the whole process is quite automatic. I don''t exactly
know who triggers the cleanup. But for Dispose in C#, the user must
explicitly trigger it, which is the problem.


你通常会使用WeakReference(如果它只是你感兴趣的内存

in)并可能订阅A的死亡(如果你也感兴趣

资源使用) 。
You''d usually use WeakReference (if it''s only memory you''re interested
in) and possibly subscribe to the death of A (if you''re also interested
in resource usage).



WeakReference似乎是句柄的替代品。我正在检查它。

WeakReference seems an alternative for handles. I''m checking it.


要记住两件事:GC是添加剂 - 你仍然可以做手动记忆

管理,如果你必须
Two things to remember: GC is additive - you can still do manual memory
management if you must



我想开发一个引用计数+智能参考机制。虽然这对于GC来说是浪费,但它确定了破坏点(适用于b / b
CPU /资源很重但内存便宜的对象)同时缓解了问题

谁应该触发破坏点。但是,为了做到这一点,

我需要堆栈对象功能。我不知道该怎么做,因为我缺乏对B#的b $ b $。

I want to develop a reference counting + smart reference mechanism. Although
it''s a waste on GC, it makes the destruction points determined ( good for
CPU/resource heavy but memory cheap objects ) while alleviates the problem
of who should trigger the destruction points. However, in order to do that,
I need stack object capability. I don''t know how to do it because my lack of
experiece about C#.


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

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