Java thousand ask _07JVM architecture (013) _java What happens when memory overflows

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1, Java heap what situation will overflow

All instances of the object are allocated memory on the Java heap, the heap size is adjusted by-xmx and-XMS, and the Java heap space is overrun if the program uses more memory than the heap maximum memory (-XMX).

Learn about the Java memory model look here: What is the Java memory model
Understanding heap Memory Look here: What is the Java heap memory?
Understanding Java garbage Collection mechanism look here: What is the Java garbage collection mechanism

Instance:

publicclass HeapOOM {     static class OOMObject{}     /**     * @param args     */     publicstaticvoidmain(String[] args) {         new ArrayList<OOMObject>();         while(true){             list.add(new OOMObject());         }     

Add the JVM parameters to run:

-verbose:gc -Xms10M -Xmx10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError

Where-VERBOSE:GC is to print the details of the GC process, the above program can quickly report a memory overflow (OOM):
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space

and can automatically generate dump files, dump records the full information of the process memory.

See the dump file here: What is a dump file

2. What happens in the Java method area

The method area (permanent generation) is the information that holds the virtual machine load class (such as classes, static variables, and constants), the size is adjusted by-xx:permsize and-xx:maxpermsize, and too many classes have the potential to cause permanent generation overflow permgen space.

Instance:

 Public  class methodareaoom {     StaticClass oomojbect{}/** * @param args * *      Public Static void Main(string[] args) {//TODO auto-generated method stub         while(true) {Enhancer eh =NewEnhancer ();             Eh.setsuperclass (Oomojbect.class); Eh.setusecache (false); Eh.setcallback (NewMethodinterceptor () {@Override                  PublicObjectIntercept(Object arg0, Method arg1, object[] arg2, Methodproxy arg3)throwsThrowable {//TODO auto-generated method stub                    returnArg3.invokesuper (arg0, arg2);             }             });         Eh.create (); }     } }

After adding the persistent JVM parameters:

-XX:PermSize=10M -XX:MaxPermSize=10M

After running, the following exception is reported:
Exception in thread "main" Java.lang.OutOfMemoryError:PermGen space

Static variables or constants also have the potential to explode the method area:

publicclass ConstantOOM {     /**     * @param args     */     publicstaticvoidmain(String[] args) {         // TODO Auto-generated method stub         new ArrayList<String>();         int i=0;         while(true){             list.add(String.valueOf(i++).intern());         }     

Also add the JVM parameters:

-XX:PermSize=10M -XX:MaxPermSize=10M

The following exception is reported after running:
Exception in thread "main" Java.lang.OutOfMemoryError:PermGen space

3. Java stack and local method stack what's going to overflow

Stack is to store the thread call method when storing local variables, operations, method exits and other information related to the execution of the method, the stack size is adjusted by-XSS, the method call hierarchy too much will make the stack overflow stackoverflowerror.

Instance:

 PackageCom.cutesource; Public  class stackoom {     /** * @param args * *     Private intStacklength =1; Public void Stackleak() {stacklength++;     Stackleak (); } Public Static void Main(string[] args)throwsthrowable{//TODO auto-generated method stubStackoom Oom =NewStackoom ();Try{Oom.stackleak (); }Catch(Throwable err) {System.out.println ("Stack Length:"+ oom.stacklength);ThrowErr }     } }

Set JVM parameters:

-Xss128k

To run a report exception:
Exception in thread "main" Java.lang.StackOverflowError

Print out the stack length:1007, as you can see, the 128k stack capacity can carry a method call with a depth of 1007. Of course, this error is rarely seen, usually only in the infinite loop recursion, in addition, too many threads will occupy the stack area (the thread is allocated its own stack, but all threads in the process can use the total size of the stack is certain):

 PackageCom.cutesource; Public  class stackoom {     /** * @param args * *     Private intStacklength =1;Private void Dontstop(){ while(true){Try{Thread.Sleep ( +);}Catch(Exception Err) {}         }     } Public void Stackleakbythread(){ while(true) {Thread T =NewThread (NewRunnable () {@Override                  Public void Run() {//TODO auto-generated method stubDontstop ();             }             });             T.start ();         stacklength++; }     } Public Static void Main(string[] args)throwsthrowable{//TODO auto-generated method stubStackoom Oom =NewStackoom ();Try{Oom.stackleakbythread (); }Catch(Throwable err) {System.out.println ("Stack Length:"+ oom.stacklength);ThrowErr }     } }

To run a report exception:
Exception in thread "main" java.lang.OutOfMemoryError:unable to create new native thread

Running this example on Windows is very careful, there may be a situation in which the system is suspended from death and the machine needs to be restarted.

Java thousand ask _07JVM architecture (013) _java What happens when memory overflows

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.