JVM inquiry

Reproduced in the mad god teacher , only use notes

. 1, the JVM position
2, the architecture of the JVM
3, class loader
4, parents delegation mechanism
5, sandbox security mechanism
6, native, PC register (thread private), method area
7, stack: a data structure
8, three kinds of the JVM
9, heap
10, JPofiler use tools to analyze the reasons OOM
11, GC garbage collector
12, JMM

1, JVM position

2, JVM architecture

3, the class loader

3.1 virtual machine comes loader

3.2. Start classes (root) loader

3.3. Extension classloader

3.4. The application loader

4, parents delegate mechanism

Class loader when the class is loaded, it will follow

APP application class loader a> EXT extend a class loader> Root Root class loader (final execution)

If you do not follow the class loader will then move forward to find (the root class loader -> extension class loader -> Application class loader)

Action
safer, to avoid the same bag-like interference

5, sandbox security mechanism

java code defined in a specific operating range of the virtual machine (JVM) in

Some remote code there may be security risks of authority control

composition

Bytecode verifier

Class loader

Access controller

Security Manager

Security package

6, native, PC register (thread private), the method area

6.1、native

Those who brought the native keyword, indicating the scope of java reach, go back and call the underlying C language library into the native method stacks, call the native method interface JNI

JNI functions: java extensions using different fusion java programming language used initially:! C, C ++

java born when the C, C ++ rampant, want to gain a foothold must call C, C + + program

It opened a special memory area in a marking region: Native Method Stack, native registration method

When the final execution method loads native methods through JNI library

Examples ------- "java printer driver program, management system, you can grasp, relatively rare in enterprise applications

6.2, register (thread-private)

Each Qian Cheng has - a program counter, the thread is private, is a pointer to the method bytecode method in the area, read the next command execution engine, is a very small memory space, almost negligible Excluding.

It means when multiple threads of execution, disturbance does not occur

6.3, the method area

Storing stuff

static modification variable, Final (constant), all Class class information (constructor, interface definition), the constant pool.

However, the presence of the heap memory instance variables, and independent method area

definition

The method area is shared among all threads, all the fields and methods byte code, as well as some special methods, such as constructor, interface code is also defined herein. Simply put, all the information about all defined methods are stored in the region, this area belongs to the shared space.

7, the stack: a data structure

Store things:

  • 8 substantially large object reference type + + method of Example

After the advanced, LIFO (queue: FIFO FIFO)

Why main () before execution, and end

Stack memory, in charge of running the program, the life cycle and thread synchronization

Thread the end of the stack memory is released to the stack, the garbage collection problem does not exist

Once the end of the thread stack Over on
the implementation of the principle of the stack: the stack frame

  • Each implementation of a method. Generating a stack frame

Stack full of ---- "StackOverflowError ----" stack memory overflow

+ Heap + stack method area: interactions

8 three JVM

8.1, Sun HotSpot company

8.2、BEA jRockit

8.3、IBM J9VM

9, heap

After 9.1, the class loader reads the class file, generally what will put the heap

Classes, methods, constants, variables ~, save us all a real object reference types

9.2, heap a jvm only a heap memory, heap memory size can be adjusted

9.3, heap memory is also divided into three regions

9.3.1 newborn region (region Eden) Young / New

Class: the birth and growth of local and even death

Garden of Eden, all objects are new out of the area in the Garden of Eden

Survivors area (0,1)

9.3.2, pension old district

Truth: After the study, 99% of the objects are temporary objects

9.3.3 permanent Perm region

This area of ​​permanent memory, used to store the JDK Class object itself carries. Interface metadata stored some java runtime environment information type or ~, garbage collection does not exist in this area! Close VM virtual memory is released this region

A startup class, loaded with a large number of third-party jar package. Tomcat deployment of too many applications, a large number of dynamically generated class reflection. Constantly being loaded. Until the memory is full, there will be OOM

Before jdk1.6: permanent generation is in the process of constant pool area
jdk1.7: permanent generations, but slowly degraded, to permanent generation heap constant pool
after jdk1.8: No permanent generations. Constant pool in element space

  • Element space: at a logical, not present (after JDK8, permanent storage zone to a name (element space)) are physically

10, OOM reason for using JPofiler analysis tool

10.1, how to quickly rule out the OOM project failures occurring in

You can see the first few lines of an error: memory snapshot analysis tool: MATJprofiler
the debugging version of ---- "line by line analysis of code

10.2, if the memory is full, OOM, not enough memory! Java.lang.OutOfMemoryErrorjava heap space

Solution:

  • 1. Try to see the result of the expansion of heap memory

  • 2. Analysis of memory to see which problem areas

10.3, MAT.Jprofiler role

Analysis Dump memory file, locate memory leaks fast
data obtained heap
obtain large objects

