testgarbage.java

来自「一些Java写的测试API的源代码」· Java 代码 · 共 45 行

JAVA
45
字号
/*
垃圾回收为一个低优先级的线程。
只有当内存不够时,才会进行垃圾处理
调用finalize();
*/

/*
见JDK API
java.lang.Object中
protected void finalize()
Called by the garbage collector on an object when garbage
collection determines that there are no more references to the object.

*/

class Garbage
{
	int index;
	static int count; //记录产生对象个数
	Garbage()
	{
		count++;
		System.out.println("object "+count+" construct");
		setID(count);
	}

	void setID(int ID)
	{
		index = ID;
	}
	
	protected void finalize()
	{
		System.out.println("object "+index+"is reclaimed");
	}	

	public static void main(String[] args)
	{
		new Garbage();
		new Garbage();
		new Garbage();
		new Garbage();
		System.gc();    
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?