Solve java.lang.OutOfMemoryError : unable to create new native Thread - Xss JVM option


Contents of page >
  • What is java.lang.OutOfMemoryError : unable to create new native Thread ?
  • Solving OutOfMemoryError : unable to create new native Thread ?
  • How to use Using the VM (virtual machine) option ss?
    • Examples of using -Xss
  • What happens if value of -Xss set is too high?
  • What happens if value of -Xss set is too high?
  • Program which throws java.lang.OutOfMemoryError : unable to create new native Thread
  • -Xss option is also known as >
  • Default Values of -Xss for different platforms >
  • Every thread has its own stack >
  • How stack frames are created when thread calls new method?
  • Let’s learn how can we reduce the stack size?
    • What does thread stack contains?
    • Does stack stores/contains object OR what stack doesn’t contains?
    • What does heap contains?


What is java.lang.OutOfMemoryError : unable to create new native Thread ?
When JVM don’t have enough memory/space to create new thread it throws OutOfMemoryError : unable to create new native Thread.





Solving OutOfMemoryError : unable to create new native Thread ?
You can resolve “java.lang.OutOfMemoryError : unable to create new native Thread” by setting the appropriate size using -Xss vm option.

Solution 1 to “java.lang.OutOfMemoryError : unable to create new native Thread”  >
Try to increase the the -Xss value so that new threads gets enough stack space.

Solution 2 to “java.lang.OutOfMemoryError : unable to create new native Thread”  >
Alternatively you could also increase the heap size available using -Xms and -Xmx options and then try to increase and set appropriate -Xss value.


How to use Using the VM (virtual machine) option ss?
We can use the VM option ss to adjust the maximum stack size.
VM option is passed using -X followed by your VM option. So, passing ss with -X forms -Xss.

Examples of using -Xss

Pass memory value you want to allocate to thread stack with -Xss.

Example1 of using -Xss >
java -Xss512 MyJavaProgram
It will set the default stack size of JVM to 512 bytes.

Example2 of using -Xss >
java -Xss512k MyJavaProgram
It will set the default stack size of JVM  to 512 kilobytes.

Example3 of using -Xss >
java -Xss512m MyJavaProgram
It will set the default stack size of JVM  to 512 megabytes.

Example4 of using -Xss >
java -Xss1g MyJavaProgram
It will set the default stack size of JVM  to 1 gigabyte.

What happens if value of -Xss set is too high?
Setting excessive value of -Xss parameter could cause StackOverFlowError in java.


Program which throws java.lang.OutOfMemoryError : unable to create new native Thread
/**
*
* Write a program which could throw
* java.lang.OutOfMemoryError : unable to create new native Thread
*
*/
public class OutOfMemoryErrorUnableToCreateNewNativeThreadExample {
   public static void main(String[] args) {
          //start/spawn infinite Threads
          while(true){
                 final int i=0;
          new Thread(new Runnable(){
              public void run() {
                System.out.println(Math.random() +" ");
                  try {
                      Thread.sleep(10000000);
                  } catch(InterruptedException e) { }    
              }   
          }).start(); //When JVM don’t have enough space to create new thread it
                                 throws OutOfMemoryError : unable to create new native Thread
          }
   }
}

In the above program we are spawning infinite Threads.
In while loop we are starting threads and putting them to sleep for a long time, so that the threads don’t die. (Learn thread states in java)
After certain time JVM won’t have enough space to create new thread and throw OutOfMemoryError : unable to create new native Thread.

Note : As above program will throw  OutOfMemoryError : unable to create new native Thread. So, executing above program will make your system to run out of memory and will likely hang up.


Output of above >
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 infiniteThreads.main(infiniteThreads.java:19)



-Xss option is also known as >
Also you must know that -Xss option is same as -XX:ThreadStackSize



Default Values of -Xss for different platforms >
For windows 32 bit its 64 KB.
For linux 32 bit its 128 KB.
For windows 64 bit its 128 KB.
For linux 64 bit its 256 KB.
For Solaris Sparc it’s 512KB.


Every thread has its own stack >
You must know that each and every thread has its own stack, which makes the methods thread-safe as well.

How stack frames are created when thread calls new method?
As we know each and every thread has its own stack. Whenever new method is called new stack frame is created and it is pushed on top of that thread's stack.

Let’s learn how can we reduce the stack size?
Before answering this question that how can we reduce the stack size, I’ll like like to discuss few basics what stack and heap can contain >

What does thread stack contains?
The stack contain
  • All the local variables,
  • All the parameters,
  • All the return address.
Does stack stores/contains object OR what stack doesn’t contains?
Stack never stores object, but it stores object reference.

What does heap contains?
Heap contains objects.
Heap even contains arrays because arrays are objects.

So, now let’s learn how can we reduce the stack size. We can try to group all the local variables and parameters in object so that only reference to the object stays on stack, which in turn will help us in reducing the stack space occupied,







Summary -

So in this core java tutorial we learned What is java.lang.OutOfMemoryError : unable to create new native Thread ? How to Solve OutOfMemoryError : unable to create new native Thread. How to use Use the VM (virtual machine) option ss. Examples of using -Xss. Program which throws java.lang.OutOfMemoryError : unable to create new native Thread.





Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

What are Minor, Major and Full garbage collection in JVM in java


JVM Heap memory (Hotspot heap structure) with diagram in java


What are Young, Old (tenured) and Permanent Generation in JVM in java


Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java


What are -Xms and -Xmx JVM parameters in java, And differences between them with examples


No comments:

Post a Comment