An analysis of the allocation of memory locations on the Java JVM

Source: Internet
Author: User
Tags true true

Analysis Java of the JVM allocation of memory locations on the

Introduction to the memory area of 1.Java

1> Program Counter:

A small piece of memory space, each thread has a separate counter, thread private; function: As the current thread code line line number indicator, this value can choose the next need to execute the bytecode directive, such as Branch, loop, etc., each created a thread will generate a program counter

2> Stack

Thread-private, for storing local variables, saving the value of the basic data type, the operand stack (preserving the intermediate result of the calculation process), dynamic link, method entry and exit, and other information; The local variable table holds the parameters and local variables of the function, and when the call is finished, the stack frame is destroyed and the local variable table is destroyed. The local variable table can reuse the slot of the expired local variable, the stack frame of the stack there are two cases, return and exception, the stack is advanced after the structure, there is depth so overflow, the above mentioned operand stack and local variable table is the stack frame part, stack frame at least three parts, the operand stack, local variable table, Frame data area.

3> Heap

Thread sharing, the largest piece of memory area managed by a virtual machine, storing object instances and arrays, and noting that the objects created contain only the individual member variables (the object's properties) and do not include member methods. Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

4> this method to the stack

For native Services (C/c++/fortran writing), the behavior and stack are basically similar

5> Method Area

Thread sharing, which holds the class information loaded by the virtual machine, the constant pool in the method area holds the constant (literal), static variables and other data

6> running a constant-volume pool

A part of the method area that holds the various literal and symbolic references (symbolic references: the fully qualified name of the class and interface, the name and descriptor of the field, the name of the method, and the descriptor) generated by the compilation period

7> Direct Memory

Direct memory is not the memory in the Java specification, nor is it subordinate to the heap, the size is not limited by the heap size, direct memory is pointing to the Java heap, directly to the system to request memory space, usually the direct memory efficiency is better than the Java heap, The sum of the direct memory and Java heap is still limited by the maximum memory limit that the operating system can give. In NiO (see later NIO), direct memory is accessed through the Directbytebuffer in the heap, which can significantly improve performance in some scenarios

2. Example 1:

Package statictest;

public class Test {//Load in method area

public static int i0=10; Method area

Public final int i=100;//constant pool

public static void Main (string[] args) {

int i1=10; Stack

int i2=10;//Stack

String str1= "ABC"; Stack points to a constant pool

String str2= "ABC"; STR2 stack points to a constant pool

String str3=str1+str2;//stack points to heap

String str4= "Abcabc"; stack points to constant pool

String Str5=new string ("abc"); Stack point to Heap

int i3=i1+i2; Stack

int i4=20; Stack

System.out.println (I0==I1);

System.out.println (I1==I2);

System.out.println (I3==I4);

System.out.println (STR1==STR2);

System.out.println (STR3==STR4);

System.out.println (STR1==STR5);

}

}true true True True false false

Analysis: The class is loaded into the method area first, and then a class object is placed in the heap as a reference, the main method itself in the method area, I0 in the method area, the base data type I1,i2, and the reference to the string STR1,STR2,STR4 using the constant pool technique is in the stack, The data in the stack can be shared, so i1,i2 is pointing to the same number, for the basic type of data stored in the stack, if it does not exist then create, if it exists then point to the existing address, the operation of the basic type data is also in the stack (operand stack), For the wrapper class (with or without new) and the basic type of data = = and various operations, Java automatically splits the operation into the corresponding basic type data, pointing to the corresponding stack, The wrapper class (with or without new) and the various operations of the wrapper class (whether or not new) are converted to the base type data accordingly (except for = =), the instance of new is in the heap, new only creates its own member variable (not including the static part), does not contain the method body, The final modified data in the constant pool, for both string and wrapper classes implement a constant pool technology, string will first check the constant pool exists, if not exist then create, exist to point to the existence of the address, the addition of the string is in the heap, the strings of the + The underlying is based on the StringBuilder (see subsequent Java character processing). In jdk6.0, the Chang of a string in the method area, the Chang of a string in jdk7.0 is in the heap, the individual member variables are created after new, and for the static member variables in the method area (which are implemented by the class object in the heap) and the method itself, there is only one copy for Constants in a constant pool exist only one copy, and cannot be changed after the initial value, for integers between byte and less than -128~127, and for wrapper classes of character and for strings that are directly pointing to the same address of the constant pool without new. New will then enter the heap accordingly, and for the other wrapper classes, it will go into the heap accordingly. The literal in the constant pool differs from the basic type data in the stack, the underlying type data in the stack is automatically converted, and the literal in the constant pool is not automatically converted, such as double d1=123456d; subsequent d, no d will compile the error, etc.

The difference between an instance and an object: a new or reflected instance (newinstance, etc.) is called an instance, a reference to a new or reflected instance is called an object;

An analysis of the allocation of memory locations on the Java JVM

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.