Java virtual machine-java memory Area

Source: Internet
Author: User

A brief description of the memory area of Java:

    • Run-time data region
    • Hotspot Virtual Machine Object
I. Overview

second, run-time data area2.1 Program Counters

Program Counter Register, which represents the line number indicator of the byte code executed by the current thread, the PC in the operating system, both of which are a concept. Execution is done by changing this counter to select the next byte code instruction to execute. Java supports multi-threading, so each thread has its own PC, similar to an attribute in the operating system thread table.

2.2 java Virtual machine stack

Like a PC, it is thread-private, as is the thread's life cycle. The virtual machine stack describes the memory model that the Java method executes, and each method creates a stack frame when it executes, with information such as storing local variables, operands, dynamic links, method exits, and so on. Local variables are placed in a local variable table in the stack, which holds the basic data type (int) known by the compiler. Float,string. ) and the object's Reference (reference).

2.3 Local Method Stack

The Native method stack is very similar to the Java Virtual machine stack, but is used for the Native method service that is called to the virtual machine.

2.4 Java Heap

The Java heap is shared by all threads and is the memory of the Java program, which is created when the virtual machine is started and the purpose of the memory area is to hold the object instance. The Java heap is the primary area that the garbage collector (GC) manages.

2.5 Method Area

Method area is a zone shared by each thread that stores data such as class information, constants, static variables, instant compiler compiled code, etc. that have been loaded by the virtual machine.

A special area within the method area is called the runtime Constant Pool, which holds various literal and symbolic references (strings) generated at compile time

2.6 Direct Memory

Direct memory is not part of the virtual runtime, but it affects the size of the Java heap

third, the virtual machine object in Hotspot3.1 Creation of objects

Java is an object-oriented language, and object creation is done through the new keyword. Main steps:

    1. Go to the method area first to check if the class is loaded
    2. Load class to method area first without loading
    3. Allocates memory for an object (the space required after the class is loaded is known)
    4. allocating memory concurrency control (cloning the same piece of memory is assigned to multiple objects)
    5. Initializes the allocated memory space to 0
    6. Place the object's basic information (belonging to that class, object hash code, GC collar, etc.) in the object header
    7. Now that the new object has been created
    8. Call the Init method to assign a value to the object's properties according to the programmer's idea
3.2 Object Memory layout

Objects in memory are divided into three main pieces:

    1. Object header: The first part stores the data of the object itself, such as hash code, GC Delling, and so on, and the other part is the type pointer, which the virtual machine uses to determine that the object is an instance of that class.
    2. Instance data: Valid information that is actually stored by the object, the contents of the various fields defined in the program code, and the Instance defined in the parent class are also inherited.
    3. Align padding (Padding): There is no special meaning, the size of the object must be a 8-byte integer multiple.
3.3 Object access and positioning

The object is created to use the object, and the Java program needs to manipulate the object on the reference data stored on the stack, the reference type is a reference to the object, and does not define how the song refers to the way to locate and access the object in the heap, So the way the object is accessed depends on how the virtual machine is implemented, and there are two main kinds of mainstream:

1. Using a handle to access an object

  

The advantage of using a handle is that the handle is stable and only changes the instance data pointer in the handle when the object is moved (after the GC), and reference does not need to be modified.

2. Using Pointers to access objects

  

The biggest benefit of using direct pointers is faster, saving time spent on pointer positioning.

Java virtual machine-java memory Area

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.