Java

Memory area

Heap

Thread the public

Store instance objects

is

GC

The main management area, therefore, can be more detailed divided into: new generation, old age

A little more detail:

Eden

Area,
From Survivor
Area,
To Survivor
area

Memory space: can be physically discontinuous, logically continuous.

Method Area

Thread the public

Main stores: class information, constants, static variables, compiled code

Run-time constant pool

Primary storage: compile-time literals and symbolic references

Dynamic, meaning that constants can be pooled at run time.

VM Stack

Thread private

Mainly include:

Local variable table: store various basic data types, object references,

returnAddress

type

Operand stack: Each element can be arbitrary

java

Type,
32
The bit data type occupies the capacity of
1
.
64
The bit data type occupies the capacity of
2

Dynamic connection:

class

The file’s constant pool contains a large number of symbolic references, some of which are converted to direct references during class loading or the first time they are used, and this part is called static resolution. The other part is converted to a direct reference every time it runs, and this part is called the dynamic connection.

Method exit: for example

A

Method is called
B
Method,
B
The return value of the method is pressed into
A
Method in the stack frame.

Native Method Stack

Thread private

with

VM Stack

Similar, the only difference is that the stack is
Native
Method services.

Hot Spot

will
VM Stack
with
Native Method Stack
Be one.

Program Counter Register

Thread private

The address of the instruction used to record the execution of bytecode by the thread.

Direct Memory

NIO

Direct memory is used to improve efficiency.

Object creation process



First when the virtual opportunity to a

new

Command to check whether the class represented by the symbol reference has completed class loading. If not, perform the following steps

Class loading

Allocates memory for objects

Assignment mode: pointer collision

/

The free list

Thread safety:

CAS

To solve

The memory space of the VM is initialized

The VIRTUAL machine performs necessary Settings on the object

Initialization is complete

Object creation completed

Object memory layout



The first part is used to store the runtime data of the object itself, such as hash codes (

HashCode

),
GC
Generational age, lock status flags, locks held by threads, thread bias
ID
, bias timestamp, age of object generation, this part of information is called”
Mark Word
“;
Mark Word
Designed as an unfixed data structure to store as much information as possible in a very small space, it will reuse its own storage space according to its own state.

The second part is the type pointer, that is, the pointer to the object’s class metadata, which the virtual machine uses to determine which class instance the object is.

If the object is one

Java

Array, which must also have a block of data in the object header that records the length of the array. Because virtual machines can pass ordinary
Java
Object metadata information is determined
Java
Object, but the size of the array cannot be determined from the array metadata.

in

32

Bit system, store
Class
The size of the pointer is
4
Bytes,
Mark Word
And the size of the space
4
Byte, so the header is
8
Bytes, plus if it’s an array
4
Bytes represent the length of the array, as shown in the following table:



in

64

A system and
64
position
JVM
Next, turn on pointer compression, then head storage
Class
The size of the pointer is still
4
Byte, and
Mark Word
The region will get bigger and become
8
Bytes, that is, the header is at least
12
Bytes, as shown in the following table:

Compressed pointer: Enable pointer compression using algorithm overhead to bring memory savings,

Java

Objects are based on
8
Byte aligned, that is, with
8
Byte is the basic unit of memory access, so in geographical processing, there are
3
The ones place is free. This is
3
The ones bits can be used to manipulate
32
Bit address Pointers are originally addressable at most
4GB
But plus
3
Bits of
8
I’m going to be able to do it internally
32GB
Addressing.

Object access location

Two ways:

Handle pool: Reference stores handle addresses. When the instance object moves, only the pointer corresponding to the handle needs to be changed instead of the reference itself, which is relatively stable.

Direct pointer: fast speed, saving the cost of a pointer location.

Commonly used instructions

invokeinterface

: to call the interface method, at run time to search an object that implements the interface method, find the appropriate method to call.

invokevirtual

The: directive is used to invoke instance methods of an object, dispatching according to the actual type of the object

invokestatic

: used to call class methods

invokespecial

The: directive is used to call instance methods that require special treatment, including instance initializer methods, private methods, and superclass methods.

invokedynamic

:
JDK1.7
A new virtual machine instruction, compared to the previous four instructions, their dispatch logic is fixed in
JVM
Inside,
invokedynamic
Is used to handle new method dispatch: It allows application-level code to determine which method call to execute, only when the call is about to be executed
.
To achieve dynamic language support.

Stack-based instruction set

&&

Register-based instruction set

java

The stack – based instruction set is used, which relies on the operand stack to work

Advantages:

Portability: Because registers are provided directly by the hardware, programs that rely on registers are inevitably constrained by the hardware

Compact program code

The compiler is simple to implement

Disadvantages:

Slow speed

Large number of instructions: More instructions are required to perform the same function than the register architecture, because there are already many instructions in and out of the stack

Memory access: Frequent stack access means frequent memory access, and memory is always a speed bottleneck for the processor.

Java

Memory overflow exception

Out of memory

When there is no memory on the heap for instance allocation and the heap cannot be expanded:

java.lang.OutOfMemoryError: Java heap space

When the method area and the constant pool of memory cannot meet the memory allocation requirements:

java.lang.OutOfMemoryError: PermGen space

Unable to allocate enough memory for stack expansion:

java.lang.StackOverflowError

A memory leak

The memory dynamically allocated by the program caused by the code design was not released, resulting in this part of the memory is not available

The difference between memory overflow and memory leak

Memory leaks are one of the causes of memory overruns, and memory leaks accumulate and cause memory overruns

Memory leaks can be fixed by improving the code. Memory leaks cannot be completely avoided, but can only be configured to reduce their frequency

Memory leakage Check memory overflow

Performance monitoring tools:

JProfiler

Optimizeit Profiler

Eclipse Memory Analyzer

EclipseMAT

JProbe