Java memory area and memory overflow exception

Source: Internet
Author: User

1: data area for Java Virtual runtime

Program Counter : is a small piece of memory that can be viewed as the line number indicator of the byte code executed by the current thread . BYTE-code interpreter work is to choose the next one by changing the value of this counter to execute the bytecode instruction, branch loop jump exception processing thread reply, etc. will depend on this counter complete.

The Java Virtual machine multithreading is implemented by rotating the threads to allocate processor time slices, because each thread needs to have a separate program counter for the switch to be able to return to the correct location, and each thread has to be independently stored. This block of memory is thread-private memory.

Java Virtual machine stack: Execute Java method for virtual machine, i.e. byte code service

Thread private, life cycle and thread are the same.

Describes the memory model that is executed by the Java method: Each method executes with a stack frame that stores local variables, operands, dynamic links, method return addresses, and so on. Each method from call to execution, corresponding to a stack frame in the virtual machine stack into the stack to the process.

Where the local variable table holds the compiler to know the various basic data types, object reference type, and so on, the table needs to allocate the memory space during compilation, when entering a method, the method needs to allocate in the frame how much local variable space is fully determined, the method will not change the size of the local variable table during operation.

What people often say is that stack memory refers to a virtual machine stack or a local variable table in a virtual machine stack.

Heap: The largest chunk of memory managed by the JVM.

The Java heap is shared by all threads, created at virtual machine startup, and the only purpose is to hold object instances where almost all object instances allocate memory.

The heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

Currently the heap is extensible:-xmx and-XMS control (specifically, refer to JVM tuning)

Method Area:

is an area of memory shared by each thread that stores the class information, constants, and static variables that have been loaded by the virtual machine, even after the compiler compiles the code . The JVM specification describes it as a logical part of the heap, but non-heap

To run a constant-rate pool:

is part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool, for the compilation period generated by a variety of literal and symbolic references, which will be loaded in the class load into the method area of the run constant pool storage .

The constant pool runs relative to the class file is dynamic, such as The Intern () method of the String class.

Public native String intern ();
The document tells us that the method returns an intrinsic reference to a string object.
It is well known that the String class maintains an object pool of initially empty strings, and when the Intern method is called, returns an instance of the object pool if the object pool already contains an equal string object, otherwise adds the string to the object pool and returns a reference to the string.
From the point of view of the program, we assume that there are two strings s1,s2, when s1.equals (S2), S1.intern () ==s2.intern (), which means that the two strings use the same instance in memory.

Intern is a native method, but according to its documentation, the JVM maintains a hash table of strings that the current process has ever seen, and queries the table when intern is called. If it already exists, the reference to the string is returned directly, and if not, one is created and added to the hash.

The words read from the file, so only after reading the word, the next intern for each word is stored, you can guarantee that the same content of the string only one copy of the storage. More specifically, there is only one object. That is, there is only one copy of the string content, and its overhead (the non-content part of the string class, such as other member variables) is only one copy.

In addition, if you use literal (literal) to define a string, intern is automatically called, reducing memory consumption.

Direct memory: Allocations are not limited by the Java heap size

2: Creation of objects

( clone, deserialize, dark copy )

When a virtual opportunity comes to a new instruction, the parameter of the digital check checks whether the parameters of this directive can be located in a constant pool to a symbolic reference of a class, checking that the symbol reference is loaded, parsed, and initialized. If not, the class's loading process is performed first.

After the check is loaded, the virtual machine allocates memory for the new object, and the size of the memory required by the object is fully determined after the class is loaded. Allocating space to an object is equivalent to dividing the size of a chunk of memory from the Java heap. If the heap is structured in memory, the "pointer collision" is used, and if it is not structured then the "Idle list". Whether or not it is structured depends on whether the garbage collector used has a compression finishing function.

After the memory is allocated, the virtual machine will be allocated to the memory space is initialized to 0, then the JVM according to the object header to make the necessary settings, if the bytecode following the invokespecial instruction, after the execution of the new instruction will then execute the <init> method, the object is initialized.

3: Memory layout of objects

The layout of objects stored in memory can be divided into 3 areas: object header, instance data, align fill

The object header includes two pieces of information, the first part is used to store the object's own run-time data, such as hash code, GC generational age, lock status flag, thread holding lock, bias thread ID, etc.

He was designed as a non-stationary data structure to store as much information as possible in a very small space, reusing its own storage space based on the state of the object.

The other part is the type pointer, which is a pointer to the class metadata that the object executes, and the virtual machine uses a pointer to determine which class of the object is an instance of.

4: Object access positioning

Java programs manipulate the concrete objects on the heap by reference data from the variable tables in the virtual machine stack. Currently the main way of access is to use handles and direct pointers.

The advantage is that reference is stored in a stable handle address, if the garbage collection when the object is moved, only changes in the handle, the instance data pointer, reference itself without modification

Fast, one-time address direct location

memory leaks, also known as "storage Leaks", are dynamically opened up with dynamic storage allocation functions and are not released after they have been used, resulting in the deposit element being occupied. Until the end of the program. (In fact, the memory space is not recycled after use) is called a memory leak .

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.