Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryError in a separate Java thread

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?

like image 200
jevon Avatar asked Oct 08 '09 10:10

jevon


People also ask

How many threads the OS will allow your JVM to use?

It means totally only 1024 threads can be created in this machine. So if your application is creating more than 1024 threads, it's going to run into java. lang.

What causes Outofmemory exception Java?

OutOfMemoryError exception. Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further.

How do I fix OutOfMemoryError in Java?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

What went wrong unable to create new native thread?

The message java. lang. OutOfMemoryError: Unable to create new native thread means that the Java application has hit the limit of how many Threads it can launch.


1 Answers

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

like image 184
Tom Hawtin - tackline Avatar answered Sep 30 '22 18:09

Tom Hawtin - tackline