1. The JVM

1.1 Java history

Oak James. Gosling wrote once run Anywhere.

1995.5 Oak  Java1.0 Write once Run anywhere 1996.1 JDk1.0 JVM Sun Classic VM 1996.9 First JavaOne Conference 1997.2 JDk1.1 Internal classes, Reflection JAR file formats, JDBC, Javabeans, Rmi 1998 1.2 J2SE j2EE j2ME Swing JIT Hotspot VM 2000.5 JDk1.3 Timer Java2D 2002 2 JDK1.4 Struts, Hibernate, Spring 1.x regular expression Nio Logging Xml parser 2004.9 JDK1.5 Tiger Autoboxing unboxing, Generics, annotations, enumerations, variable length parameters, Enhanced for loop Spring 2.x Spring4.x 2006 JDk1.6 Java EE Java Se javaMe JDK6 provides scripting language support provides compilation apis and HTTP server apis 2009 JDk1.7 acquired SUN 7.4 billion 2011 jdK1.7 2014.3 JDK1.8Copy the code

Goto is reserved keywords but not used.

1.2 New JDK8 features

  • The default and static methods of the interface

Java 8 expands the meaning of interfaces with two new concepts: default methods and static methods. The default method allows developers to add a new method to an existing interface without breaking binary compatibility, that is, it does not force classes that implement the interface to also implement the new method.

  • Lambda expressions and functional programming
  • Date API
  • Repeated notes
  • Better type inference

The Java 8 compiler has made significant improvements in type inference, allowing the compiler to deduce the data type of a parameter in many scenarios, resulting in cleaner code

  • Nashorn JavaScript engine
  • The JVM changes
    • useMetaspace(JEP 122) instead of PermGen space.
    • For JVM parameters, use ** -xx :MetaSpaceSize and -xx:MaxMetaspaceSize** replaces the original -xx :PermSize and -xx :MaxPermSize.

1.3 Java Running Mechanism

1.4 the JVM products

  • Sun Classic VM

In jdk1.1, the world’s first commercial Java virtual machine can only execute Java code in a pure interpreter mode. The interpreter and compiler cannot work at the same time.

  • Exact VM

Exact Memory Management is a trial of hybrid compiler and interpreter work and two-level just-in-time compilers released in jdk1.2 only on Solaris, not Linux and Windows (Linux and windos are now available for Sun) Classic VM) hero short of breath

  • HotSpot VM

Currently running the most widely. It wasn’t originally developed by Sun. It was acquired by Sun in 1998. Hot code techniques. Through counters,2006 open source

  • KVM (Kilobyte)

Kilobyte is simple, lightweight, highly portable and runs on mobile platforms

  • JRockit

BEA,2008, the world’s fastest Java VIRTUAL machine focused on server-side applications, advantages: garbage collector MissionControl service suite BEA JRockit MissionControl, used to diagnose leaks and identify root causes. This tool has very little overhead, so it can be used to find memory leaks in systems in production. The BEA JRockit Mission Control (JRMC) was released in December 2005 and comes bundled with the tool suite starting with JRockit R26.0.0, currently 2.0.1. It is a set of tools for monitoring, managing, and analyzing applications in a production environment with minimal overhead. It includes three separate applications: Memory Leak Detector, JVM Runtime Analyzer, and Management Console.

  • J9

IBM Technology for Java Virtual Machine IT4j

  • Azul VM

High-performance Java virtual machine

  • Liquid VM

High-performance Java virtual machine

  • Dalvik VM

Google,Dex dalvik Executalbe

  • Microsoft JVM
  • Taobao VM

Deeply customized based on hotspot

2.Java memory area

2.1 Java Memory Area – Introduction

2.1.2 overview

  • Heap memory:

All reference object types created using the Java language are stored here. It is managed by the Garbage Collection (GC), for example, freeing the memory occupied by objects no longer referenced by the program.

  • Stack memory:

In contrast to Heap, Stack holds the underlying data types. Such as: int, char, etc. The execution order of the program controls the order in which variables enter and exit the stack, rather than the GC controlling the management of stack memory.

  • Perm:

Used to store metadata for classes. Such as: class definition, method definition and so on.

Perm’s life cycle is bound to the JVM, while Heap’s life cycle is bound to the program.

2.2 Java Memory Area -Java VM Stack

