WeakReference无法正常工作? [英] WeakReference not working?

查看:98
本文介绍了WeakReference无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我们写了一个缓存,可以有不同的行为。其中一个

是WeakReference-Cache。目的是,一旦一个对象在应用程序中不再被引用

,它应该从缓存中消失。


这几乎总是好的,但不是某些物品,我找不到

为什么。出于某种原因,即使我做了MyObject = Nothing,还有一个

GC.Collect,它仍然有WeakReference.IsAlive = True......


有人知道为什么会这样吗?我能做些什么呢?我应该对我所有的objetc实施ID可以吗?这是如何工作的?我是否需要

才能在重写Dispose方法中做任何特别的事情?

非常感谢提前,

Pieter


这就是我们如何处理GC.Collect以及删除

不再使用的对象...


私有共享Sub GarbageCollect()

Dim lstID作为新列表(整数)

GC.Collect()''总理收集

GC.WaitForPendingFinalizers()''参加la fin du thread

GC.Collect()''dexièmecollectionpour tout enlever

GC.WaitForPendingFinalizers()''参加la fin du thread


For Each i as Integer in dicCacheWeakReference.Keys

if not dicCacheWeakReference(i).IsAlive Then

lstID.Add(i)''si l''objet n''existe plus,l''ajoute dans la

listeàeffacer

结束如果

下一页


每个intID作为整数在lstID中

dicCacheWeakReference.Remove(intID)''efface les ref érencesmorte

du cache

下一页

End Sub

解决方案

< blockquote> Pieter写道:





我们写了一个缓存,可以有不同的行为。其中一个

是WeakReference-Cache。目的是,一旦一个对象在应用程序中不再被引用

,它应该从缓存中消失。


这几乎总是好的,但不是某些物品,我找不到

为什么。出于某种原因,即使我做了MyObject = Nothing,还有一个

GC.Collect,它仍然有WeakReference.IsAlive = True......


有人知道为什么会这样吗?



垃圾收集不保证收集_all_对象可收集的物品,它只收集有效收集的对象。


垃圾收集通常只收集第一个

代堆中的对象。由于您缓存的对象可能比大多数对象长得多,所以它们可能已被移动到

第二代或第三代堆。


我该怎么办呢?我应该对我所有的objetc实施ID可以吗?这是如何工作的?我是否需要

才能在重写Dispose方法中执行任何特殊操作?



为什么有任何理由对此采取任何措施?如果垃圾

收藏家没有收集物品,他们使用的内存不需要
。如果应用程序在当前堆中的内存不足,那么垃圾收集器会进行更彻底的收集以收集

任何可能的内容,然后再请求更多内存系统。


>

非常感谢提前,


Pieter

这就是我们如何处理GC.Collect和删除

不再使用的对象...


私有共享Sub GarbageCollect()

Dim lstID作为新列表(整数)


GC.Collect()''premier collect

GC.WaitForPendingFinalizers()''参加la fin du thread

GC.Collect()''dexièmecollectionpour tout enlever

GC.WaitForPendingFinalizers() ''参加la fin du thread


For Each i as Integer in dicCacheWeakReference.Keys

If Not dicCacheWeakReference(i).IsAlive Then

lstID.Add(i)''si l''objet n''existe plus,l''ajoute dans la

listeàeffacer

End如果

下一页


每个intID作为整数在lstID

dicCacheWeakReference.Remove(intID)''effacelesreférencestorte

du cache

下一页

结束Sub



你为什么打电话给GC。收集什么?内存管理处理

本身,几乎没有任何理由干扰它。


除非你知道关于内存使用的重要事项<没有垃圾收集器,你可能会降低

应用程序的性能而不是改进它。


-

G?跑Andersson

_____
http://www.guffa.com


" G?ran Andersson" < gu *** @ guffa.com写信息

news:u


************** @ TK2MSFTNGP02 .phx.gbl ...


>

为什么有任何理由对此采取任何措施?



整个目的不是收集内存,而是扔掉对象......


这就是所需要的:如果用户想要打开对象A,但是对象A已经打开(并且已使用),那么它应该返回相同的对象。但是,如果它没有打开(或者之前打开但不再使用),它应该返回一个新的

数据库副本。


这正是我们使用WeakReferences的原因...


如果应用程序在当前堆中的内存不足,在系统请求更多内存之前,垃圾

收集器会进行更彻底的收集以收集任何可能的内容。



我可以强迫他做更彻底的收集吗?

