Java Virtual machine memory model

Source: Internet
Author: User

In Java application development process, we often encounter OutOfMemoryError exception, do not know what is the reason, at this time we need to know the Java memory model, find some information, the Java memory model summarized; Java memory structure, which is the data area at runtime Java Virtual machine in the process of executing a Java program, it will be managed to divide the memory into several different data regions, which have their own purpose, create time to destroy time, the Java Runtime Data area can be broadly divided into the following areas:

1.PC Register/program counter (thread private)

A program counter is a small amount of memory space that can be seen as the line number indicator of the bytecode that is being executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, jumping, exception handling, and thread recovery need to rely on this counter to complete.

Because the multithreading of a Java Virtual machine is implemented through multi-threaded switching of the processor execution time, at any one time, a processor (a multi-core processor is a kernel) will only process the instructions in one thread, so in order to switch back to the correct execution location, Each thread needs to have a separate program counter, the counters between the various threads do not affect each other, isolated storage, we call this kind of memory area as "thread-private" memory, which is somewhat similar to "ThreadLocal", is thread-safe.

This memory area is the only area in the Java virtual machine that does not stipulate any outofmemoryerror exceptions.

2.Java stack (thread-private)

Like the program counter, the Java Virtual machine stack is also thread-private, and its life cycle is the same as the thread. The Java stack is always associated with threads, and whenever a thread is created, the JVM creates the corresponding Java stack for that thread, which in turn contains multiple stack frames (stack frame) that are associated with each method, creating a stack frame each time a method is run. Each stack frame contains information such as local variables, operation Stacks, and method return values. Whenever a method executes, the stack frame pops up the element of the stack frame as the return value of the method, and the stack frame at the top of the Java stack is the stack frame that is currently executing, i.e. the current executing method, and the PC register points to that address. Each execution is a method that corresponds to the "stack" and "stack" processes of a stack frame in the virtual machine stack.

  Since the Java stack is associated with threads, Java stack data is not shared by threads, so there is no need to care about its data consistency or the issue of synchronous locks.

In the Java Virtual Machine specification, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine can be dynamically extended, if the extension cannot request enough memory, The OutOfMemoryError exception is thrown. In a hot spot virtual machine, you can use the-XSS parameter to set the size of the stack. The size of the stack directly determines the reach depth of the function call.

3. Local method stack (thread private)

The role of the local method stack and the Java Virtual machine stack is very similar, except that the Java stack executes the Java method service for the JVM, and the local method stack executes the native method service for the JVM. The local method stack also throws Stackoverflowerror and OutOfMemoryError exceptions.

4.Java heap (thread sharing)

A heap is the largest piece of memory managed by the JVM, shared by all Java threads, not thread-safe, and created when the JVM is started. The heap is where Java objects are stored, as described in the Java Virtual Machine specification: All object instances and arrays are allocated on the heap. Java heap is the main area of GC management, from the perspective of memory recycling, since the GC basically uses a generational collection algorithm, so the Java heap can be subdivided into: the new generation and the old age; if there is no memory in the heap to complete the instance assignment, and the heap cannot be extended, Will run out of the OutOfMemoryError exception.

5. Method area (thread sharing)

The method area holds information about the class to be loaded (name, modifier, and so on), static constants in the class, constants defined in the class as final, field information in the class, method information in the class, This data is derived from the method area when the information is obtained by means of the class object's Getname.isinterface in the program. The method area is shared by the Java thread lock, unlike the rest of the Java heap, which is frequently recycled by GC, it stores information that is relatively stable, is GC-OutOfMemory when the method area is using more memory than it allows, and throws an error message. The method area is also part of the heap, which is what we typically call the persistent zone permanet Generation in the Java heap, which can be set by parameters and can be specified by-xx:permsize,-xx:maxpermsize specifying the maximum value.

constant Pool: The constant pool itself is a data structure in the method area. Constant pools are stored in constants such as strings, final variable values, class names, and method names. The Chang is determined during compilation and is saved in the compiled. class file. Generally divided into two categories: literal and application volume. Literals are strings, final variables, and so on. The class name and method name belong to the reference amount. The most common reference amount is when invoking a method, a reference to the method is found based on the method name, and the function code is executed in the body. The reference amount consists of the name of the class and interface, the name and descriptor of the field, and the names and descriptors of the method.

Java Virtual machine memory model

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.