JDK-4519036 : Threads that were not started are never garbage collected
  • Type: Bug
  • Component: other-libs
  • Sub-Component: other
  • Affected Version: 1.4.0
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-10-24
  • Updated: 2001-10-24
  • Resolved: 2001-10-24
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 10/24/2001


java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, 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 which means that recommended use of it leads to memory leaks.  By making MemorySpace not a subclass of Thread, there is no OutOfMemoryError.

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: 134378) 
======================================================================

Comments
WORK AROUND Name: nt126004 Date: 10/24/2001 Customer Workaround : 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

PUBLIC COMMENTS This is a duplicate of bug 4508232.
10-06-2004