Java memory area and memory overflow exception

Source: Internet
Author: User
Tags exception handling

Various areas of Java Virtual machine memory

The role of these areas, the service objects and the problems that may arise, respectively. (Image from Network)

1. Program counter

A program counter is a small piece of memory that can be seen as the line number indicator of the bytecode that is being executed by the current thread. The function of bytecode interpreter is to change the value of the program counter to select the next byte code instruction to execute, branching, looping, jumping, exception handling, thread recovery, etc. all need to rely on the program counter to complete.

program counter memory is thread-private memory. because the Java Virtual machine multithreading is implemented by rotating threads and allocating processor execution time, at any one time, a processor executes only the instructions in one thread. So in order to ensure that the thread can be switched back to the correct execution location, each thread has a separate program counter, the individual program counters do not affect each other.

2.Java Virtual Machine Stack

The Java Virtual machine stack is thread-private and has the same life cycle as the thread. The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store local variables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame into the stack and the process of the stack. Where local variables hold the basic data types known at compile time (short, int, long, float, double, Byte, Char, Boolean), Object references and ReturnAddress types (pointing to a bytecode instruction address). The memory space required for the local variable table is allocated during compilation.

3. Local method Stack

The local method stack is very similar to the Java Virtual machine stack, and the difference between them is that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack serves the native method used and is thread-private.

Both the local method stack and the virtual machine stack can throw two kinds of exceptions:

stackoverflowerror: A Stackoverflowerror exception is thrown when the thread requests a stack with a depth greater than the depth allowed by the virtual machine.

OutOfMemoryError: If the virtual machine allows dynamic scaling, the OutOfMemoryError exception will be thrown when the extension cannot request enough memory.

4.Java Heap

The Java heap is an area of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this memory is to hold object instances where almost all of the object instances are allocated memory. as described in the Java Virtual Machine specification, all objects and arrays are allocated on the heap.

Java's heap area is also garbage collection management as long as the area, about garbage collection I was finishing.

5. Method area

A method area is an area of memory that you want to share, which is used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. memory reclamation for this area is primarily for the collection of constant pools and unloading of types.

6. Run a constant-rate pool

running a constant pool is part of the method area . class file In addition to the type of typesetting, fields, methods, interfaces and other descriptions. Another piece of information is a constant pool, which holds the various literal and symbolic references generated by the compilation period , which will be loaded into the run-time pool of the approach area when the class loads.

Java memory area and memory overflow exception

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.