代码如下:
public class he
{
public he()
{
test();
}
public void test()
{
for(int i=0;i<10000;i++)
{
System.out.println(i);
System.gc();
}
}
public static void main(String [] args)
{
new he();//没有它的引用
}
}
创建了一个对象,但并不保存它的引用,调用System.gc(),但是程序还是照常执行。虚拟机为什么没有回收这个对象呢?
public class he
{
public he()
{
test();
}
public void test()
{
for(int i=0;i<10000;i++)
{
System.out.println(i);
System.gc();
}
}
public static void main(String [] args)
{
new he();//没有它的引用
}
}
创建了一个对象,但并不保存它的引用,调用System.gc(),但是程序还是照常执行。虚拟机为什么没有回收这个对象呢?
3. 你觉得应该看见什么现象,才算回收了?
楼主的意思就是说,gc()了,he对象就不在了,那么就不会执行TEST()了...2. 调用 System.gc() 的时候,对象还没创建完呢(还运行在 constructor 里),怎么回收?
说明一切了
public class he
{
public he()
{
//test();
System.out.println();
}
public void test()
{
for(int i=0;i<10000;i++)
{
System.out.println(i);
System.gc();
}
}
public static void main(String [] args)
{
new he().test();
}
}
对象已经创建成功了阿,之后才执行函数的.
1 When you create an object of a class without a handle, somewhere in the machine, Java got to have some way to keep your object. Those works are taken care of invisibly.
2 I remember System.gc() doesn't ensure that it will collect these garbage.