About writing Java programs to crash the JVM

Source: Internet
Author: User

Today, in the book, I read a question from the author, "How to get the JVM to crash by writing Java code," which I don't understand. With a question to check, Baidu know there is such an answer:

1 package JVM;

2

3 public class Crash {

4 public static void main (string[] args) {

5

6//object[] o = {"abc"}; The initial value assignment is not affected.

7 object[] o = null;

8

9 while (true) {

Ten o = new object[] {o};

11//output, the JVM will not crash.

//system.out.println (o);

13}

14}

15}

After the program has been running for more than 10 seconds, the console will have this error:

Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space

At the JVM. Crash.main (CRASH.JAVA:10)

Obviously, the memory space error is exceeded.

I changed the original program at will, such as the initial value, and so on, no impact on the program.

But I will die in the loop o output in the console, the JVM has never blown, why output, it will not exceed the memory space?

In my opinion, the original program can cause the JVM to crash because, in the dead loop, new objects are constantly created through the old objects, that is, the created objects are referenced to each other, so the GC does not reclaim them, resulting in a stack overflow.

Modeled after this example, I wrote a simple class that mimics an array of examples in the program, as follows:

1 package JVM;

2

3 public class Jvmbean {

4

5 Jvmbean bean = new Jvmbean (this);

6

7 Public Jvmbean (Jvmbean Bean) {

8 This.bean = bean;

9}

10}

Then the simple test is as follows:

1 package JVM;

2

3 public class Mycrash {

4

5 public static void main (string[] args) {

6 Jvmbean j = null;

7 while (true) {

8 J = new Jvmbean (j);

9//The JVM will crash regardless of output output

Ten//system.out.println (j);

11}

12}

13}

The result is that the console outputs the following error:

Exception in thread "main" Java.lang.StackOverflowError

At the JVM. Jvmbean.<init> (Jvmbean.java:5)

At the JVM. Jvmbean.<init> (Jvmbean.java:5)

At the JVM. Jvmbean.<init> (Jvmbean.java:5)

At the JVM. Jvmbean.<init> (Jvmbean.java:5)

At the JVM. Jvmbean.<init> (Jvmbean.java:5)

A long list of "at JVMs." Jvmbean.<init> (Jvmbean.java:5) ", the back was omitted by me.

As a result, the JVM crashes as well, but the error type is different from the example program, which says stack overflow error, and whether or not the output is the same, the error occurs, why?

Two questions are out of the question because of the enthusiastic advice of the two-bit man who commented on it!

The whole process is over here.

The first anomaly combined with the day Tim Man said, Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space at JVM. Crash.main (CRASH.JAVA:10) is the exception that is thrown when the program cannot request enough memory, the object array o constantly points to the new object array, which is the original object array, which makes the object dimension more and more high. Constantly applying for memory space, resulting in exceeding the maximum heap size in the JVM. Heap memory overflow. Why does the output print and the time be prolonged? Yahokuma man A word awakened the dream! If the output is printed, the virtual machine does not crash, but the time for the crash is greatly prolonged. And the crash time extension is actually false, because the output is an IO event, each output CPU is interrupted, IO is time-consuming, so, the feeling will be longer.

The second exception, Yahokuma. It's clear in the comments below that I moved here-"Static properties > Static Blocks > Object Properties > Construction methods inside the class. Note that this means that the bean attribute is initialized before the Jvmbean constructor. In your main function, before the new one Jvmbean constructor, the Jvmbean object inside the class is initialized first, and the inside of the property bean inside the class also contains a Jvmbean object that needs to be initialized, called as a loop, resulting in a stack overflow. "So the exception would be this--exception in thread" main "Java.lang.StackOverflowError

I'll change the original Jvmbean.

1 package JVM;

2

3 public class Jvmbean {

4

5 Jvmbean bean = null;

6

7 Public Jvmbean (Jvmbean Bean) {

8 This.bean = bean;

9}

10}

The result will be the same as the first example.

How do you crash the JVM? If you want to make it out of heap memory space, resulting in a typical memory leak, you can create objects that keep them in deep references. Generates an error such as exception in thread "main" Java.lang.OutOfMemoryError:Java heap space. If you want to make their stack space is not enough, the simplest, is in the method, such as the construction method to constantly apply for new memory space is enough, such as my second example of the error demonstration.

About writing Java programs to crash the JVM

Related Article

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.