[Study notes] in-depth understanding of the first chapter - Chapter II Java Virtual Machine

Chapter approached Java

Java technology system

The JDK (the Java Development Kit): the Java programming language, Java virtual machine, Java API class libraries.

The JRE (Java Runtime Environment): Java SE API subset of the Java API class libraries and the Java virtual machine.

JDK for Java programs to support the development of a minimal environment.

JRE is supported Java environment running standards.

ME Java (Micro Edition): supports the Java platform running on a mobile terminal (mobile phone, PDA), and on the Java API has been streamlined, and added support for mobile terminals, the version formerly known as J2ME.

SE Java (Standard Edition): Support for the Java platform desktop applications (such as applications under Windows) offers a complete Java core API, this version formerly known as J2SE.

EE Java (Enterprise Edition): supports the use of multi-tiered enterprise applications Java platform, in addition to providing Java SE API, but also a lot of its expansion and provides related deployment support, this version formerly known as J2EE.

The second chapter Java area of ​​memory and memory overflow exception

Runtime data area

 

 

 

 

 

 Which is the method area and heap threads share area , the virtual machine stack, native method stacks, the program counter is a thread exclusive area .

Program Counter:

Is a small memory space, it can be seen as the current line number indicator bytecode execution thread .

If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the method is being performed Native, this counter value is null (Undefined). This memory area is the only one not specify any area OutOfMemoryError situation in the Java Virtual Machine Specification.
 

Java virtual machine stack:

VM stack is described in the Java memory model method performed: Each method creates a stack frame (stack frame) while performing information storage for local variables table, operand stack, dynamic linking, method exports. Each method call until completion of the procedure, it corresponds to a stack frame to push the stack in a virtual machine process stack.

Local variable table stored in the known compile all the basic data types, object references and returnAddress type (address pointing to a byte code instruction).

When entering a method that much space for local variables allocated in the frame is completely determined during operation of the method does not change the size of the local variable table.

Two kinds of exception: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverflowError will throw an exception; if the virtual machine can dynamically expand the stack, can not be extended to apply if enough memory, it will throw an OutOfMemoryError.

 

Native method stacks:

Similar virtual machine and stack effect, except that the virtual machine execution stack for the Java virtual machine service method, and the local method, the virtual machine to use a method of Native service.

 

Java heap:

For most applications, Java heap memory is the biggest piece of the Java virtual machine management. The sole purpose of this memory area is stored object instance.

Java heap is the main area managed by the garbage collector. From the memory recovery of view, the current collector is basically using generational collection algorithm.

The current mainstream virtual machines are implemented in accordance with scalable (by -Xmx and -Xms control). If no memory heap allocated instances completed, and the heap can no longer expand, it will throw OOM exception.

 

Method area:

Class information is used to store the virtual machine has been loaded, constants, static variables, the time compiler to compile the code and other data. There is an alias "non-heap", aims to distinguish and Java heap.

For HosSpot virtual machine, the method area many people become "permanent generation", essentially the two are not equivalent, simply because HotSpot virtual machine's design team chose the GC generational collection area extends to methods, or permanent generations District method to achieve it, so HotSpot garbage collector can manage the Java heap like this as part of memory management, eliminating the need for special preparation can memory management code for the method area. For other virtual machine is the concept of permanent generation does not exist.

Throws OOM exception.

 

Runtime constant pool:

The method is part of the region, Class file versions in addition to the classes, fields, methods, and interface description information, as well as a constant pool information, and for storing various literal compile generated reference symbols, this part content will run into the area after the class method load-time constant pool of storage.

Another important feature of the runtime constant pool file relative to Class constant pool is equipped dynamic, Java language does not require constant necessarily only compile in order to produce, you can also during operation, such as the intern () method of the String class.

OOM will throw an exception.

 

Direct memory:

Not part of the virtual machine runtime data area, nor is it Java virtual machine memory area defined in the specification.

It can lead to OOM.

 

HotSpot virtual machine object Quest

Objects created:

1. Does the virtual opportunity when a new instruction, first check whether the command parameters can be positioned in the constant pool of a class reference to symbol, and this symbol to check references on behalf of a class has been loaded, parsed and initialized. If not, then you must perform the appropriate class loader.

2. After the class is loaded by the virtual machine memory for the new object allocation. The required size of the object can be exactly determined after the class is loaded, the space allocated for the task object is equivalent to the one determined by the size of the Java heap memory from carved out.

Assume heap memory is absolutely regular, all memory is used aside, free memory on the other side of the middle put a pointer as the demarcation point indicator, and that the allocated memory is just the pointer to the idle moving the object side and the space period equal to the magnitude of distance, such assignment is called "pointer collisions." If the Java heap memory is not regular, it has been used and free memory intertwined, there is no way to simply be a pointer collision, the virtual machine must maintain a list of record which memory blocks are available, this allocation It is called "free list."

Whether regular Java heap by the garbage collector used with or without compression sorting function decision.

 

Concurrency is not thread safe. Two options:

1) the operation of memory space allocated sync, in fact, coupled with the virtual machine mode CAS failure retry guarantee atomic update operation.

2) The operation of memory allocation is divided among the different spatial performed, i.e. each thread a previously allocated heap memory in the Java small, called a local thread allocation buffer (Tlab) . Which thread to allocate memory when it allocated on TLAB which thread, only TLAB run out and assign a new TLAB, only need to synchronize locked.

 

3. After the memory allocation is completed, the virtual machine needs to allocate memory space is initialized to a value of zero (not including the object header).

4. virtual machine objects necessary configuration, for example, which this object is an instance of the class, how to find information such as metadata, hash object do, GC target sub-information-generation age. This information is stored in the object header object.

5. After the completion of the above work, from the point of view of the virtual machine, a new object has been created, but from the perspective of view Java programs, object creation is just beginning - <init> method has not been executed, all field is also zero. After executing new instruction is then executed <init> method.

 

Memory layout objects:

In HotSpot VM, the object is stored in memory layout may be divided into three areas: object header (Header), instance data (InstanceData) and alignment padding (Padding).

Object header information includes two parts, a first part for storing its own runtime data objects , such as hashing it, the GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp, this part length of the data in 32-bit and 64-bit virtual machine are 32bit and 64bit, officially called it "Mark Word".

 

 The other part is the type of pointer , i.e. a pointer to its class object is metadata, the virtual machine is determined by the pointer which is an instance of the object class.

Examples of valid data portion is actually stored in the object information.

 

Access the location of the object

Handle visit: the Java heap will be divided into a memory address as a handle to handle pool is stored in the reference object.

Benefits are storage-stable reference handler address changes only instance data pointer in the handle when the object moves, the reference need not be changed.

 

 

Direct pointer access: Reference is stored in direct object address.

 

 

Benefit is faster.

 

OOM combat abnormal

The String.intern () method is a Native, its role is: If the string constant pool already contains a string like object to this String, String object representing the string pool is returned; otherwise, this String object comprising adding to the string constant pool, this string object and returns a reference.

Guess you like

Origin www.cnblogs.com/mcq1999/p/12097087.html