Recently looking at "in-depth understanding of Java Virtual Machine", the book gives a few examples, better explain the process of several oom (OutOfMemory), most programmers do not pay too much attention to the Java Runtime data area structure when writing programs:
It feels necessary to deepen our understanding of these areas through a few real examples.
1) Java heap
The instance assignments for all objects are allocated memory on the Java heap, and the heap size is adjusted by-xmx and-XMS, as shown in sample:
[Java]View Plaincopyprint?
- 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 ());
- }
- }
- }
public class Heapoom {static Class oomobject{}/** * @param args */public static void main (string[] args) {List<oomobjec t> list = new arraylist<oomobject> (), while (true) {List.add (New Oomobject ());}}}
Plus the JVM parameter-verbose:gc-xms10m-xmx10m-xx:+printgcdetails-xx:survivorratio=8-xx:+heapdumponoutofmemoryerror, will be able to quote the Oom soon:
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
and can automatically generate dump.
2) Method Area
The method area 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 burst the permanent band:
[Java]View Plaincopyprint?
- 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 ();
- }
- }
- }
public class Methodareaoom {static Class oomojbect{}/** * @param args */public static void main (string[] args) {//TODO Au To-generated method Stubwhile (true) {enhancer eh = new enhancer (); Eh.setsuperclass (Oomojbect.class); Eh.setusecache ( FALSE); Eh.setcallback (new Methodinterceptor () {@Overridepublic object Intercept (object arg0, Method arg1,object[] Arg2 , Methodproxy Arg3) throws Throwable {//TODO auto-generated method Stubreturn arg3.invokesuper (arg0, arg2);}}); Eh.create ();}}}
With the JVM parameters of the permanent band:-xx:permsize=10m-xx:maxpermsize=10m, the following exception is reported after running:
Exception in thread "main" Java.lang.OutOfMemoryError:PermGen space
Static variables or constants also have the potential to explode the method area:
[Java]View Plaincopyprint?
- 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 ());
- }
- }
- }
public class Constantoom {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubli st<string> list = new arraylist<string> (), int i=0;while (TRUE) {List.add (string.valueof (i++). Intern ());}}}
Also add the JVM parameter:-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
Stack is to store the thread call method when storing local variables table, operation, method exit and other information related to the execution of the method, the stack size is adjusted by XSS, the method call hierarchy too much will burst this area, samples as follows:
[Java]View Plaincopyprint?
- 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;
- }
- }
- }
Package Com.cutesource;public class Stackoom {/** * @param args */private int stacklength = 1;public void Stackleak () {Stac Klength++;stackleak ();} public static void Main (string[] args) throws throwable{//TODO auto-generated method Stubstackoom oom = new Stackoom (); TR Y{oom.stackleak ();} catch (Throwable err) {System.out.println ("Stack Length:" + oom.stacklength); throw err;}}}
Set JVM parameters:-xss128k, Report exception:
Exception in thread "main" Java.lang.StackOverflowError
Print out the stack length:1007, as you can see, the 128k stack capacity on my machine can carry a method call with a depth of 1007. Of course, this kind of error is seldom seen, usually only in the infinite loop recursion, in addition, too many threads will occupy the stack area:
[Java]View Plaincopyprint?
- 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;
- }
- }
- }
Package Com.cutesource;public class Stackoom {/** * @param args */private int stacklength = 1;private void Dontstop () {Whil E (True) {try{thread.sleep (1000);} catch (Exception err) {}}}public void Stackleakbythread () {while (true) {thread t = new Thread (new Runnable () {@ overridepublic void Run () {//TODO auto-generated method Stubdontstop ();}); T.start (); stacklength++;}} public static void Main (string[] args) throws throwable{//TODO auto-generated method Stubstackoom oom = new Stackoom (); TR Y{oom.stackleakbythread ();} catch (Throwable err) {System.out.println ("Stack Length:" + oom.stacklength); throw err;}}}
Report exception: Exception in thread "main" java.lang.OutOfMemoryError:unable to create new native thread
However, to run this example on Windows, be careful, there will be a system of suspended animation, it may be necessary to restart the machine.
The above few examples are simple, but they can help the average programmer to understand the Java heap , the method area , theJava stack and the local method stack more intuitively.
Learn Java heap, method area, Java stack, and local method stack from several sample