Java memory area and memory overflow exception

Source: Internet
Author: User

One, Java Virtual machine memory partition

1. Program counter

Thread Private

Can be seen as the line number indicator of the byte code executed by the current thread . The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute.

A Java Virtual machine is implemented through multi-threaded rotation and allocating processor execution time, in order to correctly recover to the execution location after switching threads, each thread has a separate program counter.

2.Java Virtual Machine Stack

The thread is private, the same as the thread's life cycle.

The virtual machine stack describes the memory model that is executed by the Java method : Each method creates a stack frame, where the user stores the local variable table, the operand stack, the dynamic link, the method exit, and so on. Each method executes to the end to indicate a stack frame's entry into the stack.

Local variable table: The Basic data Type (Boolean, byte,char,short,int,float,long,double), object reference, ReturnAddress type that is known during compile time. Long,double occupies two spaces (slots), and other types occupy one.

This area specifies two exceptions: the ① thread requests a stack depth greater than the allowed depth of the virtual machine to throw Stackoverflowerror, and the most current occurrence of this exception is recursion. Simply put, there are too many layers of methods. ② virtual machine Stack if not capacity, when the method is too large, the memory is too large to appear outofmemoryerror.

3. Local Method Stack

Similar to the virtual machine stack, virtual machines perform Java method services for virtual machines, and the local method stack serves the native method used by virtual machines.

4.Java Heap

The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. All object instances and arrays are allocated on the heap.

A OutOfMemoryError exception occurs when there is no memory in the heap to complete the allocation, and the heap space cannot be extended.

5. Method area

A thread-shared area of memory that stores data such as class information, constants, static variables, and compile-time compiled code that have been loaded by the virtual machine.

A OutOfMemoryError exception will occur

6. Run a constant-rate pool

Running a constant pool is part of the method area. Class file: The version, field, method, interface, and other descriptive information of the classes, as well as constant pools: the user holds various literal and symbolic references generated during the compilation period, and this section enters the run-time pool of the method area after the class is loaded.

Second, hotspot virtual machine Object Quest

1. Creation of objects

1.1 Virtual opportunity to new, first check whether the parameter of this directive locates a reference to a class in a constant pool, and whether the reference represents a class that is loaded, parsed, or initialized, and if not, executes the corresponding class-loading process.

1.2 Virtual machines allocate memory for objects

The desired size of the object is determined after the class is loaded. Two scenarios: ① pointer collision: Requires memory to be absolutely structured, assigned on one side, unassigned on the other. The middle holds the pointer as an indicator of the cutoff point. ② Idle list: Records which memory is available. The allocation scheme is determined by the garbage collection mechanism, and there is no compression mechanism.

1.3 The virtual machine initializes the allocated memory control to 0.

1.4 The virtual machine makes the necessary settings for the object.

For example, this object is the instance of the class, how to find the class metadata information, the object's hash code, and the object's GC band age. This information is stored in the object header.

1.5 After the work is done from the virtual machine perspective, the object has been created, but all fields are 0, followed by <init> operations to initialize the object to the programmer's will.

2. Memory layout of objects

Objects in-memory layouts can be divided into 3 regions: Object header, instance data, align fill

  

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.