Java Virtual machine

Source: Internet
Author: User

Java memory Area run-time data region

Program counter (Counter Register)

    • A smaller memory space is an indicator of the line number of the bytecode that is being executed by the current thread. The bytecode interpreter obtains the next bytecode instruction by changing the value of the counter, branching, looping, jumping, exception handling, and thread recovery are done by it.
    • Each thread has its own counter, and the counters between the threads do not affect each other.
    • When you execute the native method, the counter does not work and the value is empty (Undefined).
    • This area is the only area where Oom is not specified.

Java VM stacks Java Virtual machine stack

    • belongs to the thread, and the life cycle is the same as the thread belonging to it.
    • The virtual machine stack describes the memory model that the Java method executes:
      • When the method executes, the stack frame is created to store the local variable table , the operand stack, the dynamic link, the method exit, and so on.
      • Method calls to the completion of the process, corresponding to its stack frame from the virtual machine stack into the stack to the process.
    • The local variable table holds the basic data types and object references that the compiler can know, long, double because the length is 64bit, will occupy two slots, others occupy one, so that the local variable table memory space at compile time to complete the allocation, the method runs without changing its size.
    • Two exceptions are specified in the virtual machine stack:
      • Stackoverflowerror: The stack depth of a thread request is greater than the depth allowed by the virtual machine.
      • OutOfMemoryError: When the virtual machine stack dynamically expands memory, it cannot request enough memory.

Local methods stack Native method stack

    • Similar to the virtual machine stack, the difference is that the virtual machine performs Java method services for the virtual machine, while the local method stack works for the native method, and the native method refers to the Java call function of the C + + language.
    • The local method stack area will also throw Stackoverflowerror & OutOfMemoryError exceptions.

Java Stack Java heap

  • The largest chunk of memory in the JVM, shared by all threads, created when the virtual machine is started. Almost all object instances and arrays are born here.
  • The Java heap is the primary area of garbage collection, also known as the GC (garbage collected heap) heap.
  • An Oom exception is thrown when there is no memory allocation instance in the heap and cannot be extended.
  • The Java heap can be in a physically disjoint memory space, as long as it is logically contiguous.

  • memory Breakdown of heap space :

    • Young Generation: The new generation includes Eden, S0 (Fromsurvivor), S1 (Tosurvivor), and since the new generation has a short time to live, this area typically uses the tag-cleanup algorithm GC.
    • Old Generation: An object that survived after multiple GC (each object has an Object age counter), this region uses the General replication algorithm GC; large objects, such as arrays, go directly into the old age .
    • Permanent: Permanent generation, storing immutable objects, classes, methods, strings, etc., see the following method area.
  • Generational recycling of heap memory :
    1. Most of the objects you just created will be assigned to the Eden area, and most of them will soon die out. The Eden Zone is a contiguous memory space, so it is extremely fast to allocate memory on it.
    2. When the Eden area is full, the minor GC is executed, the extinct objects are cleared, and the remaining objects are copied to a surviving area Survivor0 (at this time, Survivor1 is blank, two survivor always have a blank).
    3. Thereafter, each time the Eden area is full, the minor GC is executed once and the remaining objects are added to the Survivor0.
    4. When the Survivor0 is full, the objects that are still alive are copied directly to Survivor1, and after the Eden area executes the minor GC, the remaining objects are added Survivor1 (at this point, the Survivor0 is blank).
    5. when two survival zones have been switched several times (the Hotspot virtual machine defaults 15 times, with-xx:maxtenuringthreshold control, greater than this value entering the old age), the surviving objects (in fact only a small part, for example, our own defined objects), will be copied to the old age.
    6. Summary : The Eden area is a contiguous space, and the survivor is always empty; after GC and replication, a survivor is kept alive, and the contents of the Eden and the other survivor areas are no longer needed, and can be emptied directly. To the next GC, the roles of the two Survivor are interchanged. As a result, this way of allocating memory and cleaning up memory is extremely efficient, and this garbage collection is known as the "Stop-copy (stop-and-copy)" Cleanup (copying the Eden area and an object that is still alive in a survivor to another survivor).

Methods Area method

    • All threads are shared, and aliases no-heap(not heaps) are used to store data such as class information, constants, static variables, and instant compiler-compiled code that have been loaded by the virtual machine. Also called permanent Generation (permanent generation); It is called a permanent generation because the hotspot extends the GC from the heap to the method area. (There is no concept of a permanent generation for other virtual machine IBM J9, etc.)
    • Like the Java heap, it is a region of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine .
    • Not known as a permanent generation, where data is permanent, and GC occurs when type unloading, Chang recycling, but rare.
    • An Oom exception is thrown when this zone cannot allocate new memory.

Running a constant pool runtime Constant pool

    • is part of the method area, in addition to the class file contains the version of the classes, fields, methods, interfaces and other descriptive information, there is a constant pool (Constant pool Table), for the compilation period generated by the various literal and symbolic references, This section is stored in the run-time pool of the class load backward into the method area.
    • An Oom exception was thrown when the memory could not be requested.

Direct Memory

    • Direct memory is not part of the JVM runtime, but Java can introduce channel and buffer I/O through NiO (New input/output), which can use the native method to request out-of-heap memory. This memory is then manipulated through the Directbytebuffer object in the JVM heap, where the request for memory is implemented by the Unsafe.allocatememory method.

Object access

    • Handle Access : A chunk of memory is allocated in the Java heap as a handle pool , the handle address of the object is stored in the reference, and the handle contains the instance data pointer and the type data pointer; The advantage of this approach is that the handle address is stable and the object changes, just modify the handle , and reference does not need to be changed.
    • Direct pointer Access : The Java Heap object holds access type data information, reference directly store object address, the advantage of this approach is fast, saving the cost of pointer positioning.

Java Virtual machine

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.