In-depth JVM OOM

Source: Internet
Author: User
Tags jconsole

I saw a lot of OOM articles and tested them myself. The reason why oom is written in each program. You can start to analyze the Java memory model.

It is estimated that it is not too difficult. It is uncomfortable to use jconsole to monitor resources.


Package COM. oom; import Java. util. arraylist; import Java. util. list;/put the instance of the ** heap OOM * class into heap, therefore, you can test heap OOM by constantly creating classes. * jconsole can monitor resources. ** exception in thread "Main" Java. lang. outofmemoryerror: Java heap space */public class heapoom {static class testheepobject {} public static void main (string [] ARGs) {list <testheepobject> heaplist = new arraylist <testheepobject> (); While (true) {heaplist. add (New testheepobject ());}}}


Package COM. oom; import COM. oom. heapoom. testheepobject;/** stack outof erro ** the infinite recursion method can be used to test * each main method starts a stack, then the recursive method enters the stack (the return point of some methods for storing stack information) until the stack is full and SOF exception occurs ** exception in thread "Main" Java. lang. stackoverflowerror */public class vmsof {public static void main (string [] ARGs) {New vmsof (). testsof ();} private void testsof () {testsof ();}}

Package COM. oom; import Java. util. arraylist;/*** constantly increasing constants to the constant pool causes OOM exceptions * exception in thread "Main" Java. lang. outofmemoryerror: permgen space */public class runtimeconstantpooloom {public static void main (string [] ARGs) {/* Al references to the constant pool to ensure that it is not recycled by GC. This is because some commercial JVMs are fully GC when implementing permanent regions. * Constants are permanent partitions stored in the heap. */Arraylist <string> Al = new arraylist <string> (); int I = 0; while (true) {Al. add (string. valueof (I ++ ). intern (); // intern migrates the string to the constant pool }}}
Package com. OOM;/** the creation thread causes OOM. If your machine is strong enough, I did not say it ~~~ * I restarted ~~ **/Public class createtheadoom {Private Static void loop () {While (true) {}} private void runthead () {While (true) {New thread (New runnable () {public void run () {loop ();}}). start () ;}} public static void main (string [] ARGs) {New createtheadoom (). runthead ();}}


OOM and cglib, which use Java Direct Memory, cause the OOM to load classes in the method area.


In-depth JVM OOM

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.