Outdated: JDK 1.3 Bug - Memory Leak With Unstarted Threads

This section describes a reported bug for JDK 1.3 that unstarted thread objects cause memory leak.

There was a bug opened in 2001 for JDK 1.3 titled "Unstarted Thread causes memory leak" See http://developer.java.sun.com/developer/bugParade/bugs/4410846.html for details.

The bug report included the following program to show the memory leak:

public class ThreadTest {
    public static void main(String args[]) {
	while (true) {
	  try {
        Thread t = new Thread();
	  // start thread for memory leak workaround
//        t.start();
	  t = null;
        System.gc();
	  Thread.sleep(500);
	} catch (InterruptedException ie) {
		ie.printStackTrace();
	}
        }
    }
}

I ran this program on my J2SDK 1.4.1 system. As predicted, the memory usage of the JVM went up slowly at a rate of about 4K per 15 seconds.

As I mentioned in previous sections, Thread object created without a specific thread group will be added into the "main" group. So in the ThreadTest program included in the bug report, all the threads created will be added in the "main" group, and will stay there until they are started and ended. If we keep creating new threads, the number of threads in the "main" group will grow, and memory usage will also grow.

But in my opinion, this is not really a bug. JDK is designed to organize all Thread and ThreadGroup objects in a single thread group tree. Application programs are forced to use this structure, and must follow the rules.

However, this designed feature is dangerous if not used properly, as demonstrated by the bug report. It could be a risk for any server type application, if new threads are created for new connection requests and the start() calls are not executed for some reason.

One option to solve this problem is to:

A couple of notes on this option:

Without any help from JDK, finding a simple workaround to prevent this from the application side is not easy.

However, you can put a logic in your application to watch the number of unstarted threads. If this number is growing, you know that somewhere in your application threads are created without the start() calls. Here is the code to calculate the number of unstarted threads:

   ThreadGroup g = Thread.currentThread().getThreadGroup();
   while (g.getParent()!=null) g = g.getParent();
   Thread[] l = new Thread[g.activeCount()]];
   int unstartedThreadCount = g.activeCount() - g.enumerate(l,true):

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

Outdated Tutorials

 Outdated: Downloading and Installing JDK 8

 Outdated: Downloading and Installing JDK 1.7

 Outdated: Downloading and Installing JDK 1.6

 Outdated: Downloading and Installing JDK 1.5

Outdated: JDK 1.3 Bug - Memory Leak With Unstarted Threads

 References

 Full Version in PDF/EPUB