Java run-time data region

Source: Internet
Author: User

run-time data regionThe Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program. These zones have their own purpose, as well as the creation and destruction of time, and some regions exist as virtual machine processes start, and some are built and destroyed depending on the start and end of the user thread. 1.1 Program Countersis a small piece of memory space. Function: The line number indicator that is used to flag the byte code executed by the current thread (that is, the number is added to the bytecode). In the JVM, the bytecode interpreter works by changing the value on this counter to select the next byte-code instruction to execute. Functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on counters to complete. In multi-threading, the counters between threads do not affect each other and are stored independently. there is no area for outofmemoryerror conditions. 1.2 Java Virtual machine stackthe Java Virtual machine stack is also thread-private, with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each method is executed to create a stack frame for storing local variable tables, operand stacks, dynamic connections, method exits, and so on, and each method is called until the completion process corresponds to a stack frame in the virtual machine stack from the stack to the stack. understand what a stack frame is: a data structure that is used to support virtual machines for method invocation and method execution. Stack frame is based on the virtual machine stack, a virtual machine stack stored a number of stack frames, each stack frame records the Java method of the local scalar table, the operand stack and other information also records the method from the stack to the stack of the process. Storage structureWhen compiling program code, how much local variable table is needed in the stack frame, how deep the operand stack has been fully determined, and written to the Code property of the method table, so how much memory is allocated to a stack frame and not affected by the program runtime variable data, but only by the specific virtual machine implementation. local Variable table : is a set of variables worth storage space for method parameters, local variables, and object reference types. After compiling the program code, determine the maximum capacity required for local variables. A variable slot is the smallest unit in a local variable table. One Solt can store (x32) Boolean, Byte, char, short, int, float, reference, and ReturnAddress (addresses that execute bytecode directives) For a 64-bit long and double variable, you need to allocate two contiguous slot spaces. In order to save the stack space slots can be reused. However, it can sometimes affect garbage collection behavior. Figure two from figure I and figure two can be seen in figure two, the variable slot can be reused, variable a multiplexed placeholder slot, resulting in the Placheholder reference is deleted, memory is recycled. operand stack : often referred to as the Operation Stack is a post-in first out stack. Its maximum stack depth is also determined during compilation and saved in the code attribute of the class file. Scenario: When a method is passed a parameter, it is carried out by the operand stack. In the conceptual model, the two stack frames as the virtual machine stack of the yuan, are completely independent of each other, but most of the virtual machine implementation will do some optimization processing, so that two stack frames appear to overlap a part. Let the partial operation of the next stack frame overlap with the partial local variables table of the stack frame above, so that a partial data can be shared when the method call is returned, without the need for additional parameter copy passing. Dynamic connections: Each stack frame contains a reference to the method that the stack frame belongs to in the run-time pool, which is held to support dynamic connections during method invocation. A large number of symbolic references are stored in the class file, and the method call execution in the bytecode is the parameter that points to the method's symbolic reference in the constant pool. Some of these symbolic references are converted to direct references during the class load phase or the first use, which is called static parsing. The other part will be converted to a direct reference during each run, which is called a dynamic connection. method Exit: The method exits are divided into two kinds, one receives the return command and exits normally; the second execution encounters an exception that causes the method to exit.    There are two kinds of exceptions to the virtual machine stack: Stackoverflowerror and OutOfMemoryError1.3 Local Method StackThe local method stack is very similar to the virtual machine stack, but the difference is that the virtual machine stack is a Java method (bytecode) service, and the local method stack is the native method service used by the virtual machine. Native method: does not necessarily refer to Java in the native method (such as String.intern (): Native method), but also other libraries or other language methods such as C, C + +. There are two exceptions to the local method stack: Stackoverflowerror and OutOfMemoryError1.4 Java HeapThe Java heap is the largest piece of memory managed by a Java virtual machine. is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this memory area is to hold object instances. almost all object instances allocate memory here, but with a subtle change in the JIT compiler's transfer. The Java heap is the primary area of garbage collector management. In Java, the heap is divided into two different areas: the New Generation (young) and the older generation (old). The Cenozoic is divided into three regions: Eden Space, from Survivor space, to survivor space. The purpose of this partitioning is to better manage the objects in memory, including the allocation of memory and recycling. Heap memory model (jdk1.6)you can see: heap size = new Generation + old age. The heap size can be set by parameter-xms,-xmx. Java Heap will have OutOfMemoryError exception1.5 Method AreaThe method area, like the Java heap, is the area of memory shared by each thread, but the content is stored differently. The method area is used to store data such as class information, constants, static variables, and immediately compiled code that have been loaded by the virtual machine. Alias Non-heap. In addition, the method area is not contiguous, so the garbage collection behavior is rarely seen in this area. The method area also includes a section: a constant pool. Chang is a variety of literal and symbolic references that are used to store compiler-generated content that is stored in the run-time pool of the method area after the class is loaded. The size can be set by-xx:permsize and-xx:maxpermsize. The method area will have a OutOfMemoryError exceptionSummary:1. Separate the contents of the Java stack, Java heap, and method area:Public class demo{private String name;Public Demo (String name) {this.name = name;}Public static void Main (string[] args) {Demo d = new Demo ("Test");}}Java Heap method area Java stackNew Demo () Demo class information demo d, local variable namethe method name in the demo calls the main method main thread call stack

Java run-time data region

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.