10.4, some instructions

11, GC garbage collection

11.1, mainly in the area of ​​the Garden of Eden and pension area ~

11.2, JVM during the GC, not these three areas (the new generation, the surviving area (from, to), tenured) uniform recycling, most of the time, are the new generation of recycling

11.3, GC two types

Light GC (Common GC)
weight GC (global GC)

11.4, GC title

11.4.1, JVM memory model and partitions - each area in detail what to do?

11.4.2, heap inside the partition what? Eden, from, to, tenured, talk about their characteristics!

11.4.3, GC algorithm which

11.4.3.1, reference notation

Record number of uses object

11.4.3.2, replication algorithm

Copied from the original from area to area to live .. enough times to survive into the pension zone area

Replication algorithm optimal usage scenarios: a lower target activity time: young generation

Advantages and disadvantages:

  • benefit

    • No memory fragmentation
  • harm

    • A waste of memory space: more than half of the space is always empty to, assuming that the object 100 percent survival (extreme case)

11.4.3.3, mark sweep algorithm

Scan these objects, living objects that are marked to unmarked objects cleared

Advantages and disadvantages:

  • Advantage of
    no extra space
  • Shortcoming
    two scans, a serious waste of time, it will produce memory fragmentation

11.4.3.4, compression mark

11.4.3.5, marking clear compression

Marking the first clear several times and then compressed

11.4.4, light GC, GC were heavy at what happened when

11.4.5, GC summary

  • Memory efficient
    replication algorithm> tag sweep algorithm> tag compression algorithm (time complexity)
  • Memory uniformity
    replication algorithm compression algorithm = mark> tag Clear algorithm
  • Memory utilization (space)
    marked compression algorithm> Clear labeling algorithm> Copy algorithm

11.4.6, not the best algorithm, only the most appropriate algorithm

GC: generational collection algorithm

  • The young generation
    low survival rate, using the copy algorithm
  • Old's
    area is larger, survival, marking clear (memory fragmentation, not too much) + mark compression hybrid implementation

12, Jnn

12.1, java memory model

12.2, is doing it

Role: cache coherency protocol, define the rules for reading and writing data (compliance, found this rule)

JMM defines an abstract relationship between threads working memory and main memory: shared variables are stored in the main memory between threads, each thread has a private local memory

Since the program is run entity JVM thread, and each thread is created JVM will create a working memory (stack space), working memory is private data area for each thread, and Java memory model under which all variables are stored in the main memory, main memory is shared memory region, all threads have access, but the thread operations on variables (reading assignment, etc.) must be carried out in the working memory, the first variable you want to copy from main memory to their work memory space and then operate the variable, the variable write operation is completed and then back to the main memory. You can not directly operate the main memory variables, each thread in the working memory are stored in main memory variable copy copy, and therefore between different threads can not access each other's working memory, communication (by value) between the thread must be done by the main memory.

12.3, JMM provisions on synchronized

1, the first thread unlocks the value of the shared variable must be flushed to main memory
2, before locking thread, you must read the latest value of main memory to their working memory
3, the same lock unlock - lock

12.4, JMM thread-safe

  • Working memory and main memory latency synchronization phenomenon caused visibility problems

  • You can use synchronized or volatile keywords to solve, they can make a thread after a variable modified immediately visible to other threads. For the visibility of the issue and order problems caused by the rearrangement of instructions, can be used to address the volatile keyword, because another volatile action is to prohibit reordering optimization.

12.5, JMM adhere to three principles

12.5.1, visibility

Each thread of the main memory operations CCP variables are copied to each individual threads of memory to operate their own work and then written back to main memory, the use of the volatile keyword solve the visibility problem.

This may be one thread changes the value of the shared variable AAA but when not written back to main memory, and another thread BBB main memory to operate with a shared variable, but this time AA threads of shared variables in working memory for BBB thread is not visible, this kind of work memory and main memory synchronization delay is caused visibility problems.

12.5.2 atomicity

Atomicity in multi-threaded concurrency, will not split or stopper, there will be multiple threads of a thread is suspended, data is lost, incomplete, appear under the Atomic class package, to ensure atomicity

12.5.3, orderly, i.e. prohibition instruction reordering

When the computer executes the program, in order to improve performance, often have compiler and processor instructions are rearranged, the following three general points
source - "compiler optimization rearrangement -" instruction parallelism rearrangement - "Memory System rearrangement - "instruction executed last

Single-threaded environment inside the final results of the implementation program to ensure that the order of execution and code consistent results.

The processor must consider data dependencies between instructions during reordering

Multi-threaded environment alternately thread execution, because the compiler optimization rearrangement exists, the variables used in the two threads can ensure consistency can not be determined, the results can not be predicted

Guess you like

Origin www.cnblogs.com/zhuchengbo/p/12635407.html