Java memory overflow exception

Source: Internet
Author: User

Java memory overflow exception
Part 1: Java Memory Model 1. Java Memory Model


Ii. Program counters

1. This is a small memory space and can be seen as the row number indicator of the bytecode executed by the current thread.

In the virtual machine conceptual model, when the bytecode interpreter is working, it selects the next bytecode instruction, branch, loop, jump, exception handling to be executed by changing the counter value, this counter is required for basic functions such as thread recovery.

2. The program counter is the private memory of the thread.

The multithreading of the Java Virtual Machine is implemented by switching threads in turn and allocating the execution time of the processor. At any definite moment, a single processor (for a multi-core processor, It is a kernel) will execute commands in a thread. Therefore, in order to restore the thread to the correct execution location after switching, each thread requires an independent program counter, and each thread does not affect independent storage. This is the private memory of the thread.

3. What is stored?

 

If the thread is executing a Java method, this counter stores the address of the Virtual Machine bytecode instruction being executed; if the thread is executing a Native method, the counter value is null, Nndefined.

 

4. Will OOM exceptions occur?

This memory region is the only region in the Java Virtual Machine specification that does not specify any OutOfMemoryError conditions.

Iii. Java virtual Machine Stack

It is also the thread private memory, with the same lifecycle as the thread.

1. What is the description of the Java Virtual Machine stack?

It describes the Memory Model of Java method execution. Each method creates a stack frame while executing it to store the local variable table, operand stack, dynamic link, and method exit.

The process from calling to execution of each method corresponds to the process from stack frame to stack.

1.1 What is stored in the local variable table?

 

It stores various basic data types (boolean, byte, char, short, long, int, float, and double) known during the compilation period. The reference type is not the same as the object itself, it may be a reference pointer pointing to the starting address of the object, or it may be pointing to an object handle or other location related to the object.) returnAddress type (pointing to a bytecode instruction address)

 

1.2 allocated memory size and allocation time

Data of the 64-bit long and double types occupy two local variable spaces (slots), and only one other data type is occupied.

The memory space required by the local variable table is allocated during the compilation period. when you enter a method, the size of the local variable space allocated in the stack frame is completely determined, the local variable table size is not changed during method running.

1.3 defined exceptions

StackOverFlowError exception: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, the OutOfMemoryError exception is thrown: If the Virtual Machine stack can be dynamically expanded (currently most virtual machines can be dynamically expanded, the virtual machine specification also allows a fixed-length Virtual Machine stack). If you cannot apply for enough memory during expansion, this exception is thrown.
Iv. Local method Stack

The memory zone plays the same role as the Java Virtual Machine stack. The differences between them are:

The Virtual Machine stack is a Java method (also known as bytecode) service, and the local method stack is a Native method service used by the virtual machine, but the exception thrown is the same.

The Sun hotspot VM combines the local method stack with the Virtual Machine Stack directly.

V. Java heap
This is the largest memory managed by the Java Virtual Machine. This area is the memory area shared by all threads. Created when the VM is started.

1. What does Java heap do?

The instance that stores the object. Almost all objects are allocated memory here. In the Java Virtual Machine specification, all object instances and arrays must be allocated on the stack. But in fact, with the development of JIT compiler and the gradual maturity of escape analysis technology, allocation on the stack and scalar replacement optimization technology make this theory not so absolute

2. GC heap

Java is the main area for managing the Garbage Collector. Therefore, it is also called a GC heap. From the perspective of garbage collection, Java heap can be divided into the new generation and the old generation. From the perspective of memory allocation, the Java heap shared by threads may be divided into private allocation buffers for multiple threads. No matter how it is divided, it has nothing to do with the content to be stored. No matter which region it is stored, it stores the object instance. The purpose of the Division is to better recycle the memory or allocate the memory faster.

3. Exceptions

The Java heap can be physically discontinuous memory space, as long as it is logically continuous. Currently, mainstream virtual machines are implemented based on scalability and are controlled through-Xmx and-Xms. If the instance is not allocated in the heap and the heap cannot be expanded, an OOM exception is thrown.

Vi. Method Area

This is the memory area shared by the thread.

1. What is stored?

Stores information about classes loaded by virtual machines, constants, static variables, and Code Compiled by the real-time compiler.

2. Permanent generation

The method area is described as the logic part of the heap, but the alias is Non-heap, so as to distinguish it from the Java heap.
For hotspot virtual machines, the method area is also called permanent generation. The two are not equivalent, just because the design team of the Hotspot Virtual Machine chooses to extend GC generation collection to the method area, or use permanent generation to implement the method area.

In this way, the hotspot garbage collector can manage this part of memory just like the Java heap.

3. Exceptions

