Deep-Dive Java Virtual machine--JVM memory

Source: Internet
Author: User

In C + +, programmers have ownership of each object, but at the same time shoulder the responsibility of releasing the object's memory space, while Java has the help of a virtual machine, the programmer has ownership of the object and no longer need to free the object's memory space. Because the JVM automatically frees up object memory, memory leaks and memory overflow problems are rare.

The Java Virtual machine divides the memory space into 5 parts at run time, namely: Method area, virtual machine stack, local method stack, heap, program counter.

Program counter
    • Essence
      The program counter is essentially a small piece of memory space.

    • Role
      You can simply think of the program counter as the line number indicator of the byte code executed by the current thread.
      The bytecode interpreter selects the next byte-code instruction to be executed by changing the value of the program counter while it is working.
      In addition, the program's branches, loops, jumps, exception handling, thread recovery and other basic functions need to rely on program counters to complete.

    • Characteristics
      The program counter is a thread-private memory. Each thread has a private program counter that records where the thread is currently executing so that the thread resumes execution knowing where it was last executed. So the counters between the various threads are independent and complementary to each other.
      If the thread is executing a Java method, then when the program counter of the front process logs the address of the executing virtual machine bytecode instruction, the counter value is empty if the thread is executing a native method.
      The Ps:native method refers to methods that are not written in the Java language, or that are written in the Java language to directly manipulate computer hardware.

Java Virtual Machine stack
    • What is it?
      The JVM stack is a memory model used to describe the execution of Java methods.
      A stack frame is stored in the JVM stack, and each stack frame corresponds to a Java method.
      When a Java method is executed, the JVM creates a stack frame in the JVM stack for storing information such as the local variable table, the operand stack, the dynamic link, the method exit, and so on.
      Each Java method is called to the completion of the process, all corresponding to the stack frame in the JVM stack in the stack and out of the stack.

    • Characteristics
      The JVM stack is thread-private and each thread has a separate JVM stack, and the method to be executed in that thread creates a stack frame in the JVM stack it corresponds to.

    • Commonly referred to as "heap" and "stack"
      Some of the memory used by the JVM is divided into stacks of memory and heap memory, this method is more coarse.
      The "stack" in this division is actually just a table of local variables in the stack frame of the JVM stack.

    • Table of local variables in stack frame
      The local variable table holds various basic data types, object references, and returnaddress types that are known during compile time.
      The amount of memory space required for a local variable table is allocated at compile time, and the local variable table required for a method is fully deterministic, and the size of the local variable table does not change when the program is run.
      PS: Object reference is also called reference type, its essence is an address, not the object itself, different virtual machines this address points to the same content; The pointer may point to the starting address of an object, or it may point to a handle representing an object.
      The ps:returnaddress type is essentially an address, which points to a byte code execution.

    • Possible exceptions for Java Virtual machine stacks
      The StackOverflow exception is thrown if the thread request has a stack depth greater than the maximum allowed depth of the virtual machine.
      If the virtual machine stack can be dynamically extended without the maximum depth limit, the OutOfMemory exception is thrown when the memory is exhausted and the stack depth cannot be extended.

Local method Stack

The characteristics of the local method stack are almost identical to those of the JVM stack, except that the JVM stack holds information about the Java method, and the local method stack holds information about the local method.
The local method stack also throws OutOfMemoryError and StackOverflow exceptions.

Heap
    • What is it?
      The only purpose of heap memory is to store all object instances.

    • Characteristics
      Heap memory is the largest piece of memory in memory required by a Java virtual machine.
      It is an area of memory that is shared by all threads.
      Heap memory does not have to be contiguous physically, it only needs to be logically contiguous.

    • What is the relationship between heap and garbage collection?
      Heap memory is the primary area of garbage collection, so it is also called a GC heap.

    • Heap may throw an exception
      An OutOfMemoryError exception occurs when the instances in the heap are useful and the memory is exhausted.

Method area
  • What is it?
    The method area is also an area of memory that the JVM needs to use to store information about classes that have been loaded by the JVM, constants, static variables, compiled code, and so on.

  • Characteristics
    The method area and the heap are areas of memory shared by each thread.
    It does not physically require contiguous memory space.
    The size of the method area can be fixed, or can be extended.
    The method area can not implement garbage collection.

  • Can not implement garbage collection, then the data in the method area will exist permanently?
    No. Garbage collection behavior in the method area is relatively rare, but not the data entry method area is permanent.
    The primary goal of the method area's memory reclamation is to recycle the constant pool and unload the type.

  • Exceptions that may be thrown by the method area
    The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

  • Run a constant-rate pool
    A) what is it?
    Running a constant pool is part of the method area.
    We know that. In addition to the. class file that is generated after the Java file is compiled, there is a constant pool that holds the various literal and symbolic references generated during the compilation period, except for the classes ' versions, fields, methods, interfaces, and so on. All the contents of a constant pool in a class file are stored in the run-time pool of the method area after the class is loaded.
    Ps:int age = 21;//age is a variable that can be assigned, and 21 is a literal constant that cannot be assigned to a value;
    int final Pai = 3.14;//pai is a symbolic constant that cannot be modified once it has been assigned.

    b) Features
    The constant pool in the. class file is dynamic. Java does not require constants to be generated at compile time and cannot be increased at runtime; Java allows new constants to be placed in the run-time pool of the method area during runtime.
    The Intern method in the string class in Java is based on the dynamic nature of the run-time-constant pool. When the Intern method is called, the string in the pool is returned if the pool already contains a string equal to this string object. Otherwise, this string object is added to the pool and a reference to this string object is returned.

    c) Exceptions that may be thrown
    Running a constant pool is part of the method area, so it is limited by the memory of the method area, so the OutOfMemoryError exception is thrown when the const pool is no longer available for memory.

Direct Memory

Direct memory is not an area of memory defined in the JVM specification, but is frequently used during the actual operation of the JVM. It can also cause outofmemoryerror anomalies.
The new nio=new Input/output class was added to JDK 1.4, introducing a channel-and buffer-based IO approach, which can allocate out-of-heap memory directly using local functions. The data in the out-of-heap memory is then manipulated using a Directbytebuffer object stored in the heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and the native heap.
Direct memory is not limited by the Java heap size, but is still limited by the native total memory.

Deep-Dive Java Virtual machine--JVM memory

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.