Deep understanding of JVM storage area and memory overflow

Source: Internet
Author: User

Article Directory
  1. 1. Java memory area and memory overflow exception
    1. 1.1.  run-time data region
      1. 1.1.1.  Program counter
      2. 1.1.2. java virtual machine stack
      3. 1.1.3.   local method stack
      4. 1.1.4. java Heap (Java heap)
      5. 1.1.5.  method area
      6. 1.1.6.  run a constant pool
      7. 1.1.7.  Direct memory
    2. 1.2. Hotspot Virtual Machine
      1. 1.2.1. creation of Objects
      2. 1.2.2. Accessing and Locating objects
    3. 1.3. How to solve the oom anomaly
    4. 1.4. Reference
Java memory area and memory overflow exception run-time data region

Program counter
    • The line number indicator of the byte code executed by the current thread
    • Current thread Private
    • There will be no outofmemoryerror situation
Java Virtual Machine stack
    • Thread private, life cycle is the same as thread
    • The memory model executed by the Java method, where each method executes creates a stack frame that stores local variable tables (basic types, object references), operand stacks, dynamic links, method exits, and so on.
    • Stackoverflowerror exception: The stack depth requested by the thread is greater than the allowed depth of the virtual machine
    • OutOfMemoryError exception: Unable to request sufficient memory if the stack is extended
Local method Stack

Similar to virtual machine stack, the native method service is mainly used by virtual machine, and the local method stack and virtual machine stack are directly in the hotspot virtual machine.

Java heap (Java heaps)

The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area is to store object instances. The Java heap is the primary area of garbage collector management. Java heap can also be subdivided into: the new generation and the old age. In the finer point there is Eden Space, Form survivor space, to survivor space and so on.

    • The size of the heap can be controlled by-XMX and-XMS
    • OutOfMemoryError exception: When there is no memory in the heap to complete the instance assignment, and the heap can no longer be expanded.
Method area
    • Sharing between threads
    • Used to store data such as class information, constants, static variables, immediate compiler-compiled code, etc. loaded by the virtual machine
    • OutOfMemoryError exception: When the method area does not meet the allocation requirements for memory
Run a constant-rate pool
    • Part of the method area
    • A variety of literal and symbolic references that are used to store compile-time builds
    • OutOfMemoryError exception: When a constant pool is no longer available for memory
Direct Memory
    • NiO can use the native library to directly allocate out-of-heap memory, and the Directbytebuffer object in the heap operates as a reference to this memory
    • Size is not limited by Java heap size and is subject to native (server) memory limitations
    • OutOfMemoryError exception: When system memory is low
Creation of hotspot Virtual machine objects

Virtual opportunity to a new directive, first checks whether the object's arguments are anchored to a class's symbolic reference in a constant pool, and checks whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, you must first perform the class's load process.
After the class load check passes, the virtual machine allocates memory for the new object. The desired memory size of the object is then determined after the class load is complete. Memory allocations can take the form of "pointer collisions" and "idle lists".

Access positioning of objects

Java programs need to manipulate the concrete objects on the heap through the reference data on the stack. Access is handled using both a handle and a direct pointer.

    • A handle to the Java heap will be partitioned into a block of memory to be used as a handle pool, where the handle address of the object is stored, and the handle contains the specific address information of the object instance data and the type data reference
    • Direct pointer access to the Java Heap object layout must consider how to place information about the access type data, which is the object address stored in the reference
The solution of Oom anomaly

To generate a dump Snapshot file:

    • The JVM parameter-xx:-heapdumponoutofmemoryerror allows the JVM to dump the current memory dump snapshot in the event of a memory overflow
    • Using Jmap to produce the dump file, win through the Task Manager to view the Tomcat process pid,linux with the PS command to view the process PID, and then use the Jmap command

Analysis is performed through memory image analysis tools, such as Eclipse's memory Analyzer, which are commonly seen in the following scenarios:

    • Memory leaks, objects are dead, can not be automatically recycled through the garbage collector, by identifying the location and cause of the leaked code, to determine the solution;
    • Memory overflow, objects in memory must also exist, which means that the Java heap allocation is insufficient, check the heap size (-XMX and-XMS), check the code for the existence of an object life cycle too long, holding state too long.

An Oom exception example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
Package oom;

Import java.util.ArrayList;
Import Java.util.List;

/**
* VM Args:-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror
* @ClassName: Heapoom
*
*/
public class heapoom {
static class oomobject{

}

public Span class= "literal" >static void main (string[] args) {
list<oomobject> List = new arraylist<oomobject> ();
while (true) {
List.add ( span class= "keyword" >new oomobject ());

/span>

Reference

Deep understanding of Java Virtual Machine JVM advanced features and best practices

Deep understanding of JVM storage area and memory overflow

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.