Play the bad JVM very simple--tostring recursive call

Source: Internet
Author: User

A memory overflow situation is rare under the JVM's memory management mechanism. At least I met less, as if I had been in ssh several times when I published the project. Today saw a very simple way to make memory overflow (well, I seem to be dead--! ):

1  Public classinfiniterecursion {2      PublicString toString () {3         return"Infiniterecursion Address:" + This+ "\ n" ;4     }5      Public Static voidMain (string[] args) {6Infiniterecursion demo =Newinfiniterecursion ();7 System.out.println (demo.tostring ());8     }9}

The problem with the above code is the recursive invocation of the ToString method, and when the compiler finds a string with a plus sign followed by an object, it converts the object to a string by default, The method is to invoke the ToString method of the object (note: The Java object must have the ToString method), and then there is infinite recursion.

Exception in thread "main" Java.lang.StackOverflowError
At Java.lang.abstractstringbuilder.<init> (abstractstringbuilder.java:63)
At Java.lang.stringbuilder.<init> (stringbuilder.java:109)
At Test. Infiniterecursion.tostring (Infiniterecursion.java:6)
At Java.lang.String.valueOf (string.java:2854)
At Java.lang.StringBuilder.append (stringbuilder.java:128)
At Test. Infiniterecursion.tostring (Infiniterecursion.java:6)

.......

What do we have artifice play bad JVM, to share a bit ~ ~ ~

Play the bad JVM very simple--tostring recursive call

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.