Stacks, heaps, method areas, and constant pools in Java __JVM

Source: Internet
Author: User
Tags garbage collection wrapper

To speak of stacks, heaps, method areas, and constant pools in Java, mention Hotspot,hotspot is the virtual machine that is in the sun jdk and Open JDK. (The code implementation is essentially the same except for the annotations of the Sun JDK and the Open JDK)

The following content is all about hotspot.

Stack (stack): is divided into VM stack (virtual machine stack) and native method stack (local methods stacks), but the hotspot virtual machine directly to the local method stack and virtual machine stack.

Virtual machine stack: Thread-Private, describes the memory model executed by the Java method, while the method invocation creates a stack frame (storing local variable tables, operand stacks, method exits, etc.), and each method's call until execution completes corresponds to the stack frame                               In the virtual machine into the stack and the process of stack. local variable table (usually said stack is actually the local variable table inside the stack): Storage of basic data type variables and objects of the reference (there is a compilation time required to complete the allocation, method runtime does not change the local variable table size, four bytes occupy a local variable space)

The data in the--------stack can be shared:

int a = 3;
int b = 3;
The compiler deals with int a = 3 First, it creates a reference to a in the stack, finds out if there is a 3 value in the stack, and if not, it stores 3 in, then points a to 3. then process int b = 3, after you create the reference variable for B, because you already have 3 in the stack, point B directly to 3. In this way, there is a case where A and B both point to 3. At this point, if you make a=4 again, the compiler will search for a 4 value in the stack, and if not, put 4 in and point A to 4, and if so, direct a to this address. Therefore the change of a value does not affect the value of B. Note that this sharing of data is different from the two-object reference to an object, because the modification of a does not affect B, it is done by the compiler, and it helps save space. While an object reference variable modifies the internal state of the object, it affects another object reference variable.

Wrapper class data, such as Integer, String, double, which wraps the corresponding basic data type. All of these class data exist in the heap, and Java uses the new () statement to tell the compiler that it is dynamically created at run time, so it is more flexible, but the disadvantage is that it takes more time.

= = Compare the address of the object, that is, whether it is the same object;

Equal compares the value of an object.

When an int becomes an integer, if the value is between -128~127, the new integer object is not stored in the constant pool, this is done to improve efficiency-----> Remember to see it somewhere else.


Java code
public class Test {
public static void Main (string[] args)
{int a1=1;
int b1=1;
int c1=2;
int d1=a1+b1;
Integer a = 1;
Integer B = 2;
Integer C = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println (A1==B1); True result 1
System.out.println (C1==D1); True result 2
System.out.println (C==d); True result 3
System.out.println (E==F); False result 4
}
}
Analysis: Results 1:a1==b1 As mentioned above, will open storage space in the stack to store data.
Result 2: First of all, it creates a reference in the stack for a variable of C1, then look for any address with a literal value of 2, not found, to open a deposit of 2 of the value of the address, and then point the C1 to 2 of the address, D1 two literal values are added to 2, because the stack already has 2 this literal, The D1 directly points to the address of 2. In this way, there is a situation where C1 and D1 both point to 3.

Before we analyze the results below let's take a look at the Java automatic unboxing and boxing: When automatically boxing, the int becomes an integer, there is a rule when the value of your int is in -128-integercache.high (127), Instead of returning an integer object that is already cached in the heap, we can see that the system has cached an integer from 128 to 127 into an integer array. If you want to turn an int into an integer object, first go to the cache, find the words directly return the reference to you, you do not have to new one, if not -128-integercache.high (127) Returns an integer object that is newly created.

Result 3: Since 3 is in range so the data is taken from the cache, C and D point to the same object, and the result is true;
Result 4: Because 321 is not in scope so it is not the data from the cache but rather the new object, E and F do not point to the same object, and the result is false;

Java heap:

The Java heap is an area shared by all threads that is created when the virtual machine is started, and the only purpose of this memory is to store the main areas of object instances and array GC administration.

divided into the Cenozoic (Eden Survivor survivor8:1:1) and the old age

The Java heap can be in a physically discontinuous memory space, as long as the logic is continuous

About creating objects Please refer to: Http://note.youdao.com/yws/public/redirect/share?id=5177014ee5ad1ac3f0af9fdab3b011a3&type=false
For GC Recycling Please refer to: Http://note.youdao.com/yws/public/redirect/share?id=96928e82082b4dec8831e5099769172b&type=false

Method Area: Non-equivalent Yunju generation hotspot using the permanent generation of the method area (in the hotspot of jdk1.7 has already put in the permanent generation of the string constant pool out) is a thread-shared memory area like a heap .              Used to store data such as class information, constants, static variables, and Just-in-time compiler-compiled code that have been loaded by the virtual machine. Garbage collection behavior rarely occurs in the method area, and the primary goal of this area collection is to recycle and unload the type for a constant pool

Run frequent pool: part of the method area constant pool is used to hold the various literal and symbolic references generated during compilation (and                         A direct referencethat is translated, which is stored in a Run-time constant pool of the class loading the LIFO method area. Another important feature of the runtime's constant pool relative to the class file is its dynamic nature and the possibility of putting new constants into the pool during the run

Literal: such as a literal string, declared as a final constant value, and so on.

Public stick Final int i = 3;

String s= "ABC";

Symbolic references: Fully qualified names of classes and interfaces

The name and descriptor of the field

The name and descriptor of the method

The nature of a String is an array of characters. The standard implementation of String contains 4 instance variables that point to the reference int of the character array Type offset int type string length int type hash value
public class string{Private char[]value;       private int offset;       private int count; private int hash; ... }
A string object uses a total of 40 bytes (16-byte object itself cost + 8-byte reference + three int type (12 bytes) + 4 bytes (padding bytes)), because a string's char array is often shared among multiple strings, so a string object is immutable

A string is a special wrapper class data. That is, it can be created in the form of string str = new String ("abc"), or it can be created in the form of string str = "ABC";

String str = "ABC" Procedure for creating an object
1 First find out if there is an "ABC" string object in the constant pool
2 Create "ABC" in the constant pool if it does not exist, and have str reference the object
3 if present, let Str refer to the object directly


As for "ABC" is how to save, where to save. A constant pool is part of the class information, the class information reflected in the JVM memory model corresponds to the method area that exists in the JVM memory model, meaning that the constant pool concept in this class information exists in the method area, and the method area is allocated by the JVM in the heap in the JVM memory model, so "ABC" It can be said to exist in the heap (and some data, in order to differentiate the method area's heap from the JVM's heap, refer to the method area as a stack). In this case, "ABC" is written in bytecode at compile time, so the JVM allocates memory for "ABC" in a constant pool when class is loaded, so it is almost the same as the static area.
Procedure for creating an instance of 4 (2) String str = new String ("abc")
1 first create a specified object "ABC" in the heap (not a constant pool) and have the STR reference point to the object
2 view in the string constant pool, if there is an "ABC" string Object
3 if present, link the new string object to the object in the string constant pool
4 If it does not exist, create a string object with the content "ABC" in the string constant pool and associate the object in the heap with it

String str1 = "abc"; String str2 = "AB" + "C"; STR1==STR2 is ture.
Because string str2 = "AB" + "C" looks for the existence of an "ABC" string object in a constant pool, if there is a direct str2 reference to the object, it is obvious that the string str1 = "abc", it says, will create an "ABC" object in the constant pool, So str1 references the object, STR2 also references the object, so STR1==STR2
String str1 = "abc"; String str2 = "AB"; String STR3 = str2 + "C"; STR1==STR3 is false---------//Refer to Java Programming Ideas Fourth Edition p284,285
Because the string str3 = str2 + "C" involves the addition of a variable (not all constants), a new object is generated, whose internal implementation is the first one StringBuilder, then the Append (str2), append ("C"); Then let STR3 refer to the object returned by ToString ()

string s = new String ("abc"); produces several objects.

One or two, if there is no "ABC" in the constant pool, it is two (data sharing in the reference stack).



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.