Java area of memory and memory overflow exception, to create an object

Runtime data area

Here Insert Picture Description

Program Counter (Program Counter Register)

A smaller memory space can be seen asByte code executed by the current thread indicator line number. In the conceptual model of virtual machines, the bytecode interpreter of this work is that by changing the value of the counter to select a next bytecode to be executed instruction, branching, looping, branching, exception handling, and other basic functions are thread recovery need to rely on the counter to complete.
"Thread-private": Each thread has a separate program counter due to the multi-threaded Java Virtual Machine is in turn switched and allocation of processor execution time by threads to achieve, at any time a determined, a processor will only an instruction execution thread.
If a Java method is being performed, the counter records the address of the virtual machine bytecode instruction being executed; and if a native method, then the counter value is null
program counter isJVM OutOfMemoryError situation does not provide the only region

Stack Java Virtual Machine (Java Virtual Machine Stacks)

Thread-private, Life cycle and the same thread.
Is a virtual machine stack Java memory model described method performed: Each method creates a stack frame while performing for the local variable table information is stored, the operand stack, dynamic linking, method exports.
Local variable table: storing basic data of various types known in the compiler, the type of object reference (reference type, it is not equivalent to the object itself, may be a reference to an object pointer to the starting address, it may be a point representing an object handle or other object associated with this position) and returnAddress type (address pointing to a byte code instruction).
The method of execution of each process corresponding stack frame is a stack in a virtual machine to process the stack from the stack.
When entering a method, this method requires much space for local variables allocated in the frame is completely determined during the implementation of the method will not change the size of the local variable table.
Two kinds of abnormal:
1, if the thread request stack depth greater than the depth allowed by the virtual machine, StackOverflowError exception is thrown.
2. If the virtual machine can dynamically expand the stack, not enough memory to apply the extended, it will throw an OutOfMemoryError.
Example: recursive reference each other, two entity classes.

Native method stacks (Native Method Stack)

The virtual stack machine similar effect, differences: a virtual machine for the station to execute Java virtual machine process (bytecode) services, native method stacks for the virtual machine to service the Native methods.
Note: a Native Method is a non-call interface java java code.

Java 堆 (Java Heap)

Java Java Virtual Machine heap is managed byThe biggest piece of memory.
Java heap is allThreads shareA memory area that is created when the virtual machine is started.The only purpose is to store the object instance, Java Virtual Machine Specification Description: all object instances and arrays to be allocated on the heap. JIT compiler development and maturation escape analysis, distribution on the stack, replacing the scalar optimization techniques to send change, all objects allocated on the heap is not so absolute.
Java garbage collector heap management heap is the main area, also known as "GC heap." From the perspective of memory recovery, Java stack further broken down into: the old and the new generation's; then carefully Eden into space, From Survivor To Survivor space and space. From the perspective of memory allocation, threads share the Java heap may be divided into multiple threads private allocate a buffer (TLAB).
If no memory heap allocated instances complete, the heap can not be extended, it will throw an OutOfMemoryError.

Method region (Method Area)

Threads shareRegional, class information storage has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data. (Referred to permanent generation essence not equivalent)

Runtime constant pool (Runtime Constant Pool)

Generated by the compiler to store the various symbols and literal references cited translated directly into the runtime constant pool is stored after the class is loaded.
Includes a dynamic, during operation of the new constant may be placed, e.g. intern () method of class String.

Direct memory (Direct Memory)

Not part of the virtual machine runtime data area, nor is it a virtual machine memory area defined in the specification, may also lead to an OutOfMemoryError.

Creating an object, layout and access

Creating objects

Create an object only by a new keyword, the process of the virtual machine as follows:
1, first checks whether the parameters of this command to locate in the constant pool of a class symbol references, and check whether the class has been loaded, parsed and initialized .
2, if there is no load, but also the first implementation of the corresponding class loading process.
3, the virtual machine memory for the new object allocation.
4, even after the completion of the memory allocation of the allocated memory space is initialized to zero values, so as to ensure an object instance field may not be assigned an initial value in the Java code can be used directly.
5, will be followed by the implementation of the method, the object is initialized in accordance with the wishes of the programmer.

================================================== =======
virtual machine for the newborn objects are two ways to allocate memory:
select allocation: Java heap whether structured, in turn, used by the garbage collector decides whether to compress with finishing capabilities.
(1) a pointer collision
Java heap memory is structured, when allocating memory, the pointer to the idle memory to the object side moving distance equal size;
(2) free list
Java heap memory is not regular, the recording list of available memory which , recording a large enough space allocated to the object instance and update the list from the list found in the distribution time.

================================================== =======
thread safety issues: concurrent case, the object a memory allocation may occur, not enough time to modify pointer, object B while using the original pointer. Solutions are as follows:
(1) Sync: locking to ensure atomic update operation.
(2) local thread allocation cache (TLAB): the memory allocation operation carried out in accordance with different spatial division threads, each thread pre-allocated heap memory in Java.

Memory layout objects

Structure of an object: object header (Header), instance data (Instance Data), alignment padding (Padding).
(1) object header
a) storing runtime data object itself (Mark Word): hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp.
b) type pointers: a pointer to its class object is metadata, the virtual machine is determined by the pointer which is an instance of the object class.
(2) Examples of data
a) storing sequentially received allocation strategy parameters affecting the virtual machines and the order of fields defined in Java source code.
b) the same width as the field is always assigned together.
c) variables defined in the parent class will appear before the subclass.
(3) alignment padding
a) placeholder effect.
Reason: HotSpot VM requires that objects starting address must be a multiple of 8bit, the size of the object must be a multiple of 8bit.

Access the location of the object

(1) using the handle
advantage that the stable reference is stored in the handler address, only the change in the handle pointer to the instance data when the object moves, the reference itself need not be modified.
Here Insert Picture Description
(2) direct pointer
advantages: speed, saving a pointer is positioned in time overhead.
Here Insert Picture Description

Published 18 original articles · won praise 0 · Views 425

Guess you like

Origin blog.csdn.net/qq_33805483/article/details/104105100