Learn Java heap, method area, Java stack, and local method stack from several sample

Source: Internet
Author: User
Tags stub throwable

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?
  1. Public class Heapoom {
  2. static class oomobject{}
  3. /**
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. list<oomobject> list = new arraylist<oomobject> ();
  8. While (true) {
  9. List.add (new Oomobject ());
  10. }
  11. }
  12. }
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?
  1. Public class Methodareaoom {
  2. static class oomojbect{}
  3. /**
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. //TODO auto-generated method stub
  8. While (true) {
  9. Enhancer eh = new enhancer ();
  10. Eh.setsuperclass (oomojbect.   Class);
  11. Eh.setusecache (false);
  12. Eh.setcallback (new Methodinterceptor () {
  13. @Override
  14. Public Object Intercept (object arg0, Method arg1,
  15. Object[] arg2, Methodproxy arg3) throws Throwable {
  16. //TODO auto-generated method stub
  17. return Arg3.invokesuper (arg0, arg2);
  18. }
  19. });
  20. Eh.create ();
  21. }
  22. }
  23. }
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?
  1. Public class Constantoom {
  2. /**
  3. * @param args
  4. */
  5. public static void Main (string[] args) {
  6. //TODO auto-generated method stub
  7. list<string> list = new arraylist<string> ();
  8. int i=0;
  9. While (true) {
  10. List.add (String.valueof (i++). Intern ());
  11. }
  12. }
  13. }
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?
  1. Package Com.cutesource;
  2. Public class Stackoom {
  3. /**
  4. * @param args
  5. */
  6. private int stacklength = 1;
  7. public void Stackleak () {
  8. stacklength++;
  9. Stackleak ();
  10. }
  11. public static void Main (string[] args) throws throwable{
  12. //TODO auto-generated method stub
  13. Stackoom oom = new Stackoom ();
  14. try{
  15. Oom.stackleak ();
  16. }catch (Throwable err) {
  17. System.out.println ("Stack length:" + oom.stacklength);
  18. throw err;
  19. }
  20. }
  21. }
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?
  1. Package Com.cutesource;
  2. Public class Stackoom {
  3. /**
  4. * @param args
  5. */
  6. private int stacklength = 1;
  7. private void Dontstop () {
  8. While (true) {
  9. Try{thread.sleep (1000);} catch (Exception err) {}
  10. }
  11. }
  12. public void Stackleakbythread () {
  13. While (true) {
  14. Thread t = new Thread (new Runnable () {
  15. @Override
  16. public Void Run () {
  17. //TODO auto-generated method stub
  18. Dontstop ();
  19. }
  20. });
  21. T.start ();
  22. stacklength++;
  23. }
  24. }
  25. public static void Main (string[] args) throws throwable{
  26. //TODO auto-generated method stub
  27. Stackoom oom = new Stackoom ();
  28. try{
  29. Oom.stackleakbythread ();
  30. }catch (Throwable err) {
  31. System.out.println ("Stack length:" + oom.stacklength);
  32. throw err;
  33. }
  34. }
  35. }
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

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.