JDK-4521146 : (thread) OutOfMemoryError when start() is never invoked on a Thread
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-10-30
  • Updated: 2005-09-09
  • Resolved: 2005-09-09
Related Reports
Duplicate :  
Description
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) 
======================================================================

Comments
EVALUATION Confirmed this was fixed by the dupe bug with a more specific test case.
09-09-2005

WORK AROUND Name: gm110360 Date: 10/30/2001 Always starting a thread. For javax.swing.SwingUtilities always reusing the only one created static thread instead like this: static Component focusComp; static Thread focusThr; public static void requestFocusLater(Component c) { if (null == focusThr) focusThr = new Thread () { public void run() { Component c = focusComp; focusComp = null; if(null == c) return; repositionTabbedPanes(c); requestFocusIntelligently(c); } }; focusComp = c; SwingUtilities.invokeLater(focusThr); }; ======================================================================
11-06-2004

EVALUATION As this code doesn't use SwingUtilities, the leak is not in Swing, but I believe the result of never invoking start on a Thread. While Thread implements Runnable you typically wouldn't pass one to invokeLater so that this usually isn't an issue when using SwingUtilities.invokeLater. ###@###.### 2002-01-03 This bug is either a duplicate or closely related to 4508232 and 4533087. -- iag@sfbay 2002-01-03
03-01-2002