《Java的内存管理2》提到finalize方法延缓了GC在heap中对类实例的回收,尤其在JVM内存吃紧时,所幸的是这种延缓仅会发生一次。也正是因为finalize方法只会在GC首次打算回收该实例时被调用,因此finalize方法极大地不同于C++中的析构函数:当一个实例再次被JVM装入内存,直到它被销毁前,你无法在finalize方法中执行关闭文件连接等操作。
在C++缓存管理过程中,程序员通常会自己新建一个队列,将被清除出内存的实例推入队列,等下次被调用时再取出。好在java.lang.ref.ReferenceQueue帮我们实现了这个队列,我们只需将软引用实例在队列中注册即可。因此,如果你需要对象中的一些操作,仅在它的实例被GC回收时才执行,便可以通过ReferenceQueue队列调用它们。JavaDoc1.6写道:
SoftReferencepublic SoftReference ( referent,<? super > q)
-
创建一个引用给定对象的新的软引用,并向给定队列注册该引用。
-
参数:
-
referent
- 新的软引用将引用的对象 -
q
- 该引用向其注册的队列,如果不需要注册,则为 null
-
我将上篇示例代码中的finalize()方法的内容注释掉,新建了一个GC监听线程,监听哪些实例被GC清理到ReferenceQueue队列中,并显示它们。
ReferenceQueue中的remove()方法将返回队列中下一个新生效的实例引用(所有实例都已经在队列中注册,当一个实例被GC清理出内存后,该实例在队列中的引用就会生效)。线程随后被阻塞,直到队列中有新生效引用为止。JavaDoc1.6写道:
removepublic <? extends > remove ()throws
-
移除此队列中的下一个引用对象,阻塞到某个对象变得可用为止。
-
-
返回:
- 某个引用对象,阻塞到某个对象变得可用为止。 抛出:
-
- 如果等待被中断
控制台输出结果如下:
Creating bookshelf: 1
Creating bookshelf: 2
Monitor:1 was collected.
Creating bookshelf: 3
Creating bookshelf: 4
Creating bookshelf: 5
Creating bookshelf: 6
Creating bookshelf: 7
Creating bookshelf: 8
Creating bookshelf: 9
Creating bookshelf: 10
Creating bookshelf: 11
Creating bookshelf: 12
Creating bookshelf: 13
Monitor:2 was collected.
Monitor:3 was collected.
Monitor:4 was collected.
Monitor:5 was collected.
Monitor:6 was collected.
Monitor:7 was collected.
Monitor:8 was collected.
Monitor:9 was collected.
Monitor:10 was collected.
Monitor:11 was collected.
Creating bookshelf: 14
Creating bookshelf: 15
Creating bookshelf: 16
Creating bookshelf: 17
Creating bookshelf: 18
Creating bookshelf: 19
Creating bookshelf: 20
Creating bookshelf: 21
Creating bookshelf: 22
Monitor:12 was collected.
Monitor:13 was collected.
Monitor:14 was collected.
Monitor:15 was collected.
Monitor:16 was collected.
Monitor:17 was collected.
Monitor:18 was collected.
Monitor:19 was collected.
Monitor:20 was collected.
Monitor:21 was collected.
Creating bookshelf: 23
Creating bookshelf: 24
Creating bookshelf: 2
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
class Book
{
char[] symbols = new char[300000];
}
class BookShelf
{
private int ID;
Book[] books = new Book[100];
public BookShelf()
{
for (int i = 0; i < books.length; i++)
{
books[i] = new Book();
}
// TODO Auto-generated constructor stub
}
public BookShelf(int ID)
{
this.ID = ID;
for (int i = 0; i < books.length; i++)
{
books[i] = new Book();
}
// TODO Auto-generated constructor stub
}
@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
// super.finalize();
// System.out.println("BookShelf: " + ID + " is dying!");
}
}
public class Library
{
public static void main(String[] args)
{
// BookShelf[] shelves = new BookShelf[50];
// SoftReference[] shelves = new SoftReference[50];
BookShelfReference[] shelves = new BookShelfReference[50];
GarbageMonitor monitor = new GarbageMonitor(BookShelfReference.collectedQueue);
monitor.start();
for (int i = 0; i < shelves.length; i++)
{
// shelves[i] = new BookShelf();
shelves[i] = new BookShelfReference(new BookShelf(i + 1));
System.out.println("Creating bookshelf: " + (i + 1));
}
}
}
class BookShelfReference extends SoftReference
{
int ID;
static ReferenceQueue<BookShelfReference> collectedQueue = new ReferenceQueue<BookShelfReference>();
public BookShelfReference(BookShelf shelf)
{
super(shelf, collectedQueue);
this.ID = ID;
// TODO Auto-generated constructor stub
}
}
class GarbageMonitor extends Thread
{
ReferenceQueue<BookShelfReference> queue;
public GarbageMonitor(ReferenceQueue<BookShelfReference> queue)
{
this.queue = queue;
// TODO Auto-generated constructor stub
}
@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
while (true)
{
try
{
BookShelfReference shelfRef = (BookShelfReference) queue.remove();
System.out.println("Monitor:" + shelfRef.ID + " was collected.");
}
catch (Exception e)
{
break;
// TODO: handle exception
}
}
}
}