更好(因为我不喜欢使用垃圾收藏家):没有

有另一种方法来实现这种行为吗?


谢谢,


Pieter


Hi,

We have written a cache, which can have different behaviours. One of these
is a WeakReference-Cache. The purpose is that, once an object isn''t referred
anymore in the application, it should dissappear from the cache.

This works almost always fine, but not for some objects, and I can''t find
out why. For some reason, even when I do a "MyObject = Nothing" and a
"GC.Collect", it still has its "WeakReference.IsAlive = True"...

Anybody knows why this happens? And what can I do about it? Should I
implement IDisposable to all my objetcs? And how does that work? Do I have
to do anything special in the overriden Dispose-method?
Thanks a lot in advance,
Pieter

This is how we did the GC.Collect and the removal of the
not-anymore-used-objects...

Private Shared Sub GarbageCollect()
Dim lstID As New List(Of Integer)

GC.Collect() ''premier collect
GC.WaitForPendingFinalizers() ''attend la fin du thread
GC.Collect() ''dexième collect pour tout enlever
GC.WaitForPendingFinalizers() ''attend la fin du thread

For Each i As Integer In dicCacheWeakReference.Keys
If Not dicCacheWeakReference(i).IsAlive Then
lstID.Add(i) ''si l''objet n''existe plus, l''ajoute dans la
liste à effacer
End If
Next

For Each intID As Integer In lstID
dicCacheWeakReference.Remove(intID) ''efface les reférences morte
du cache
Next
End Sub

解决方案

Pieter wrote:

Hi,

We have written a cache, which can have different behaviours. One of these
is a WeakReference-Cache. The purpose is that, once an object isn''t referred
anymore in the application, it should dissappear from the cache.

This works almost always fine, but not for some objects, and I can''t find
out why. For some reason, even when I do a "MyObject = Nothing" and a
"GC.Collect", it still has its "WeakReference.IsAlive = True"...

Anybody knows why this happens?

A garbage collection isn''t guaranteed to collect _all_ objects that are
collectable, it only collects the objects that are efficient to collect.

A garbage collection usually only collects objects in the first
generation heap. As the objects that you cache probably lives for a
longer time than most objects, they are likely to have been moved to the
second or third generation heap.

And what can I do about it? Should I
implement IDisposable to all my objetcs? And how does that work? Do I have
to do anything special in the overriden Dispose-method?

Why is there any reason to do anything about it? If the garbage
collector doesn''t collect the objects, the memory that they are using
isn''t needed. If the application runs out of memory in the current heap,
the garbage collector does a more thorough collection to collect
anything that is possible, before more memory is requested from the system.

>
Thanks a lot in advance,
Pieter

This is how we did the GC.Collect and the removal of the
not-anymore-used-objects...

Private Shared Sub GarbageCollect()
Dim lstID As New List(Of Integer)

GC.Collect() ''premier collect
GC.WaitForPendingFinalizers() ''attend la fin du thread
GC.Collect() ''dexième collect pour tout enlever
GC.WaitForPendingFinalizers() ''attend la fin du thread

For Each i As Integer In dicCacheWeakReference.Keys
If Not dicCacheWeakReference(i).IsAlive Then
lstID.Add(i) ''si l''objet n''existe plus, l''ajoute dans la
liste à effacer
End If
Next

For Each intID As Integer In lstID
dicCacheWeakReference.Remove(intID) ''efface les reférences morte
du cache
Next
End Sub

Why are you calling GC.Collect at all? The memory management handles
itself, and there is hardly ever any reason to interfer with it.

Unless you know something important about the memory usage that the
garbage collector don''t, you will likely reduce the performance of the
application rather than improve it.

--
G?ran Andersson
_____
http://www.guffa.com


"G?ran Andersson" <gu***@guffa.comwrote in message
news:u


**************@TK2MSFTNGP02.phx.gbl...

>
Why is there any reason to do anything about it?

The whole purpose is not collecting memory, but throwing away the object...

This is what is needed: If the user wants to open object A, but object A is
already opened (and used), it should return the same objet. But if it''s not
opened (or opened before but not used anymore), it should return a fresh
copy of the database.

That''s exactly why we are using WeakReferences...

If the application runs out of memory in the current heap, the garbage
collector does a more thorough collection to collect anything that is
possible, before more memory is requested from the system.

And can I force him to do this more thorough collection?
And better (because I don''t really like using the garbage collector): isn''t
there another way to achieve this behaviour?

Thanks,

Pieter


这篇关于WeakReference无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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