Talking about Java memory structure

Source: Internet
Author: User

Overview

Memory is where the program runs, and when programming code in the Java language, it is necessary to properly understand the partition and usage of the Java language for memory.

Memory structure (area does not represent the size of memory consumed)

Thread-Private:

    • PC Register
    • Virtual Machine Stack
    • Local method Stack

Thread Sharing:

    • Heap
    • Method area
Action heap for each region (heap)

Describe

    • A heap is a zone shared by all threads in a virtual machine (involving communication between threads).
    • A heap is a run-time data area where all the class instances and arrays of memory are allocated.
    • Heaps are created when a virtual machine is started, the heap of objects is managed by the garbage collector, and objects are not displayed for recycling.
    • Heap memory is not contiguous.
    • The garbage collector focuses on areas.
    • The OutOfMemoryError exception will be thrown if the heap memory has been unable to continue expanding and the instance memory needs to be allocated.

Sub-generational:

    • Cenozoic
      • Eden Zone
      • Survivor0 District
      • Survivor1 District
    • Old age
      • Old Area

Parameters

    • -xms50m # defines the minimum value of the heap
    • -xmx50m # Defining the maximum value of a heap
    • -XMN10M # Set Cenozoic size (Eden Zone + S0 zone + S1 zone)
Method area

Describe:

    • Shared between virtual machine threads.
    • It stores class structure information, such as field and method data, as well as method and constructor code, including running a constant pool, and so on.
    • The method area logically belongs to the heap, but it also has an alias: Non-heap. The GC does not recycle the zone.
    • According to the virtual machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.
PC Register (Program Counter Register)

Describe:

    • Also known as the program counter, is a small amount of memory space.
    • This counter is used to indicate the number of bytecode lines that the current thread executes, such as branching, looping, jumping, exception handling, and thread recovery.

Because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (which is a kernel for a multicore processor) executes only the instructions in one thread, so that, in order for the thread to switch back to the correct execution position, Each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this kind of memory area is "thread-private" memory.

Virtual Machine Stack

Describe:

    • Service for Java methods
    • The stored elements are stack frames (storing local variable tables, action stacks, dynamic links, method exits, and so on).
    • A method from the call to the completion of execution, corresponding to a stack frame in the virtual machine from the stack to the stack process.

The Local variables table holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types, which are not equivalent to the object itself and are implemented according to different virtual machines). It may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive).

The 64-bit length of long and double data takes up 2 local variable space (slots), and the remaining data types occupy only 1. The amount of memory space required for a local variable table is allocated during compilation, and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the local variable table while the method is running.

In the Java Virtual Machine specification, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended (most of the current Java virtual machines can be dynamically extended, However, a fixed-length virtual machine stack is also allowed in the Java Virtual Machine specification, which throws a OutOfMemoryError exception when the extension fails to request enough memory.

Local method Stack

Describe:

    • Similar to virtual machine stacks
    • Servicing the native method

The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, except 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 by the virtual machine. The language, usage, and data structure of the methods used in the local method stack in the virtual machine specification are not mandatory, so the virtual machine can implement it freely. Even some virtual machines, such as sun hotspot virtual machines, combine the local method stack and the virtual machine stack directly. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

Talking about Java memory structure

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.