Chapter II Java memory area and memory overflow exception

Source: Internet
Author: User

    • Overview
    • Run-time data region
      • Program counter
      • Java Virtual Machine stack
      • Local method Stack
      • Java heap
      • Method area
      • Run a constant-rate pool
      • Direct Memory
    • Hotspot Virtual Machine Object Quest
      • Creation of objects
      • Memory layout of the object
      • Access positioning of objects

Overview

Java programmers do not need to manually free up memory, and C, C + + programmers need to operate on their own, because the Java virtual machine to help us complete this action, so we also need to understand the JVM mechanism, so that the problem will not know what it is. This chapter describes the various areas of JVM memory.

Run-time data region

According to the Java Virtual Machine specification (Java SE version 7), the memory of the Java Virtual machine includes the following runtime data regions

Program counter

This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions. The multithreading of a Java Virtual machine is implemented by thread switching and allocating processor execution time, and the function of the program counter is to remember where the current thread is executing.
The line number indicator of the byte code executed by the current thread, and the bytecode interpreter works by changing the line number of the current program counter.

Java Virtual Machine stack

English name is Java virtual machine stacks, thread private, life cycle is the same as thread
Each method creates a stack frame at execution time, which corresponds to the stack frame's stack and stack in the process from the call to execution.
Stack frame concept: Used to store local variable table, operand stack, dynamic link, method exit login information.
Local Variables Table: stores the compiler to know the various basic data types and object references, ReturnAddress type, etc., the required memory at compile time to complete the allocation
Abnormal:
1. The requested stack depth is greater than the allowed depth of the virtual machine, throwing a Stackoverflowerror exception
2. If the virtual machine stack can be dynamically extended, the request does not have enough memory to throw the OutOfMemoryError exception

Local method Stack

The local method stack is the same as the Java Virtual machine stack, except that the method of the local method stack is the native method

Java heap

The largest chunk of Java Virtual machine memory that stores object instances and arrays, and thread-sharing zones. The main area of the garbage collector management, also known as the GC heap.
Can be in a physically discontinuous memory space, logically contiguous.
-xmx and-xms control, throws a OutOfMemoryError exception if no memory is allocated in the heap and the heap cannot be extended

Method area

Thread-shared memory, storing data such as class information, constants, static variables, instant compiler compiled code, etc. that have been loaded by the virtual machine, alias Non-heap (not heap)
The primary target for memory reclamation is the collection of constant pools and the unloading of types (conditions are harsh)
The method area does not meet the memory allocation requirements and throws a OutOfMemoryError exception

Run a constant-rate pool

Runtime Constant Pool Method section, which holds the various literal and symbolic references generated during the compilation period, may also put new constants into the pool during the run
OutOfMemoryError exception thrown when unable to request memory

Direct Memory

Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the VM specification, but it is also used frequently and can cause outofmemoryerror exceptions.
JDK1.4 new to NiO, a channel-to-buffer-based I/O approach, you can use the native library to directly allocate out-of-heap memory and then manipulate the Directbytebuffer object as a reference to that memory. Avoids copying data back and forth in the Java heap and the native heap.

Creation of a Hotspot Virtual machine Object Quest Object Created with Rapha?l 2.1.0 New Keyword in a constant pool, navigate to a symbol reference? allocating memory (memory acknowledged) initialize the allocated memory to a value of 0 Set object header (e.g which instance, how to find class metadata information, object hash code, object GC generational age, etc.) Initialize settings by program Object Creation Complete class Loading Yes No

Methods for allocating memory in the Java heap: pointer collisions (Bump the Pointer), free list
Concurrency action pointer non-thread safe solution: ① The action of allocating memory space to synchronize the lock processing, ② the memory allocation action according to the thread charge to different space, that is, the thread Local Allocation Buffer,tlab allocates and assigns a new tlab, only need to synchronize lock. Whether the virtual machine uses Tlab and-xx:+/-usetlab parameters to set

Memory layout of the object

Three blocks: Object header (header), instance (Instance data), align Fill (Padding)
-Object Header
1. Officially known as Mark Word, the 32 and 64-bit virtual machines are 32bit and 64bit in length respectively. Stores the object's own runtime data, such as hash code, GC generational age, lock status flag, thread-held lock, biased thread ID, biased timestamp, and so on. Because of the need to store a lot of data, it is designed as a non-fixed data structure to take advantage of more space, which reuse the current space according to the state of the object
2. The type pointer, which points to the pointer to its class metadata, determines which instance of the class this object is. If the object is a Java array, there will be an area in the object header for recording the length of the array
-Instance Data
Valid information that the object really stores, that is, the various types of field classes, whether it is a parent class or a subclass, should be recorded
-Align Fill
It's not necessarily there. The role of the placeholder. The HotSpot VM automatic memory management system requires that the object start address must be an integer multiple of 8 bytes, and the object header is exactly 8 integer times, so the auto-completion is required when the instance data is not aligned.

Access positioning of objects

Java programs need to manipulate the concrete objects on the heap through the reference data on the stack. Because the Java Virtual Machine specification does not define how this reference is implemented, the implementation is also changed with the virtual machine.
The main ways of access are:

    • Using handles

    • Direct pointer

The benefit of using handle access is that when the object changes, the reference does not need to be modified, only the pointer to the object instance is changed.
The advantage of using direct pointers is that it is faster to access objects

There is no time to finish.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Chapter II Java memory area and memory overflow exception

Related Article

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.