JVM Memory leak after interrupting threads??

Discussion in 'Java' started by slubowsky@netzero.net, Jan 27, 2005.

  1. Guest

    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");
    }
    }
    }
    }
    , Jan 27, 2005
    #1
    1. Advertising

  2. Roland Guest

    On 27-1-2005 17:12, wrote:

    > 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_/ /__/
    / \ /_/ / \
    Roland, Jan 27, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Jan 27, 2005
    #3
  4. On Thu, 27 Jan 2005 08:59:25 -0800, wrote:

    > 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.

    --
    In pioneer days they used oxen for heavy pulling, and when one ox
    couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
    be trying for bigger computers, but for more systems of computers.
    --- Rear Admiral Grace Murray Hopper
    Stefan Schulz, Jan 27, 2005
    #4
  5. Roland Guest

    On 27-1-2005 17:59, wrote:

    > 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_/ /__/
    / \ /_/ / \
    Roland, Jan 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soeren Gerlach
    Replies:
    0
    Views:
    441
    Soeren Gerlach
    Jul 15, 2004
  2. mon Gars

    JVM Memory Leak

    mon Gars, Sep 9, 2004, in forum: Java
    Replies:
    2
    Views:
    4,077
  3. Heiko Neuhaus

    POSIX Threads causing memory leak

    Heiko Neuhaus, Dec 21, 2003, in forum: C++
    Replies:
    3
    Views:
    2,576
    Paul Pluzhnikov
    Dec 21, 2003
  4. cham
    Replies:
    5
    Views:
    770
  5. Icarus
    Replies:
    5
    Views:
    424
    Icarus
    Feb 18, 2009
Loading...

Share This Page