Java stackoverflow error, javastackoverflow
This article will record the program that tries to generate stackoverflow
1-Xss = 1 k, set the stack size to 1024 bytes, and generate 515 long records.
2 nested call
3. Create a large number of threads
1-Xss = 1 k, set the stack size to 1024 bytes, and generate 515 long records.
Conclusion: Impossible
1. The stack in Java is dynamically scalable, not fixed. Therefore, it cannot be implemented.
2. Java objects are referenced and no space is allocated on the stack. Attention should be paid to this person from C ++.
3. In some operating systems, the assigned stacksize is fixed and is easy to support the stack.
1 public class MyMain {2 3 public static void main (String [] args) {4 5 System. out. println ("stack overflow"); 6 7 // MyMain main; this is a variable declaration without allocating memory space. 8 // long aa [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; note that although this is an array of basic variables, but it is also in heap 9 // String str = "hello world"; this is to allocate 10 11 long a0 = 0 in the method/constant area; 12 long a1 = 0; 13 long a2 = 0; 14 ...... 525
15 long a514 = 0; 526 527} 528 529}
2 nested call
Conclusion: The stack can be cracked.
Nested calling of functions is in-depth. If the ending condition is not properly set, it is easy to stack overflow
public class MyMain { private static void foo(){ foo(); } public static void main(String[] args) { System.out.println("stack overflow"); foo(); }}
3. Create a large number of threads
Conclusion: I have not observed it on my machine.
Set-Xss = 100 M. Only the speed of the memory is increased by M. However, when the speed is increased to 7.9G, the system does not move. No OutOfMemoryError occurs. The reason is unknown.
OutOfMemoryError can be observed in the book "deep understanding of Java Virtual Machine.
public class MyMain { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("stack overflow"); while(true){ Thread th = new Thread(new Runnable(){ @Override public void run() { while(true){ System.out.println("do not stop"); } } }); th.start(); } }}