Learn Java heap, Method Area, Java stack and local method Stack from several samples

Source: Internet
Author: User

1) Java heap

All object instances are allocated with memory on the Java stack. The heap size is adjusted by-Xmx and-Xms, as shown in the following sample:

[Java]
Public class HeapOOM {

Static class OOMObject {}
 
/**
* @ Param args
*/
Public static void main (String [] args ){
List <OOMObject> list = new ArrayList <OOMObject> ();

While (true ){
List. add (new OOMObject ());
}
}
 
}

With the JVM parameter-verbose: gc-Xms10M-Xmx10M-XX: + PrintGCDetails-XX: Export vorratio = 8-XX: + HeapDumpOnOutOfMemoryError, OOM will be reported soon:
Exception in thread "main" java. lang. OutOfMemoryError: Java heap space

Dump can be automatically generated.

2) Method Area

The method area stores information about VM loading classes, such as classes, static variables, and constants. The size is adjusted by-XX: PermSize and-XX: MaxPermSize, if there are too many classes, the permanent belt may be burst:

[Java]
Public class MethodAreaOOM {

Static class OOMOjbect {}
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
While (true ){
Enhancer eh = new Enhancer ();
Eh. setSuperclass (OOMOjbect. class );
Eh. setUseCache (false );
Eh. setCallback (new MethodInterceptor (){
 
@ Override
Public Object intercept (Object arg0, Method arg1,
Object [] arg2, MethodProxy arg3) throws Throwable {
// TODO Auto-generated method stub
Return arg3.invokeSuper (arg0, arg2 );
}

});
Eh. create ();
}
}
 
}

Add the permanent JVM parameter-XX: PermSize = 10 M-XX: MaxPermSize = 10 M. The following exception is reported after running:
Exception in thread "main" java. lang. OutOfMemoryError: PermGen space

Static variables or constants may also support the method zone:

[Java]
Public class ConstantOOM {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
List <String> list = new ArrayList <String> ();
Int I = 0;
While (true ){
List. add (String. valueOf (I ++). intern ());
}
}
 
}

Add the JVM parameter-XX: PermSize = 10 M-XX: MaxPermSize = 10 M. The following exception is reported after running:
Exception in thread "main" java. lang. OutOfMemoryError: PermGen space

3) Java stack and local method Stack

Stack stores information related to method execution such as local variable tables, operations, and method exits when a thread calls a method. The stack size is adjusted by Xss, if there are too many Method Invocation layers, this region will pop up. samples is shown as follows:

[Java]
Package com. cutesource;
 
Public class StackOOM {
 
/**
* @ Param args
*/

Private int stackLength = 1;

Public void stackLeak (){
StackLength ++;
StackLeak ();
}

Public static void main (String [] args) throws Throwable {
// TODO Auto-generated method stub
StackOOM oom = new StackOOM ();
Try {
Oom. stackLeak ();
} Catch (Throwable err ){
System. out. println ("Stack length:" + oom. stackLength );
Throw err;
}

}
 
}

Set the JVM parameter-Xss128k and report an exception:
Exception in thread "main" java. lang. StackOverflowError

Print the Stack length: 1007. here we can see that the 1007 K Stack capacity on my machine can carry a method call with a depth. Of course, it is rare to report such an error. Generally, only the infinite loop recursion occurs. In addition, too many threads will occupy the full stack area:

[Java]
Package com. cutesource;
 
Public class StackOOM {
 
/**
* @ Param args
*/

Private int stackLength = 1;

Private void dontStop (){
While (true ){
Try {Thread. sleep (1000);} catch (Exception err ){}
}
}

Public void stackLeakByThread (){
While (true ){
Thread t = new Thread (new Runnable (){
 
@ Override
Public void run (){
// TODO Auto-generated method stub
DontStop ();
}

});
T. start ();
StackLength ++;
}
}

Public static void main (String [] args) throws Throwable {
// TODO Auto-generated method stub
StackOOM oom = new StackOOM ();
Try {
Oom. stackLeakByThread ();
} Catch (Throwable err ){
System. out. println ("Stack length:" + oom. stackLength );
Throw err;
}

}
 
}

Exception: Exception in thread "main" java. lang. OutOfMemoryError: unable to create new native thread
However, you must be careful when running this example on windows. The system may be suspended and you may need to restart the machine.

Although the above examples are relatively simple, they can help common programmers Better Understand Java heap, Method Area, Java stack and local method stack.

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.