2.2.1 profile

  • Virtual machine stack: Describes the dynamic memory model of Java method execution. The virtual machine stack holds stack frames in and out.
  • Thread-exclusive: Accompanying methods, so exclusive.
  • Stack frames: A method generates stack frames one by one, following the method from creation to completion. Used to store local variable tables, operand stacks, dynamic links, method exits, etc. If a method calls another method, the other method calls the stack frame, and then that method goes on the stack, and then when it’s done it goes off the stack and then it’s destroyed, and then finally the original method completes and then it goes off the stack.
  • The local variable table stores various basic data types known at compile time, reference types, and returnAddress types.

The memory space of the local variable table is allocated at compile time. When entering a method, the amount of memory that the method needs to allocate during the frame is fixed. The size of the local variable table does not change during the method execution. Reference type :user object,name attribute (String), local variable inch is only the object reference, the size of the reference is not changed.

  • The size of the stack
    • There are a lot of stack frames to put in, so it may not fit, and then StackOverflowError(stack memory overflow, such as during recursive calls)
    • If the size of the stack exceeds memory, OutOfMemory will be generated.

Java Virtual machine runtime stack frame structure

2.2.2 Stack Frame

  • Stack Frame is the data structure of method invocation and method execution during virtual machine execution. It is the constituent element of virtual Stack data area.

  • For each method execution, a stack frame is created, accompanying the method from creation to completion. Each method from call to method return corresponds to a stack frame in and out of the stack process, stack frame out of the stack destruction.

  • The size of the local variable table and the depth of the operand stack that each stack frame needs to compile the program’s Code are determined and written into the Code attribute, and the amount of memory required for each stack frame at a time is not affected by the program’s runtime variable data, but only depends on the specific virtual machine implementation.

  • Method calls in a thread can be very long and many methods are in execution state. For the execution engine, only the Stack Frame at the top of the Stack is valid, called the Current Stack Frame, and the Method associated with it is called the Current Method.

  • A typical Stack Frame consists of Local Stack Frame, Operand Stack and Dynamic link

Linking, Return Address (method exit)

2.2.2.1 Scale of local variables

  • Function: Store various basic data types known at compile time, reference type, returnAddress type.

  • The memory space of the local variable table is allocated at compile time. When entering a method, the amount of memory that the method needs to allocate during the frame is fixed. The size of the local variable table does not change during the method execution.

  • A local scale is a storage space for a set of variable values, used to store method parameters and local variables. Max_locals in the Code property of the method table of the Class file specifies the maximum size of the local variable table required by the method.

    • A Variable Slot is the smallest unit of a local Variable table. There is no mandatory size

32 bits, although 32 bits is enough for most types of data. A Slot can hold Boolean, byte, CHAR, short, int, float, Reference, and returnAddress. Reference refers to a reference to an object instance, which can be used to obtain the index of the starting address of the object stored in the Java heap and the type information of the data type in the method area. ReturnAddress refers to the address of a bytecode instruction. For 64-bit long and double variables, the virtual machine allocates two consecutive Slot Spaces.

  • A VM uses a local change table based on index location. The local variable table stores method parameters and local variables. When calling a method that is non-static, the Slot in the zeroth index of the local variable table defaults to passing a reference to the instance of the object to which the method belongs, that is, the object to which the “this” keyword points. After the method parameters are allocated, local variables defined inside the method are allocated in turn.

  • To save stack frame space, slots in the local variable table can be reused. When some variables are out of scope, the Slot corresponding to those variables can be given to other variables. This mechanism can sometimes affect garbage collection behavior.

public static void main(String[] args){ { byte[] placeholder = new byte[64*1024*1024]; } System.gc(); } Run result: [Full GC 378K->378K(15872K), 0.0603803 secs] [Full GC 66093K->65914K(81476K), Public static void main(String[] args){{byte[] placeholder = new byte[64*1024*1024]; } int a = 0; System.gc(); } Run result: [Full GC 602K->378K(15872K), 0.0018270 secs] [Full GC 378K->378K(15872K), 0.0057871 secs] 0.0054067 secs]Copy the code

2.2.2.2 Operand stack

  • An Operand Stack, also known as an operation Stack, is a last-in, first-out Stack. Max_stacks in the Code attribute of the Class file specifies the maximum stack depth during execution. The interpretation execution engine of the Java virtual machine is called “stack-based execution engine”, where the stack refers to the operand stack.

  • The operand stack is used to perform arithmetic operations during method execution or to call other methods to pass arguments.

  • In the conceptual model, the two stack frames are independent of each other. But most virtual machine implementations are optimized so that the two stack frames overlap. Overlap the bottom partial operand stack with the top local table of variables so that a portion of the data can be shared during method calls without additional parameter copying

