Java stackoverflow error, javastackoverflow

Source: Internet
Author: User

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();                    }                    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.