Differences between Java stack overflow ror and outofmemoryerror

1, StackOverflow: Whenever a Java program starts a new thread, the Java virtual machine allocates a stack for it, and the Java stack keeps the thread running in frames. When a thread calls a method, the JVM presses a new stack frame onto the thread’s stack, and the stack frame exists as long as the method has not returned.
if the method has too many layers of nested calls (such as recursive calls), as the number of frames in the Java stack increases, the total size of all stack frames in the stack of this thread will eventually be greater than the value set by -xss, resulting in a StackOverflowError overflow exception. When the Java program starts a new thread, there is not enough space to allocate the Java stack for the thread. The size of a thread’s Java stack is determined by the -XSS setting. The JVM throws an OutOfMemoryError exception. The Java heap is used to store instances of objects. When it is necessary to allocate memory for instances of objects, and the heap occupation has reached the maximum set value (via -Xmx), OutOfMemoryError will be thrown. The method area is used to store information about Java classes, such as class names, access modifiers, constant pools, field descriptions, method descriptions, and so on. As the class loader loads the class file into memory, the JVM extracts the class information and places it in the method area.
when the class information needs to be stored and the method area has reached its maximum memory footprint (via -xx :MaxPermSize); An OutOfMemoryError exception will be thrown for tests in this case, the basic idea being that the runtime generates a large number of classes to fill the method area until it overflows. You need to manipulate the bytecode runtime directly with the help of CGLib to generate a large number of dynamic classes.


Read More: