Home > Java > javaTutorial > Share a summary of Java memory distribution

Share a summary of Java memory distribution

Y2J
Release: 2017-04-28 10:09:46
Original
1542 people have browsed it

This article mainly introduces the detailed explanation of the memory distribution when the Java program is running. Friends who need it can refer to

Java Memory Distribution: The Java virtual machine will The memory it manages is divided into several different data areas: method area, virtual machine stack, local method stack, heap, and program counter.

1. Program counter

The program counter is a small memory space. It can be regarded as a line number indicator of the bytecode executed by the current thread. . In the conceptual model of the virtual machine, the bytecode interpreter works by changing the value of this counter to select a bytecode instruction that needs to be executed.

Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this counter to complete.

In order to restore the correct execution position after thread switching, each thread needs to have an independent program counter. The counters between each thread do not affect each other and are stored independently, so this type of memory area is " Thread private" memory.

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed; if the thread is executing a Native method, the counter value is empty, and this memory This area is the only area in which the Java Virtual Machine Specification does not specify any OutOfMemoryError conditions.

2. Java virtual machine stack

Like the program counter, the Java virtual machine stack is also private to the thread, and its life cycle is the same as the thread. The virtual machine stack describes the memory model of Java method execution; when each method is executed, a stack frame is created to store information such as local variable tables, operand stacks, dynamic links, method exits, etc.

Each method, from invocation to completion of execution, corresponds to the process of a stack frame being pushed into the virtual machine stack and then popped out of the stack.

The local variable table stores various basic data types, object references (not equivalent to objects, but references to objects) and returnAddress types that are known at compile time. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown. If sufficient memory cannot be applied for, an OutOfMemory exception will be thrown.

3. Local method stack

The difference between the local method stack and the virtual machine stack is that the virtual machine stack serves the Java (bytecode) executed by the virtual machine , and the local method stack serves the Native methods used by the virtual machine.

4.Java Heap

The Java heap is a memory area shared by all threads and is created when the virtual machine starts. The only purpose of this memory area is to store object instances. The Java heap is the main area managed by the garbage collector

5. Method area

The method area is the same as the Java heap. It is a memory area shared by each thread and is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.

6. Runtime constant pool

The runtime constant pool is part of the method area. In addition to the description information of the class version, fields, methods, interfaces, etc., the Class file also has a constant pool, which is used to store various literals and symbol references generated during compilation. This part of the content will be loaded in the class. Then it is stored in the runtime constant pool in the method area.

Many explanations of constant pools on the Internet will use strings as examples:

For example,

String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hel" + "lo";
String s4 = "Hel" + new String("lo");
String s5 = new String("Hello");
String s6 = s5.intern();
String s7 = "H";
String s8 = "ello";
String s9 = s7 + s8;
 
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // true
System.out.println(s1 == s4); // false
System.out.println(s1 == s9); // false
System.out.println(s4 == s5); // false
System.out.println(s1 == s6); // true
Copy after login

s1==s2 is true and is easy to understand, pointing to the same The memory address of a constant pool.

s1==s3 is true: For s3, since all concatenations are literals, the compiler will optimize, which actually means s3="Hello"

s1== s4 is false: Since new String("lo") is not a literal, but a variable, the compiler will not optimize because the variable may change.

s1==s9 is false: the same as above.

s4==s5: The references of two different objects are of course different.

s1==s6: Since the String.intern() method means: if the constant pool already contains a string equal to this String object (the object is represented by equals(Object ) method determines), then the string in the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

The above is the detailed content of Share a summary of Java memory distribution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template