views:

196

answers:

1

Consider that I have a main Thread which executes a new Runnable in a new Thread. Now, while the new Thread is executing, the Java VM runs out of memory and throws an OutOfMemoryError.

What happens? Does the target thread stop? Will the main thread continue? When the new Thread crashes, will the VM reclaim the memory from it and let execution continue?

+5  A: 

One of the threads will throw OutOfMemoryError during the allocation part of a new. To avoid thrashing, there is likely to be a significant amount of memory free after the error is thrown. So the other threads can carry on, and are unlikely to OOME for a period.

If the OOME is not caught, then the thread will exit and the uncaught exception handler called. On exit the thread and associated objects will be available for garbage collection as usual (subject to not being referenced by other means).

Tom Hawtin - tackline