Java memory area and overflow exception

Source: Internet
Author: User
Tags throw exception

Memory Partitioning:

Java virtual machines are managed by dividing the memory into the following areas during the execution of Java programs

Thread Private
Virtual Machine Stack
Local variable table
Basic data types
A long and double occupy two slots
Object reference
return address

Operand stacks
Dynamic links
Method exports and other information

    Throw exception:

Stack depth too large stackoverflowerror
Request Insufficient Memory space OutOfMemoryError

Program counter

Local method Stack
Thread-Shared
Heap
Virtual Machine Qidong when created
Method area
Collection of constant pools and unloading of types
Run constant pool: literal and symbolic references translated directly by reference

Direct Memory

NiO can be allocated directly using the native library, which can significantly increase efficiency because data is not replicated in the Java heap and the native heap

However, when allocating memory, if the direct memory plus Java memory exceeds the computer physical memory limit, it will be reported OutOfMemoryError

Virtual Machine objects:
Object is created:
1.new defines a symbol in a constant pool
2. Check whether the class represented by the symbol is loaded parsing initialization
If not, the load class
3. After the class is loaded, allocate memory for the new object (the allocated memory size is determined after the class is loaded)

Memory allocation Method:
If the memory is a regular pointer collision (where the pointer moves the memory size of the object)
If the memory is interleaved, the free list records which memory is available, allocates an object instance when it is allocated to large enough memory to the object, and updates the list

The choice of which allocation method is determined by whether the Java heap is structured or not, and whether the heap is structured by the Java collector is determined by the compression finishing function.


Thread safety issues for allocating memory
Synchronous processing of memory-allocated actions
Tlab local allocation buffer each thread pre-allocates a chunk of memory, which thread allocates memory, which is allocated on which thread, and the synchronization lock is required only if Tlab is exhausted and a new tlab is allocated
4. After memory allocation, the memory space is initialized to 0 values
5. Set the object to the necessary settings (class metadata hashcode GC years)

The layout of the object:
Object Header
Run-time data
Hashcode GC Generational age lock status identity thread hold lock bias thread ID bias timestamp
Type pointer
If it is an array, the object header holds the length information of the record array
Instance data
Virtual Machine Assignment Policy
Snap To fill
Hotpot virtual machines require that the size of the object is 8 bytes of integer times the object header is exactly a multiple of 8 bytes if the instance data is not aligned, it needs to be supplemented with an aligned fill


Object Access Location:
The reference definition on the stack, the virtual machine specification does not define the way in which the reference locates the object location, which is done by the virtual machine itself, and the main access methods are two kinds:
1. Handle

    

Benefit: When moving an object, you only need to change the instance data pointer of the handle, without changing the pointer of the local variable in the stack (it points to)

2. Direct pointer

    

Benefit faster, saving time for pointer positioning

OutOfMemoryError

VM Args:-xms20m-xmx20m-xx:+heapdumpoutofmemoryerror

Java memory area and overflow exception

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.