等待特定对象的垃圾回收 [英] Wait for Garbage Collection of a specific object

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

问题描述

 
跟踪正在等待删除的文件,并删除它们当
关联标记对象被垃圾回收器回收时。

可以在 FileCleaningTracker object。



现在我只是好奇我怎么能自己做到这一点?当垃圾收集器回收对象时,我的代码如何检测?解析方案

根据源代码它使用 PhantomReference 类。根据文档:


幻象参考对象,在收集器之后入队确定它们的参考对象可能被回收。 Phantom引用通常用于以比Java finalization机制更灵活的方式安排验尸前清理操作。



如果垃圾收集器在某个时间点确定虚幻引用的引用是虚幻可访问的,那么在那个时候或稍后时间它会将引用排入队列。

为了确保可回收对象保持如此状态,可能不会检索幻影引用的指示对象:幻像引用的get方法始终返回null。



不像软引用和弱引用,当垃圾收集器入队时,幻影引用不会自动清除。通过幻影引用可访问的对象将保持如此状态,直到清除所有这些引用或者自己无法访问。


PhantomReference 构造函数接受两个参数:


指示对象 - 新幻影引用将引用的对象



q - 引用要注册的队列,如果不需要注册,则为null

q 参数是 ReferenceQueue 类的一个实例。 PhantomReference 将被添加到 ReferenceQueue 中,当它是指涉变成幽灵可达。发生这种情况时,可以使用 poll() remove()来检索 PhantomReference ReferenceQueue class。



例如:

  T objectToWatch = ...; 
ReferenceQueue< T> referenceQueue = new ReferenceQueue< T>();
new PhantomReference< T>(objectToWatch,referenceQueue);

//稍后,可能在另一个线程中...
参考< ;?延伸T> nextReference = referenceQueue.remove();
//收拾起来!

注意: PhantomReference 具有名为< a href =http://java.sun.com/javase/6/docs/api/java/lang/ref/SoftReference.html =noreferrer> SoftReference WeakReference ,这也可能是有用的。这些关系记录在 java.lang.ref包文档


I was just digging around in the commons-io library and found this:

Keeps track of files awaiting deletion, and deletes them when
an associated marker object is reclaimed by the garbage collector.

This can be found in the documentation for the FileCleaningTracker object.

Now I am just curious how I can do this by myself? How can my code detect when an object is reclaimed by the garbage collector?

解决方案

According to the source code, it uses the PhantomReference class. According to the documentation:

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable, then at that time or at some later time it will enqueue the reference.

In order to ensure that a reclaimable object remains so, the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.

Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.

The PhantomReference constructor accepts two arguments:

referent - the object the new phantom reference will refer to

q - the queue with which the reference is to be registered, or null if registration is not required

The q argument is an instance of the ReferenceQueue class. The PhantomReference will be added to this ReferenceQueue when it's referent becomes phantom reachable. When this happens, you can retrieve the PhantomReference by using the poll() or remove() methods of the ReferenceQueue class.

For example:

T objectToWatch = ...;
ReferenceQueue<T> referenceQueue = new ReferenceQueue<T>();
new PhantomReference<T>(objectToWatch, referenceQueue);

// Later on, probably in another thread...
Reference<? extends T> nextReference = referenceQueue.remove();
// Tidy up!

Note: PhantomReference has sibling classes named SoftReference and WeakReference that may also be of use. The relationship between these are documented in the java.lang.ref package documentation.

这篇关于等待特定对象的垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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