Java Interview Preparation JVM detailed study One (Java memory area)

Source: Internet
Author: User

New Object ();

Main reference: "In-depth understanding of Java Virtual Machine", without the consent of the author and the original book to prohibit reprint.

Data regions in the JVM

The data in the JVM is divided into the following areas:

which

1. Program Counter:

A program counter is a small amount of memory space that is the line number indicator of the bytecode that is executed by the current thread . In other words, after the program is translated into bytecode, the functions such as branching, looping, jumping, exception handling, and thread recovery all need program counters to complete.

Note This area stores something that is the address of the bytecode directive that executes the virtual machine, and if the Natvie method (Natvie: A keyword that calls code other than the Java language), the store is empty.

(special) This area is the only one in the JVM that does not specify any OutOfMemoryError(memory overflow error)

2.Java Virtual Machine Stack

This area describes the memory model of the method execution, where each method creates a stack frame to store information such as the local variable table, the action stack, the dynamic Link method exit, and so on.

    the process of each method invocation is a stack frame in the virtual machine stack into the stack and the process of the stack.

Local Variables Table : Store the compiler to know the various basic data types (eight kinds: boolean\byte\char\short\int\long\float\double), The object reference type and the ReturnAddress type (indicates the address of a bytecode directive).

There are two exceptions in this area: Stackoverflowerror exception (triggered when the stack depth is greater than the depth allowed by the virtual machine)

OutOfMemoryError exception (this of course, except the program counter area will have)

3. Local Method Stack

Note Name: Native method Stack, as in Java keyword native is the same, meaning that this area is similar to the virtual machine stack, but it is for the local law (that is, native modified parts) to work .

Exceptions are thrown in the same way as a virtual machine station.

4.Java Heap

Although the figure looks small, it is the largest piece of memory managed by the JVM, shared by all threads;

The only function of this area is to store object instances, and almost all object instances are allocated on the heap (which can be analogous to basic data types, which are allocated in the virtual machine stack). The Java Virtual Machine specification stipulates that all object instances and array coordinates are allocated on the heap . Understanding this section has a general impression of our memory allocation in the program.

The Java heap is also the main area of garbage collector management, with an alias called "GC Heap" at a time, which is discussed in a later article.

In addition, the Java heap can be physically discontinuous and throws a OutOfMemoryError exception when there is not enough memory in this area.

5. Method area

The function is to store data such as class information, constants, and static variables that have been loaded by the virtual machine.

6. Run a constant-rate pool

According to the name, it is the place where the constants are stored, in the method area. Usually at compile time will determine the program's constants

This section is useful for understanding string:

In general, we use a double quote "" to create a constant and store it in a constant pool, so:

      

String S1 = "java"= "java"; // S1 = = S2 true

When a string object is created, the object is not in the heap, but in a constant pool; When S2 continues the same statement, it looks for the same Unicode-encoded string in the constant pool, and no longer creates it if there is one. The above code creates a total of 1 objects.

The OutOfMemoryError exception also occurs in this area.

However, if you create a string using the new method, that is, a string object that creates two identical content in the heap, it will output a different result:

New String ("Java"new string ("Java"); // S1 = = S2 to False

Because the object is created with the new operator, because the contents of the parentheses (that is, the parameters of the constructor are completely indeterminate), the above code creates two objects with the same contents but different reference addresses.

Object access:

A simple Java statement:

New Object ();

How much of it is involved in the JVM?

First of all, the use of the program counter must be, if there is no program counter, the statement where the JVM will not know; second, this part of Object obj will react to the local variable table in the tool's virtual machine stack as a reference type data, and the new Object () is the allocation of memory in the heap; In an object instance, it contains address information for many object type data, which are stored in the method area and accessible to other threads.

Here, we discuss the object access in the JVM in two ways, one is to access the object through a handle,

  

One is to access the object through a direct pointer. In the case of the virtual machines (Sun hotspot) We use, they are accessed through direct addresses:

  

(Memory area introduced)

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.