For more information, see writing Java programs to crash the Jvm.

Source: Internet
Author: User

If you have encountered a problem, I would like to share it with you and hope you can get a comprehensive answer.

Today, I saw an author asking a question: "How to Write Java code to cause Jvm to crash". After reading it, I don't understand it at all. After checking the question, Baidu knows that there is such an answer:

                                      Object[] o =            (             o =                              }

After running the program for more than 10 seconds, the console will see this error:

Exception in thread "main"10)

Obviously, an error occurred while exceeding the memory size.

I randomly changed the original program, such as assigning an initial value, without affecting the program.

 

In my opinion, the original program can crash the Jvm because new objects are constantly created through the old objects in an endless loop, that is, the created objects are referenced by each other, therefore, GC will not recycle them, resulting in stack overflow.

Taking this example as an example, I wrote a simple class that imitates the array in the example program, as shown below:

          JvmBean bean =  JvmBean(                   .bean =  }

Then perform a simple test as follows:

                     JvmBean j =          (             j =                              }

The result is the following error output on the console:

Exception in thread "main"<init>(JvmBean.java:5<init>(JvmBean.java:5<init>(JvmBean.java:5<init>(JvmBean.java:5<init>(JvmBean.java:5)

A long string of "at jvm. JvmBean. <init> (JvmBean. java: 5)" is omitted.

 

 

Let's take a look at the entire process.

Exception in thread "main" java. lang. OutOfMemoryError: Java heap space at jvm. Crash. main (Crash. java: 10)The exception is thrown when the program cannot apply for sufficient memory. The Object array o constantly points to the new Object array, and the array element is the original Object array, which increases the Object dimension. Continuously applying for memory space will eventually exceed the maximum heap size in the jvm. Heap memory overflow. If the output is printed, the VM does not crash, but the crash time is greatly extended.

The second exception, yahokuma, has already been quite clear. I will move it here-"static attributes inside the class> static blocks> Object Attributes> constructor. Note that the bean attribute is initialized before the JvmBean constructor. In your main function, before a new JvmBean constructor, The JvmBean objects in the class should be initialized first, the attribute bean inside this class also contains a JvmBean object that needs to be initialized and called cyclically, resulting in stack overflow." This is the Exception -- Exception in thread "main" java. lang. StackOverflowError.

I will change the original JvmBean.

          JvmBean bean =                    .bean =  }

The final result is the same as that in the first example.

 

Exception in thread "main" java. lang. OutOfMemoryError: Java heap spaceThis error occurs. To make them have insufficient stack space, the simplest thing is to keep applying for new memory space in methods, such as constructor. For example, I would like to demonstrate the second example of an error.

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.