2.2.2.3 Dynamic Connection

  • Each stack frame contains a reference to the method that the stack frame belongs to in the execution runtime constant pool. This reference is held to support Dynamic Linking during method invocation.

  • The Class file holds a large number of symbolic references, and the method invocation instructions in bytecode take symbolic references to methods in the constant pool as arguments. Some of these symbolic references are converted to direct references during class loading or the first time they are used, which is called static resolution. The other part is converted to a direct reference during each run, called the dynamic join

2.2.2.4 Method Return address

  • When a method starts executing, there are only two ways to exit the current method:

    • When the execution encounters a return command, the return value is passed to the upper-level Method caller, known as the Normal Method Invocation Completion. Generally, the caller’s PC counter can be used as the return address.
    • When an exception is encountered and the Method is not processed in the current Method, the Method exits. There is no return value, called the Abnormal Method Invocation Completion, and the return address is determined by the exception handler table.
  • When the method returns, there are three possible operations:

    • Restores the local variable table and operand stack of the upper method
    • Pushes the return value onto the operand stack of the caller’s stack frame
    • Adjust the value of the PC counter to point to an instruction following the method call instruction

2.2.2.5 Additional Information

  • The virtual machine specification does not specify what additional information a particular virtual machine implementation contains; this part is entirely up to the implementation. In practical development, dynamic linkage, method return address and additional information are generally grouped together as stack frame information

2.2.3 size

StackOverflowError If the stack is large and automatically expands, OutOfMemory may be reported

2.2.4 supplement

  • Stack memory by the user to the system application, the applicant for management.

2.3 Java Memory Area – Local method stack

2.3.1 overview

  • Thread exclusive
  • The hotspot VIRTUAL machine combines the local method stack with the virtual machine stack
  • The virtual machine stack services the execution of Java methods for virtual machines.

The native method stack performs native method services for virtual machines.

Java memory area – local method stack

2.3.2 Java Virtual Machine stack vs. Local Method Stack

Java virtual machine stack vs. native method stack

2.4 Java Memory region – Program counter

Program Counter Register:

  • Is the line number indicator of the bytecode file (class) executed by the current thread.

In virtual machines, the bytecode interpreter selects the next bytecode instruction to execute by changing the value of the counter. Branch, loop, jump, exception handling, and thread recovery all require program counters. Holds the address of the bytecode instruction being executed. If native methods are executed, this counter is undefined.

  • Thread the exclusive

Because the processor executes instructions in only one thread at a certain time, the trace of execution is recorded through the counter after the thread switch, so it can be seen that the program counter is private to each thread

  • Small footprint, negligible, does not cause OutofMemoryErrors (in the JVM)

This area is the only one that does not specify any OutOfMemoryError cases in the Java Virtual Machine specification. Because the developer does not handle this area, it is maintained internally by the VIRTUAL machine.

2.5 Java Memory Area – Heap

2.5.1 Overview of Heap Memory

  • The main area managed by the garbage collector, where the collection is most efficient
  • In simple terms, heap memory is used to hold objects and arrays created by new, and memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector.

2.5.2 Heap Memory Classification

  • The new generation
    • Eden the garden of Eden
    • [/s] the team in the “Survivor0” group is a Survivor.
  • Old Tenured Space

The purpose of sorting is for recycling.

2.5.2.1 Cenozoic

  • All newly created objects are first placed in the Young Generation memory area. If the Young Generation memory is full, Garbage Collection is executed. This type of GC is called a Minor GC.

  • The Young Generation area is divided into three parts: Eden Memory, Survivor0 Memory(S0), Survivor1 Memory(S1).

  • Most new objects are stored in Eden Memory.

  • If Eden Memory is full, GC is performed. Also move ungc objects to S0 or S1. The Minor GC also checks and moves objects in S0 and S1 at this point. And then I’m going to make S0 and S1 null.

  • Objects that remain ungc after multiple GC sessions are moved to the Old Gen memory area. Usually Object is set to a polling threshold by GC.

2.5.2.2 old s

  • The Old Gen memory area holds objects that cannot be GC after several Minor GC’s.

  • As with Young Gen, a GC operation is performed when the Old Gen is full, which is called the Major GC. It also takes a relatively long time

  • Stop – the – world events

Both Young Gen and Old Gen can actively trigger stop-the-world events to suspend all tasks and perform GC operations. Pending tasks will resume execution only after THE GC completes.

  • In most cases, GC performance tuning refers to reducing the time of GC execution in stop-the-world.

2.5.3 Heap Memory Is Abnormal and configured

  • OutOfMemory

  • -Xms

