Java:使用jlibs保证垃圾回收 [英] Java: Guaranteed garbage collection using jlibs

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

问题描述

以下摘录自来自jlibs的RuntimeUtil.java 保证GC完成垃圾收集。

既然它也使用System.gc(),我不明白他们怎么能保证它会发生100%的时间。



以下是代码片段:

  / ** 
*此方法保证垃圾收集完成
*而不是< code> {@ link System#gc()}< / code>
* /
public static void gc(){
Object obj = new Object();
WeakReference ref = new WeakReference< Object>(obj);
obj = null;
while(ref.get()!= null)
System.gc();


解决方案

从我所能看到的应该像他们一样工作

<1>创建一个对象
2)获得对它的弱引用
3)null引用它,为了将其标记为垃圾收集
4)并等待while循环,直到它真正消失


The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.

Since, it also uses System.gc(), i dont understand how can they guarantee that it will happen 100% of the times.

Following is the snippet:

/**
     * This method guarantees that garbage collection is
     * done unlike <code>{@link System#gc()}</code>
     */
    public static void gc(){
        Object obj = new Object();
        WeakReference ref = new WeakReference<Object>(obj);
        obj = null;
        while(ref.get()!=null)
            System.gc();
    }

解决方案

From what I can see it should work as they

1) create an object 2) get a "weak" reference to it 3) null the reference to it, in order to mark it for garbage collection 4) and wait with the while loop until it actually disappears

这篇关于Java:使用jlibs保证垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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