Java Virtual machine: JVM memory model

Source: Internet
Author: User

One, JVM memory model diagram

The JVM runtime area, in fact, refers to the partitioning and allocation of the JVM's memory space during the runtime of the JVM. Two images found on the web are shown below (individuals think that the second figure native Method stack should be drawn in the Java THEAD module):

Ii. Introduction of the data regions1. Stack Area

Stack is divided into Java Virtual machine stack and local method stack

    • The focus is on the Java Virtual machine stack, which is thread- private and has the same life cycle as the thread.
    • Each method execution creates a stack frame for storing local variable tables, operations stacks, dynamic links, method exits , and so on. Each method is called from until it finishes executing. Corresponds to the process of a stack frame from the stack to the stack in the virtual machine.
    • The usual stack refers to the local variable table section, which holds the 8 basic data types that are known during compilation, as well as object references and instruction addresses. The local variable table is allocated during compilation , and when a method is entered, the local variable allocated memory size in the stack is determined.
    • There are two kinds of abnormal stackoverflowerror and outofmemoneyerror. A stackoverflowerror error is thrown when the thread requests a stack depth greater than the allowed depth of the virtual machine, and the virtual machine stack dynamically expands, throwing outofmemoneyerror when the extension cannot request enough memory space.
    • The local method stack is the local method service (native) used by the virtual machine and is thread-private .
2. Heap Area
    • The heap is shared by all threads, created at the time the virtual machine starts, and the object instance is stored for the sole purpose .
    • The heap area is the main area of GC, usually divided into two blocks of young and old generations. The younger generation is also divided into the Eden area. The most new objects to be created, from survivor and to survivor the surviving objects after the GC is saved, each of which is 8:1:1 by default.
      However, many articles are divided into 3 blocks, the method area is counted as a permanent generation. This is probably based on the Hotspot virtual Machine division, and then for example IBM J9 there is no permanent generation introduction. Regardless of partitioning, the object instance is stored.
    • There will be abnormal outofmemoneyerror
3. Method Area
    • shared area by all threads for storing data such as class information, constants, static variables, and so on, which have been loaded by the virtual machine . is described by a Java virtual machine as a logical part of the heap. The habit is also called it permanent generation (permanment generation)
    • garbage collection rarely visits this area, but it also needs to be recycled, mainly for constant pool recycling, type unloading .
    • Constant pools are used to hold the various bytecode and symbol references generated during the compilation period , and the constant pool has a certain dynamic , which can hold the constants generated during the compilation period, and the constants during the run can also be added into the constant pool, such as the Intern of string. () method.
4. Program Counter
    • The line number indicator that the current thread is executing. By changing the value of the counter to determine the next instruction, such as Loop, branch, jump, exception handling, thread recovery, etc. are dependent counters to complete .
    • Java Virtual machine multithreading is achieved by rotating threads and allocating processor execution time. for thread switching to be able to recover to the correct location, each thread needs a separate program counter , so it is thread-private .
    • The only piece of Java Virtual machine does not stipulate any outofmemoryerror blocks
Three, data area summary

Name

Characteristics

Role

Configuration parameters

Abnormal

Program counter

Small footprint, thread-private, life cycle same as thread

Approximate byte-code line number indicator

No

No

Virtual Machine Stack

Thread private, with the same life cycle as the thread, using contiguous memory space

Java method Execution memory model, store local variable table, Operation Stack, dynamic link, method exit and other information

-xss

Stackoverflowerror

OutOfMemoryError

Java heap

Thread sharing, life cycle is the same as virtual machine, can not use contiguous memory address

Save the object instance, and all object instances (including arrays) are allocated on the heap

-xms

-xsx

-xmn

OutOfMemoryError

Method area

Thread sharing, life cycle is the same as virtual machine, can not use contiguous memory address

Stores data such as class information, constants, static variables, and instant compiler-compiled code that have been loaded by the virtual machine

-xx:permsize:16m

-xx:maxpermsize:64m

OutOfMemoryError

Run a constant-rate pool

Part of the method area, with dynamic

storing literal and symbolic references

Iv. extension: Direct memory

Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the Java VM specification, but this part of memory is also used frequently and can cause outofmemoryerror anomalies to occur. So we put it here to explain it together.

In JDK 1.4, a new NiO (Newinput/output) class was introduced, introducing a channel-and buffer-based I/O approach that can be used to directly allocate out-of-heap memory using the native library, and then through a stored Java The Directbytebuffer object inside the heap operates as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap.

V. Extension: Stack vs. stack

Java memory is often differentiated into heap memory (heap) and stack memory (stack), which is coarser, and the division of Java memory areas is actually far more complex. The popularity of this partitioning method only shows that most programmers are most concerned about the memory area that is most closely related to object memory allocation.

The heap is flexible, but not secure. For the object, we want to create, destroy dynamically, not to say after the creation of the object is not destroyed, the previously created objects can not be destroyed, so that our program is not moving, so Java with the heap to store objects. And once the objects in the heap are destroyed and we continue to refer to this object, there will be the famous nullpointerexception, which is the heap's disadvantage-the wrong reference logic will only be found at runtime.

The stack is inflexible, but very strict, safe and easy to manage. Because as long as the above reference is not destroyed, the following references must still be, in most programs, are defined variables, reference advanced stacks, after the definition of the backward stack, while the variables inside the block, reference in the block when the stack, the block at the end of the stack, understand this mechanism, We can easily understand the concept of the scope of various programming languages, and this is the advantage of the stack-the wrong reference logic can be found at compile time.

Stack--primarily holds references and basic data types.

Heap--Used to hold new object instances.

Reference: http://blog.csdn.net/u012152619/article/details/46968883

Java Virtual machine: JVM 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.