[In-depth understanding of Java Virtual Machines]< reading notes >

Source: Internet
Author: User

Overview
    • Approaching Java: Introducing the history of Java
Part II: Automatic memory management mechanism

The programmer gives the power of memory control to the Java Virtual machine, allowing automatic memory management when encoding. But on the other hand, if there is a memory leak and overflow problems, you need to understand some of the underlying knowledge to do the wrong troubleshooting.

    • Automatic memory management mechanism: Describes how memory is divided .
    • Garbage collector and memory allocation policy: Analyze garbage collection algorithms .
    • Virtual machine performance monitoring and fault handling tools
    • Tuning case study and actual combat
Part III: Virtual Machine execution Subsystem
    • Class file system: Describes the various components of the class file structure .
    • Virtual machine class loading mechanism: Describes the various stages of the class loading process .
    • Virtual machine byte code execution engine
    • Class loading and execution subsystem cases and actual combat
Part IV: Program compilation and code optimization

Java program compiled from the source code into bytecode and bytecode compilation cost of machine code two process, add up is equivalent to a traditional compiler executed by the compilation process.

    • Early (compiler) Optimizations : Analyze the causes and consequences of multiple syntactic sugars , such as generics, active unboxing and boxing, and conditional compilation.
    • Advanced (Runtime) optimization : Introduce the Hotspot detection method of virtual machine, instant compiler of hotspot and so on.
Part V: Efficient concurrency
    • Java Memory models and threads
    • Thread Safety and lock optimization
Go near Java
    • Java Technology System:
      • Java programming language
      • Java virtual machines on a variety of hardware platforms
      • class file format
      • Java API Class Library
      • Third-party Java class libraries

Where theJava language + JVM + API Class Library = JDK (Java development Kit). The JDK is the smallest environment that supports Java program development.

Automatic memory management mechanism Java memory area and memory overflow exception 1. Run-time data region

The JVM divides the memory it manages into several different data areas when it executes a Java program. each region has its own purpose, as well as the creation and destruction times .

  • Program Counter: A small amount of memory space that can be seen as The line number indicator of the bytecode that the current thread executes . The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, branching, looping, jumping, exception handling, thread recovery, etc. all depend on the counter. The multithreading of the
    • JVM is implemented in a way that threads rotate and allocate processor execution time, and at any one time, a processor executes instructions in only one thread. Therefore, in order to return to the correct execution location for thread switching, Each requires a separate program counter .
    • If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed, and if the native method is executed, the counter value is empty.
    • The memory area is .
  • Java Virtual machine stack:
    • like a program counter, . The
    • JVM stack describes the memory model that is executed by the Java method. Each method executes at the same time creating a stack frame, which is used for , operand stacks, dynamic links, method exits, and so on.
    • Each method from the call to the completion of the process, corresponding to a stack frame in the JVM stack into the stack of the process .
    • Many times, we often say that Java memory is divided into heap and stack, this method is rough, this refers to the stack is actually the local variable table described above, because this is the most attention of the programmer.
    • (note that this is not the same as the object itself, it may be a reference pointer to the start address of the object, It is also possible to point to a handle that represents an object or other location related to this object. ) and the ReturnAddress type.
    • The memory space required for the local variable table is allocated during compilation , when entering a method, the method needs to allocate the local variable space in the frame is fully determined, The size of the local variable table is not changed while the method is running.
    • in the JVM specification, two exceptions are specified for the zone:
      • If the thread requests a stack depth greater than the depth--> stackoverflowerror exception that is run by the virtual machine;
      • if the JVM stack can be dynamically extended (most of the current JVMs can be dynamically extended, but the JVM specification also runs a fixed-length JVM stack), the OutOfMemoryError exception is thrown if sufficient memory is not available for the extension.
  • Local method Stacks: Similar to JVM stacks, the main difference is that the JVM stack does not have a JVM executing Java methods (that is, bytecode) services, while the local method stack is the native method service used by the JVM.
  • Java heap: For most applications, the heap is the largest chunk of memory managed by the JVM.
    • The heap is shared with all threads and is created when the JVM starts .
    • The only purpose of this memory area is to hold object instances where almost all objects allocate memory.
    • The heap is the primary area that the garbage collector manages. From the memory recycling point of view, since the current collector basically uses the Generational collection algorithm, so the Java heap can also be subdivided into the new generation and the old age .
    • According to the JVM specification, the heap can handle physically discontinuous memory space as long as it is logically contiguous. Can be either fixed-sized or extensible.
    • A OutOfMemoryError exception is thrown when there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.
  • Method Area: A memory area shared by multiple threads 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 .
    • Although the JVM specification describes a method area as a logical part of a heap, it has an alias called Non-heap.
    • The JVM has a very loose limit on the method area, and you can choose not to implement garbage collection in addition to the heap, which does not require contiguous memory and optional fixed size or extensibility.
    • The memory reclamation targets for this zone are primarily for the collection of constant pools and for unloading types.
    • May throw OutOfMemoryError exceptions.
  • run a constant pool: is part of the method area .
    • In addition to descriptive information such as the version, field, method, and interface of the class file, there is also a constant pool that holds the various literal and symbolic references generated by the compiler , which is stored in the run-time pool where the class loads the backward into the method area.
    • A key feature of the runtime constant pool relative to the class file constant pool is its dynamic nature . That is, you can put new constants into the pool during run time. The most common is the Intern () method of the String class.
  • Direct Memory
    • Direct Memory is not part of the JVM Runtime data area, nor is it a memory area defined in the JVM specification.
    • However, this part of memory is also used frequently, which can also lead to outofmemoryerror.
    • NiO class: An I/O method based on channel and buffer (buffer) is introduced to directly allocate out-of-heap memory directly using the native function library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap. (This can significantly improve performance because it avoids copying data back and forth between the Java heap and the native heap.) )
    • In summary, the allocation of direct memory is not limited by the Java heap size, but is still subject to the native total memory limit and may still be thrown outofmemoryerror.
2. Hotspot Virtual Machine Object Quest
    1. Object creation: When a virtual machine encounters a new command:
      1. [Class Load Check]: first check that the parameter of the directive is able to locate the symbol reference of a class in a constant pool, and check that the class represented by the symbol reference has been loaded, parsed, and initialized.
      2. [Allocate memory for new objects]: The size of the memory required by the object is fully deterministic after the class is loaded (a follow-up describes how to determine it). Allocating memory to an object

[In-depth understanding of Java Virtual Machines]< reading notes >

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.