Java & memory area of memory overflow exception introduction

  Java memory area

  Heap

  Thread public

  Store instance of an object

  Is the main management area GC, it can be divided into more detailed: the new generation, old years

  Again more carefully divided: Eden District, From Survivor area, To Survivor District

  Memory Space: may not physically continuous, continuous to the logic.

  Method Area

  Thread public

  Main storage: class information, constants, static variables, the compiled code

  Runtime constant pool

  Main memory: compile the literal and symbolic references

  It is dynamic, i.e., may be run into the constant pool.

  VM Stack

  Thread-private

  mainly include:

  Local variable table: all the basic data types of the stored compiled, object reference, type returnAddress

  Operand stack: each element can be any type of java, 32-bit data type capacity occupied by data bits 1,64 occupied capacity type 2

  Dynamic Link: class file constant pool of a large number of symbolic references, these symbolic references in part or in class loading phase is converted to a direct reference to the first time you use this part as a static resolution. Another part is the conversion time for each run direct reference, which is the dynamic part of the connection.

  Method outlet: for example, the method A calls B, the return value of the stack frame push A method B method.

  Native Method Stack

  Thread-private

  And VM Stack similar, the only difference is that the Native method stack to serve.

  The VM Stack with Hot Spot Native Method Stack one.

  Program Counter Register

  Thread-private

  Recording the address of the bytecode instruction execution threads.

  Direct Memory

  NIO use direct memory and improve efficiency.

  Object creation

  Whether the first opportunity when the time to a new virtual instruction, go check on behalf of the symbolic references of the class class loading has been completed, if not completed, perform the following steps

  Class loader

  Allocate memory for the object

  Distribution: pointer collision / free list

  Security Thread: CAS solve

  VM initialization memory space

  Virtual machine objects necessary settings

  Execution is complete initialization

  Objects created

  Object Memory layout

  A first portion for storing runtime data object itself, such as a hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp, subject generational age, this part information called "Mark Word"; Mark Word is designed as a non-fixed data structures for storage in a very small space as much information, it will reuse their own storage space according to their state.

  The second part is the type of pointer, i.e. a pointer to its class object is metadata, the virtual machine is determined by the pointer which is an instance of the object class;

  If the object is a Java array, in that the object must have a head for recording the data length of the array. Because the virtual machine can determine the size of Java objects through metadata information ordinary Java objects, but can not determine the size of the array from the metadata array.

  In the 32-bit system, Class storage space pointer is 4 bytes, Mark Word space is 4 bytes, the header is 8 bytes, if it is combined with the array requires 4 bytes indicates the length of the array.

  In the 64-bit and 64-bit system, the JVM, pointer compression is turned on, then the head pointer storage space or Class 4 bytes, and Mark Word area becomes large, it becomes 8 bytes, that is, a minimum of 12 characters head section, as shown in the following table:

  

Here Insert Picture Description

Zhengzhou flow of the hospital http://mobile.zzzzyy120.com/

  Compression pointer: pointer to open the overhead of compression algorithm is used to save memory, Java objects are based on 8-byte aligned, it is the basic unit of 8 bytes of memory access, then on geoprocessing, there are three bits are free, three bits may be used to virtual, address pointer using the maximum of the original 32-bit address 4GB, but with three bits of eight internal operation, can change the address of 32GB.

  Object Access positioning

  In two ways:

  Pool handle: the handle is stored in the reference address, when the object moves instance, only the corresponding pointer to change the handle, without changing the reference itself, is relatively stable.

  Direct pointers: fast, save a pointer is positioned overhead.

  Common commands

  invokeinterface: to call the interface method at runtime search for an object that implements this interface method call to find suitable method.

  invokevirtual: instructions for instance method invocation object is dispatched according to the actual type of the object

  invokestatic: to call the class method

  invokespecial: Instructions for invoking the instance methods require special handling, including instance initialization method, and a private methods inherited methods.

  invokedynamic: JDK1.7 added a new virtual machine instructions, as compared to four instructions prior to their dispatch logic within the JVM are cured, and then the new invokedynamic dispatch method for processing: it allows application-level code to determining which one of a method call, only when calls to be executed, will be such judgments, so as to achieve support for dynamic languages.

  Stack-based instruction set register-based instruction set &&

  java instruction set used is based on the stack, such an instruction set depends on the operand stack work

  advantage:

  Portable: Since registers are provided directly by the hardware, if so the program will be inevitably dependent register hardware constraints

  Compact program code

  Simple compiler

  Disadvantages:

  Slow

  Multi number of instructions: instructions needed to complete the same functionality than the multi-architecture register, because the light stack, the stack has a lot of instruction

  Memory Access and more: Stack frequent access means frequent memory access, and for processors, memory speed is always a bottleneck.

  Java memory overflow exception

  Memory overflow

  When no memory heap allocation and heap to be completed instance can not be extended: java.lang.OutOfMemoryError: Java heap space

  When the method area and memory pool can not meet the constant demand for memory allocation: java.lang.OutOfMemoryError: PermGen space

  Stack unable to apply adequate memory expansion: java.lang.StackOverflowError

  Memory Leak

  Program code design due to dynamically allocated memory is not released, resulting in the portion of memory is not available

  The difference between memory overflow and memory leak

  Memory leaks are one of the causes memory overflow, memory leaks can lead to memory overflow accumulated

  Memory leaks can be solved by improving the code, a memory leak can not be completely avoided, it can only be reduced by configuring the frequency of occurrence

  Memory leak memory leak check

  Performance monitoring tools:

  JProfiler

  Optimizeit Profiler

  Eclipse Memory Analyzer

  EclipseMAT

  JProbe


Guess you like

Origin blog.51cto.com/14503791/2457185