jdb does not enable garbage collection for objects for which garbage collection was disable earlier by jdb 'disablegc' command.
To reproduce the problem next class was used.
Steps:
* disablegc for created WeakReference wr.
* enablegc for same object
import java.lang.ref.*;
class node {
byte [] arr;
node(int size){ arr = new byte[size]; }
}
class GC {
public static void GenGarb() {
int num = 50;
SoftReference[] srArr = new SoftReference[num];
WeakReference[] wrArr = new WeakReference[num];
for (int j = 0;j < num;j++) {
for (int i = 0;i < num;i++) {
srArr[i] = new SoftReference(new node(90000));
wrArr[i] = new WeakReference(new node(90000));
}
System.gc();
}
}
public static void main(String args[]) {
Runtime rt = Runtime.getRuntime();
Reference rc;
ReferenceQueue rq = new ReferenceQueue();
WeakReference wr = new WeakReference(new node(20000), rq);
node t;
int count = 0;
try {
while (count++ < 10) {
GenGarb();
t = (node) wr.get();
if (t == null) {
rc = rq.poll();
System.out.println("Object has been cleared.");
wr = new WeakReference(new node(200000), rq);
} else {
rc = rq.poll();
System.out.println("Object is still in memory.");
}
}
}catch (OutOfMemoryError e) {
System.gc();
System.out.println("Failed at the " + count +"th iteration.");
}
}
}
To reproduce the problem next class was used.
Steps:
* disablegc for created WeakReference wr.
* enablegc for same object
import java.lang.ref.*;
class node {
byte [] arr;
node(int size){ arr = new byte[size]; }
}
class GC {
public static void GenGarb() {
int num = 50;
SoftReference[] srArr = new SoftReference[num];
WeakReference[] wrArr = new WeakReference[num];
for (int j = 0;j < num;j++) {
for (int i = 0;i < num;i++) {
srArr[i] = new SoftReference(new node(90000));
wrArr[i] = new WeakReference(new node(90000));
}
System.gc();
}
}
public static void main(String args[]) {
Runtime rt = Runtime.getRuntime();
Reference rc;
ReferenceQueue rq = new ReferenceQueue();
WeakReference wr = new WeakReference(new node(20000), rq);
node t;
int count = 0;
try {
while (count++ < 10) {
GenGarb();
t = (node) wr.get();
if (t == null) {
rc = rq.poll();
System.out.println("Object has been cleared.");
wr = new WeakReference(new node(200000), rq);
} else {
rc = rq.poll();
System.out.println("Object is still in memory.");
}
}
}catch (OutOfMemoryError e) {
System.gc();
System.out.println("Failed at the " + count +"th iteration.");
}
}
}