Java virtual machines have loose specifications for the method area. In addition to Java, they do not require continuous memory and Class 1 to be fixed or scalable. In addition, they can also choose not to implement garbage collection.

An OOM exception is thrown when the method area cannot meet the memory allocation requirements.

7. Runtime Constant Pool

Is a part of the Method Area. In addition to the Class version, field, method, interface, and other description information, the Class file also contains the constant pool table ), it is used to store various nominal value constants and symbol references generated during the compilation period.

8. Direct Memory

It is not part of the data zone during virtual machine running, nor is it the memory zone defined by the Java Virtual Machine specification. However, it is frequently used and may throw an OOM exception.

 

Part 2: Hotspot virtual machine object discovery I. Object Creation

Object creation is divided into three stages

Phase 1: class loading check

When the VM encounters a new command, it first checks whether the parameter of this command can locate a Class symbol reference in the constant pool, check whether the class referenced by this symbol has been loaded, parsed, and initialized. If not, execute the class loading process of the response.

Phase 2: allocate memory

The size of the memory required by the object can be determined after the class is loaded. Allocating memory for the object is equivalent to dividing a fixed size of memory from the Java heap on the Java stack.

Two partitioning methods (Allocation Method)

1. pointer collision (Bump the point) ---- the memory in the Java heap is absolutely regular

The memory in the Java heap is absolutely complete, so all the used memory is put on one side, the idle memory is put on the other side, and a pointer is put in the middle as the demarcation point indicator, then, the allocated memory is

Move this pointer to the idle memory to the distance of an object.

2. Free List ---- the memory in the Java heap is not regular

The memory in the Java heap is not regular. That is to say, if the used memory and the unused memory are intertwined, there is no way to Use Pointer collision. The virtual machine must maintain a list, record the blocks that can be used. When allocated, find a large enough space in the list and divide it into the object instance, and update the records in the list.

The distribution method is determined by the Java heap's normalization. the Java heap's normalization is determined by the garbage collector's compression and sorting function.

All collectors with Compact processes, such as Serial and ParNew, Use Pointer collisions.

The CMS-based Collector Based on the Mark-Sweep algorithm uses the idle list.

After the memory is allocated, the VM initializes all allocated memory space to zero (excluding the object header)

Phase 3: Necessary settings

The virtual machine makes necessary settings on the object and sets the instance of the class as the object. How can we find the metadata information of the class, the hash of the object, and the GC age of the object, the information is stored in the object header.

From the perspective of virtual machines, a new object has been generated, but from the perspective of Java programs, object creation has just begun, The method has not been executed, and all fields are still zero.

Therefore, after executing the new command Method to initialize the object according to the wishes of the programmer. In this way, a real object is created.

 

 

Ii. Object Memory Layout

The layout of Java objects in the memory can be divided into three areas: object Header, Instance data, and object Padding ).

1. Object Header)

The object header of the Hotspot Virtual Machine includes two parts: the first part is used to store the data of the object's own runtime, the entry hash, GC generational age, lock status mark, and the lock held by the thread, biased towards thread ID and timestamp. the data length is 32-bit and 64-bit on 54-bit virtual machines, respectively. It is officially called Mark Word.

The second part is the type pointer, that is, the pointer of the object to its class metadata. The VM determines through this pointer that the object is an instance of that class, not all Virtual Machine implementations must retain type pointers on object data. If the object is an array, the object header must also have a piece of data used to record the length of the array.

2. Instance data)

This is really valid information ., That is, the content of various types of fields defined in the program code, whether inherited from the parent class or defined in the subclass, must be recorded.

The storage order of this part will be affected by the order defined by VM allocation policy parameters and fields in Java source code.

3. Object filling (Padding ).

This part does not exist and has no special meaning. It only acts as a placeholder.

 

3. Access and positioning of Objects

Objects are created to use objects. Our Java program uses reference data on the stack to operate specific objects on the stack. The reference type specifies

Does not specify the method in which the reference should be located to access the specific location of the object in the heap. Therefore, object access also depends on the implementation of virtual machines. Currently, there are two mainstream access methods: handle and direct pointer.

1. Handle

The Java heap divides a piece of memory into a handle pool. The reference stores the handle address of the object, and the handle contains the specific address information of the object instance data and the type data.

2. Direct pointer

In the layout of Java heap objects, you must consider how to prevent access to the relevant information of type data, and the object address stored in reference is directly the object address.

The two methods have their own merits. The advantage of using a handle is that the reference stores a stable handle address. objects are moved when they are moved (garbage collection, object movement is common) the value changes the pointer of the instance in the handle, but the reference itself does not need to be modified.

However, the biggest advantage of direct pointer is faster speed, saving the time overhead of one pointer location.


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.