Name: gm110360 Date: 10/30/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The following Memory.java ends up with an OutOfMemory error. This is very unpleasant concerning that javax.swing.SwingUtilities.invokeLater() executes run() method of a thread but not start() which means that recommended use of it leads to memory leaks.
import java.util.*;
import java.lang.ref.*;
public class Memory {
public static void main(String argv[]) {
GarbageListener gl = new GarbageListener();
while (true) {
gl.register(new MemorySpace());
}
}
}
class MemorySpace extends Thread {
int [] some = new int[10000];
public void run() {
}
}
class GarbageListener {
Map types = new HashMap();
ReferenceQueue queue = new ReferenceQueue();
Thread printer = null;
public void register(Object o) {
if (null == printer) {
printer = new Thread() {
public void run() {
while (true) {
try {
Reference r = queue.remove();
System.err.println(types.get(r));
}
catch(InterruptedException ie) {
}
}
}
};
printer.start();
}
types.put(new WeakReference(o, queue), o.getClass().getName());
}
}
(Review ID: 134379)
======================================================================
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The following Memory.java ends up with an OutOfMemory error. This is very unpleasant concerning that javax.swing.SwingUtilities.invokeLater() executes run() method of a thread but not start() which means that recommended use of it leads to memory leaks.
import java.util.*;
import java.lang.ref.*;
public class Memory {
public static void main(String argv[]) {
GarbageListener gl = new GarbageListener();
while (true) {
gl.register(new MemorySpace());
}
}
}
class MemorySpace extends Thread {
int [] some = new int[10000];
public void run() {
}
}
class GarbageListener {
Map types = new HashMap();
ReferenceQueue queue = new ReferenceQueue();
Thread printer = null;
public void register(Object o) {
if (null == printer) {
printer = new Thread() {
public void run() {
while (true) {
try {
Reference r = queue.remove();
System.err.println(types.get(r));
}
catch(InterruptedException ie) {
}
}
}
};
printer.start();
}
types.put(new WeakReference(o, queue), o.getClass().getName());
}
}
(Review ID: 134379)
======================================================================
- duplicates
-
JDK-4533087 If start method is never called on a thread, that thread is leaked
- Resolved