Set the Heap size at JVM startup

  • -Xmx Sets the maximum Heap size,For setting the maximum Heap size.

  • -Xmn Sets the memory size of Young Gen

  • -xx :PermGen Sets the initial size of the PermGen memory

  • -xx :MaxPermGen Sets the maximum value of PermGen memory

  • -xx :SurvivorRatio Sets the memory size ratio of Eden Gen to S0 Gen and S1 Gen. Default value: 8

For example, if Young Gen is 10M, -xx :SurvivorRatio=2. Then: The size of Eden Gen is 5, S0 and S1 are 2.5 respectively

  • -XX:NewRatioSet the value of Old Gen/Young Gen. Default: 2

2.6 Java memory area – Method area

2.6.1 Overview of method areas (HotSpot Persistent Generation)

  • The HotSpot VIRTUAL machine chooses to extend GC generation collection to the method area, or to implement the method area with persistent generation.

The goal :HotSpot’s garbage collector can manage this part of memory as well as the Java heap, eliminating the need to write memory-management code specifically for the method area.

  • This is not the case in the virtual machine specification and other virtual machines.

  • Perm Gen(Permanent Generation,JDK1.8) ===> Metaspace (Metaspace,JDK1.8)

2.6.2 Function of method area

  • The Perm Gen area is a special JVM memory area because it is used to store application metadata (application metadata; A Class may not be part of the Java language, or it may be.

Data such as: class information, constants, static variables, just-in-time compiler compiled code, class version, fields, methods, interfaces, and constant pools.

  • Perm Gen is used by the JVM during application runtime, based on the classes used by the application.
  • Classes in the Java SE package are also included in Perm Gen.

2.6.2.1 Runtime Constant Pool (Runtime Constant Pool)

The constant pool holds the various literal and symbolic references that are generated at compile time, which go into the runtime constants of the method area after the class is loaded.

String a =" ABC "; "ABC" is not in the Java heap, but in the constant pool (StringTable:HashSet) of the method area. String b =" ABC "; //b is a reference to a local variable table that exists on a stack. "ABC" is not in the Java heap, but in the constant pool of the method area. String c = new String (" ABC "); A = b true a = c false a = C.intern true // C.intern Runtime constant. Fetch values from the heap into run-time constants.Copy the code

2.6.3 Garbage collection

  • Garbage collection behavior in the method area: less garbage collection, because less collection, low efficiency.
  • Perm Gen is only GC when Full GC is performed.

2.6.4 Method area exception and Configuration

  • Java. Lang. OutOfMemoryError: PermGen however no matter how you set the -xmx don’t work.

Because the argument to set its size is not -xmx, but

  • -xx :PermGen, -xx :MaxPermGen (varies with Java versions)

2.6.5 supplement

  • Perm Gen is not part of Heap

  • You’ll see later that not all object instances are on the heap. But it can be understood that the object instances are all on the heap.

Thread private memory is also allocated in the heap. The purpose is also for recycling.

  • Strings are in the constant pool, normal objects are in the heap,

2.7 Method region JDK8(JDK8 Metaspace replaces Perm Gen)

JDK8- PermGen for Metaspace

2.7.1 Replacement Cause

  • The removal of persistent generations is an effort to merge HotSpot JVM with JRockit VM, as JRockit does not have persistent generations and does not need to be configured.
  • Due to the permanent generation memory often enough or memory leaks occur, when abnormal Java. Lang. OutOfMemoryError: PermGen

2.7.2 Meta-space Function

  • The meta-space is the implementation of the method area in HotSpot JVM. The method area is used to store class information, constant pools, method data, method code, etc. The method area is logically part of the heap, but is often referred to as “non-heap” to distinguish it from the heap.

  • Similar in nature to permanent generations, meta-spaces are implementations of method areas in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. , theoretically depends on how much memory a 32-bit / 64-bit system can virtualize. Visibility is not unlimited, you need to configure parameters.

2.7.3 Common Configuration Parameters

  • 1.MetaspaceSize

The size of the initialized Metaspace, which controls the threshold at which GC occurs in the meta-space. Dynamically increase or decrease MetaspaceSize after GC. By default, this value varies from 12M to 20M depending on the platform. Use the Java -xx :+PrintFlagsInitial command to view the native initialization parameters

  • 2.MaxMetaspaceSize

Limit the growth of Metaspace to prevent situations that cause Metaspace to use local memory indefinitely and affect other programs. The default value of this parameter is 4294967295B (about 4096MB).

  • 3.MinMetaspaceFreeRatio

After the Metaspace GC is performed, the current Metaspace free space ratio is calculated. If the free ratio is less than this parameter (i.e. the actual non-free ratio is too large and the memory is insufficient), the virtual machine will increase the Metaspace size. The default value is 40, which is 40%. If this parameter is set, the growth rate of Metaspace can be controlled. If the value is too small, the growth rate of Metaspace will be slow, and the use of Metaspace will gradually become saturated, which may affect the class loading in the future. Too large a value will cause Metaspace to grow too fast, wasting memory.

  • 4.MaxMetasaceFreeRatio

After the Metaspace GC is performed, the current Metaspace free space ratio is calculated. If the free ratio is greater than this parameter, the virtual machine frees part of the Metaspace space. The default value is 70, which is 70%.

  • 5.MaxMetaspaceExpansion

The maximum amount of Metaspace growth. The default value of this parameter is 5452592B (about 5MB) on the local machine.

  • 6.MinMetaspaceExpansion

The minimum amount of Metaspace growth. On the local machine, the default value of this parameter is 340784B.

2.8 Direct Memory

  • Direct memory is not part of the runtime data area of the virtual machine, nor is it defined in the Java Virtual Machine specification.
  • Frequent use of this memory can also cause OutOfMemoryError exceptions

2.9 Layout of Objects in Memory

2.9.1 Object Creation

  • Object creation steps:

1) according to the parameters of the new definition of a class in the constant pool symbols referenced 2) if the symbol is not found, that is loaded, after class is not a class loading, parsing and initialized. 3) virtual machine allocates memory for the object in the heap) 4) will be allocated memory is initialized to zero (does not contain the object head) 5) call object method

  • Object memory allocation strategy:

