Java Virtual machine memory model

Source: Internet
Author: User

1) Program counter: Thread Private

When the number of threads exceeds the number of CPUs, the threads of a thread are polled to snatch CPU resources, and for a single-core CPU, only one thread is running at a time, and the other threads must be switched out. Therefore, each thread has a separate program counter that records the next instruction to be run, and the counters between the threads do not affect each other, which is the private memory space of a thread. When a thread is executing a Java method, the program counter records the Java bytecode address being executed, and if the native method is executed, the counter is empty
2) Java Virtual machine stack: Thread private

Created with the Java thread at the same time to hold local variables of the method, partial results, and participate in the invocation and return of the method. The Java Virtual machine defines two types of stack-space-related exceptions: Stackoverflowerror,outofmemoryerror. Throws Stackoverflowerror when the requested stack depth is greater than the maximum available stack depth. If the stack can be dynamically extended, there is not enough memory space in the process of the expansion stack to support the expansion of the stack (system memory is low). ), the OutOfMemoryError is thrown. The following is an instance of a thrown java.lang.StackOverflowError

public class teststack{    private static int count=0;    public static void recursion (long A,long B,long c) {        long d=0;        Long e=0;        Long f=0;        count++;        Recursion (a,b,c);    }    public static void Main (String []args) {        try{            recursion (1l,2l,3l);        } catch (Throwable e) {            System.out.println ("Count:" +count);            System.out.print (e);        }    }    public void Recursion1 (long a,long b,long c) {        long d=0;        Long e=0;        Long f=0;        count++;        Recursion1 (a,b,c);    }} /* Direct Run output:    count:4927 use-XSS to enlarge stack space, run java-xss1m teststack output:    count:27590*/

The virtual machine stack uses a data structure called a stack frame to hold the context in the stack frame, storing the local variable table of the method, the operand stack, the dynamic link method, and the return address. Each method call is accompanied by the stack frame of the stack operation, the return of the method represents the stack frame of the stack operation. Method invocation, the more parameters and local variables of the method, the larger the local variable table, and the stack frame becomes larger to satisfy the information required by the method call. Therefore, the size of the stack space required for a single method call is also more

Conclusion: Using-XSS to enlarge stack space and increase the size of stack space, the function call depth increases. The more local variables of the function, the larger the stack frame, the higher the requirement of the single function call to the stack space, and the less the number of nested calls.

The space of the

Local variable table is reusable, and for the capacity of the largest local variable table allocated by a method, you can use the Jclasslib tool to view the

public class test1{public static void Main (String []args) {test3 ();        } public static void Test1 () {{byte[] b=new byte[6*1024*1024];        }//At this time B is still in the stack frame of the variable table,//GC root can refer to the memory block, and not enough local variables to take the memory block,//so will not be recycled [1] System.GC ();            System.out.println ("First explict GC over");            } public static void Test2 () {{byte[] b=new byte[6*1024*1024];        b=null;//Help system GC}//Is recycled [2] System.GC ();        System.out.println ("First explict GC over");        } public static void Test3 () {{byte[] b=new byte[6*1024*1024];        } int a=0;//takes B's memory block, GC Root cannot find B, so it is recycled [3] System.GC ();        System.out.println ("First explict GC over"); }}/*JAVA-VERBOSE:GC TEST1--[1][GC 224k->134k (5056K), 0.0027563 secs][full GC 134k->134k (5056K), 0.0177525 secs][ Full GC 6278k->6278k (11204K), 0.0151799 secs]first explict GC over--[2][gc 224k->134k (5056K), 0.0027134 secs][full GC 134k->134k (5056K), 0.0178466 secs][full GC 6278k->134k (11204K), 0.0152811 Secs]first explict GC OVER--[3][GC 224k->134k (5056K), 0.0027211 secs][full GC 134k->134k (5056K), 0.0174787 secs][full GC 6278K-> 134K (11204K), 0.0147169 Secs]first explict GC over*/

3) Local method stack:
Similar to the Java Virtual machine stack functionality, the Java Virtual machine stack is used to manage Java function calls, while the local method stack is used to manage calls to local methods with C implementations. The local method stack and Java Virtual machine stack are not distinguished in the hot spot virtual machine, so the Stackoverflowerror,outofmemoryerror exception is also thrown

4) Java heap: The most important part of Java Runtime memory
Allocates all objects and arrays at run time. Divided into the Cenozoic (eden,survivor space0[s0,from space],survivor space1[s1,to space]) and the old age.

5) Method Area: Thread sharing
The method area holds the type information of the class, the constant pool, the domain information, the method information, and so on most of the information from the class file. In hot spot virtual machines, the method area is also called the permanent Zone, which is a separate memory space from the Java heap and can also be recycled by GC, except that it behaves differently from the Java heap.

Exception in thread "main" Java.lang.OutOfMemoryError:PermGen Spacepublic class permgengc{public static void main (STR        ing []args] {list<string> list=new arraylist<string> ();        int i; for (i=0;i<integer.max_value;i++) {//If the current string exists in the constant pool, the object in the pool is returned, and if it does not exist, the string is first joined to the constant pool and then returned to the Pool object reference list . Add (string.valueof (i). Intern ());//join to constant pool}}}/*java-xx:permsize=2m-xx:maxpermsize=4m permgengcexception in th Read "main" Java.lang.OutOfMemoryError:PermGen space at Java.lang.String.intern (Native Method) at Permgengc.main (Pe Rmgengc.java:7) java-xx:permsize=2m-xx:maxpermsize=4m-xx:+printgcdetails PERMGENGC[GC [defnew:896k->64k (960K), 0.0043064 secs] 896k->180k (5056K), 0.0050202 secs] [times:user=0.00 sys=0.00, real=0.01 SECS][GC [defnew:919k->0k (960K), 0.0031302 secs] 1035k->236k (5056K), 0.0039759 secs] [times:user=0.02 sys=0.00, real=0.00 SECS][GC [defnew:778 k->0k (960K), 0.0029668 secs] 1015k->390k (5056K), 0.0036493 secs] [times:user=0.00 sys=0.00, real=0.00 secs][gc [defnew:896k->0k (960K), 0.0030697 secs] 1286k->619k ( 5056K), 0.0037499 secs] [times:user=0.00 sys=0.00, real=0.00 secs][gc [defnew:896k->0k (960K), 0.0033067 secs] 1515K-& gt;964k (5056K), 0.0040117 secs] [times:user=0.00 sys=0.00, real=0.01 secs][full GC [tenured:964k->581k (4096K), 0.059 5687 secs] 1665k->581k (5056K), [perm:4095k->4095k (4096K)], 0.0610942 secs] [times:user=0.06 sys=0.00, real=0.06 s Ecs][full GC [tenured:581k->581k (4096K), 0.0591349 secs] 581k->581k (5056K), [perm:4095k->4095k (4096K)], 0.0636965 secs] [times:user=0.06 sys=0.01, real=0.06 secs]exception in thread ' main ' [Full GC [tenured:581k->236k (409 6K), 0.0289138 secs] 596k->236k (5056K), [perm:4095k->360k (4096K)], 0.0332241 secs] [times:user=0.03 sys=0.00, Rea l=0.03 Secs]java.lang.outofmemoryerror:permgen space at Java.lang.String.intern (Native Method) at Permgengc.main (Pe RMGENGC.JAVA:8) */

Java Virtual machine memory model

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.