JVM Memory leak after interrupting threads??

S

slubowsky

I have a little test program that simply loops creating threads and
then interrupts them. On my windows XP box with java 1.5.0 it hangs the
JVM with the following exception

Created 7124 threads
Exception in thread "main" java.lang.OutOfMemoryError: unable to create
new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Unknown Source)
at ThreadTest.main(ThreadTest.java:25)

What is going on here? Why is the JVM (apparently)not reclaiming the
thread memory after it dies?

See below for simple test program

Stephen

import java.lang.Thread.*;

public class ThreadTest
{
public static void main(String [] args)
{
int successfulThreadCreations = 0;
while(true)
{
try
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
int x = 0;
while(true)
{
x++;
try{Thread.sleep(25);}
catch(InterruptedException e){}
}
}
});
thread.start();
System.out.println("Created

"+(++successfulThreadCreations)+" threads");
try{Thread.sleep(100);}
catch(InterruptedException e){}
thread.interrupt();
}
catch(Exception e)
{
System.out.println("Died with exception " + e + " after
creating "+successfulThreadCreations+" threads");
}
}
}
}
 
R

Roland

I have a little test program that simply loops creating threads and
then interrupts them. On my windows XP box with java 1.5.0 it hangs the
JVM with the following exception

Created 7124 threads
Exception in thread "main" java.lang.OutOfMemoryError: unable to create
new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Unknown Source)
at ThreadTest.main(ThreadTest.java:25)

What is going on here? Why is the JVM (apparently)not reclaiming the
thread memory after it dies?
The 7124 are still running. The catch(InterruptedException) is inside
the while(true) loop. None of the threads finish.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
S

slubowsky

That catch belongs to the sleep. It shouldnt catch the thread
interrupt. (The cut and paste didnt keep the indenting of the nice and
clean)

Stephen
 
S

Stefan Schulz

That catch belongs to the sleep. It shouldnt catch the thread
interrupt. (The cut and paste didnt keep the indenting of the nice and
clean)

It does catch the InterruptedException you gave the thread. From the
javadoc of Thread.sleep:

Throws:
InterruptedException - if another thread has interrupted the current
thread. The interrupted status of the current thread is cleared when
this exception is thrown.
 
R

Roland

That catch belongs to the sleep. It shouldnt catch the thread
interrupt. (The cut and paste didnt keep the indenting of the nice and
clean)

Stephen
In addition to Stefan's reply, the javadoc of Thread.interrupt() states
<quote>
If this thread is blocked in an invocation of the wait() [...] methods
of the Object class, or of the [...] sleep(long), or sleep(long, int),
methods of this class, then its interrupt status will be cleared and it
will receive an InterruptedException.
[...]
If none of the previous conditions hold then this thread's interrupt
status will be set.
</quote>

IOW, Thread.interrupt() only sets an interrupt flag if it is not blocked
in sleep(25). It does not cause the thread to finish. If the thread
receives interrupt() while it's blocked in sleep(25), sleep() ends with
an InterruptException (and the thread's interrupt flag is cleared).
Because in your example the catch clause of InterruptException is empty
and you do not check for the interrupt status, it will continue to run.

--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top