class B
{
private
String name;
public B(String s)
{
name = s;
}
protected void finalize()
{
System.out.print(name);
}
}
class E
{
public static void m()
{
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args)
{ m(); System.gc();
}
}
From what I understand, objects x1, y1 created in m() are eligible for garbage collection outside m(), since m() doesn't return any reference to these objects.
System.gc() expends effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.
The general contract of finalize() is that it is invoked if and when the JJVM has determined that there is no longer any means by which this object can be accessed by any
thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The usual purpose of finalize, is to perform cleanup actions before the object is irrevocably discarded.
I suppose, in cases like above, any JVM would certainly be able to figure out that the objects x1, y1 are eligible for g.c. Else, it'd defeat the very purpose of having a g.c. facility (which cannot be forced) provided by the underlying language itself!