1) Pointer collision (regular memory block) 2) Free list: irregular memory, there is a table of free memory, then go to find free memory area. Allocation. Whether free memory is regular is determined by garbage policy. If there is a compressing function, then use pointer collision for rules. If there is no compressing function, use the free list irregularly.

  • Thread safety

One space of memory may be applied for by two simultaneously: scheme 1) thread synchronization, locking, safe, but slow execution efficiency is low. The local thread allocation buffer (TLAN) is used to allocate an area for each thread. When each thread needs to allocate an area, it is allocated from the local battery allocation buffer, so that it does not conflict with the area of other threads. If the area is full, it is allocated another area (use a synchronous lock strategy when allocating new areas).

  • Initialize an object

Initialize default values, 0 for integers, false for Boolean types, and null for objects.

  • Execute constructor

The constructor is executed after initialization.

2.9.2 Object Structure

  • Header p47

The stored information is:

    • Ojbect’s hashCode is the natVIE method that is called. GC generation age, generation in memory: mark clear, mark collation, copy, generation collection lock status flag thread hold lock bias thread ID bias timestamp

It is found in the above figure that objects in different lock states use the same space to express different contents, so that space can be reused.

    • Type a pointer

    Not all need type Pointers.

    • The length of the array

If it’s an array, there’s also a section in the object header that holds the array length.

  • InstanceData

The valid information of the stored object is the object we see. The same allocation of fields will be in a piece of Longs/ Doubles; shorts/chars .

  • Padding

Padding is not necessarily there, it’s a placeholder, it has no real meaning for padding. Cause: Automatic memory management requires that the start address of the object must be an integer multiple of eight bytes, and the object header must also be an integer multiple of eight bytes P48.

2.9.3 Locating object Access

P48 specifies the reference address of stack memory to heap memory. But the address doesn’t have to be the object itself. It can also be a handle.

  • Using handles has a pool of handles, saves instance object addresses, and then finds the real address of the object. The advantage is that the address of the handle pool does not change (regardless of object movement or garbage collection). The reference address in the handle pool changed. Stack pointing does not change, high performance.

  • Uses direct Pointers but HotSpot uses this approach.

  • Conclusion: Object construction

    • When using a handle:

Stack, heap (handle [pointer to object instance data {point to object in heap}, pointer to object type data {point to method area], object), method area (object type data area)

    • Direct pointer:

Stack, direct object (pointer to object type data {point to method area}), method area (object type data area)

2.10 supplement

  • Constant pool: is a constant pool in the Class structure file, run time to run time constant pool.

2.11 Java code memory space – actual combat and reference articles

Memory allocation for the String class in Java

Java memory allocation comprehensive analysis

Java memory allocation and variable storage location examples

Memory allocation and related issues involved in Java object (instance) creation

Stack and memory allocation principles in JAVA

In-depth understanding of the Java Virtual Machine book