what is jvm?

Original: http://blog.csdn.net/stanlee_0/article/details/51171382

Description: Almost everyone who does java development knows the term jvm, but because jvm is not much related to actual simple development, it usually takes a year or two to work (of course, excluding those who love to learn and specialize in performance optimization) ), very few people can learn and understand what jvm is and how jvm works. Personally, I think this is still very necessary to understand and study carefully, especially for those who are just getting started or who are just getting started. For development, this is the cornerstone of java.

JVM (Java Virtual Machine, Java Virtual Machine)

    The cross-platform feature of a Java program mainly means that the bytecode file can be run on any computer or electronic device with a Java virtual machine. The Java interpreter in the Java virtual machine is responsible for interpreting the bytecode file into a specific machine code for running. . Therefore, at runtime, the Java source program needs to be compiled into a .class file by the compiler. As we all know, java.exe is the execution program of the java class file, but in fact the java.exe program is just an execution shell, it will load jvm. , for: libjvm.so), this dynamic link library is where the actual operation of the java virtual machine is located.

    JVM is part of JRE. It is a fictitious computer, which is realized by simulating various computer functions on an actual computer. JVM has its own complete hardware architecture, such as processor, stack, registers, etc., and also has a corresponding instruction system. The most important feature of the Java language is that it runs across platforms. The JVM is used to support operating system-independent, cross-platform. Therefore, the JAVA virtual machine JVM belongs to the JRE, and now when we install the JDK, the JRE is also installed (of course, the JRE can also be installed separately).

JVM memory area division

Roughly, the internal architecture of the JVM is divided into three parts, namely: the class loader (ClassLoader) subsystem, the runtime data area, and the execution engine.

class loader

    Each Java virtual machine consists of a class loader subsystem (class loader subsystem), which is responsible for loading the types (classes and interfaces) in the program and assigning a unique name. Every Java virtual machine has an execution engine (execution engine) responsible for executing the instructions contained in the loaded classes. The two types of JVM class loaders include: startup class loader and user-defined class loader. The startup class loader is a part of the JVM implementation, and the user-defined class loader is a part of the Java program and must be a subclass of the ClassLoader class. .

Execution engine: it is either executing bytecode or executing native methods

    The main execution technologies are: interpretation, just-in-time compilation, adaptive optimization, and direct execution at the chip level. Interpretation belongs to the first-generation JVM, just-in-time compilation JIT belongs to the second-generation JVM, and adaptive optimization (currently Sun's HotspotJVM adopts this technology) Learn from the experience of the first-generation JVM and the second-generation JVM, and use a combination of the two.

    Adaptive optimization: Start by interpreting all the code and monitor the code execution, then start a background thread for those methods that are called frequently, compile it to native code, and do careful optimization. If the method is not used frequently, the compiled code is canceled, and it is still interpreted and executed.

Runtime data area: mainly includes: method area, heap, Java stack, PC register, local method stack

jvm structure

  • Method area and heap are shared by all threads

    Heap: Stores all objects created by the program at runtime

    Method area: When the class loader of the JVM loads the .class file and parses it, the parsed type information is put into the method area.

  • Java stack and PC registers are exclusively shared by threads

    The JVM stack is private to the thread. When each thread is created, a JVM stack is created. The JVM stack stores the local basic type variables in the current thread (eight basic types defined in java: boolean, char, byte, short, int, long, float, double), partial return results and Stack Frame, objects of non-basic types only store an address pointing to the heap on the JVM stack

  • Native method stack: stores the state of native method calls

JVM runtime data area

    Because the data area of ​​the jvm runtime is still very important to our development, so let's talk about it.

  • 方法区域(Method Area)

    在Sun JDK中这块区域对应的为PermanetGeneration,又称为持久代。

    方法区域存放了所加载的类的信息(名称、修饰符等)、类中的静态变量、类中定义为final类型的常量、类中的Field信息、类中的方法信息,当开发人员在程序中通过Class对象中的getName、isInterface等方法来获取信息时,这些数据都来源于方法区域,同时方法区域也是全局共享的,在一定的条件下它也会被GC,当方法区域需要使用的内存超过其允许的大小时,会抛出OutOfMemory的错误信息。

  • 堆(Heap)

    它是JVM用来存储对象实例以及数组值的区域,可以认为Java中所有通过new创建的对象的内存都在此分配,Heap中的对象的内存需要等待GC进行回收。

    堆是JVM中所有线程共享的,因此在其上进行对象内存的分配均需要进行加锁,这也导致了new对象的开销是比较大的

    Sun Hotspot JVM为了提升对象内存分配的效率,对于所创建的线程都会分配一块独立的空间TLAB(Thread Local Allocation Buffer),其大小由JVM根据运行的情况计算而得,在TLAB上分配对象时不需要加锁,因此JVM在给线程的对象分配内存时会尽量的在TLAB上分配,在这种情况下JVM中分配对象内存的性能和C基本是一样高效的,但如果对象过大的话则仍然是直接使用堆空间分配

    TLAB仅作用于新生代的Eden Space,因此在编写Java程序时,通常多个小的对象比大的对象分配起来更加高效。

  • JavaStack(java的栈):虚拟机只会直接对Javastack执行两种操作:以帧为单位的压栈或出栈

    每个帧代表一个方法,Java方法有两种返回方式,return和抛出异常,两种方式都会导致该方法对应的帧出栈和释放内存。

    帧的组成:局部变量区(包括方法参数和局部变量,对于instance方法,还要首先保存this类型,其中方法参数按照声明顺序严格放置,局部变量可以任意放置),操作数栈,帧数据区(用来帮助支持常量池的解析,正常方法返回和异常处理)。

  • ProgramCounter(程序计数器)

    每一个线程都有它自己的PC寄存器,也是该线程启动时创建的。PC寄存器的内容总是指向下一条将被执行指令的饿地址,这里的地址可以是一个本地指针,也可以是在方法区中相对应于该方法起始指令的偏移量。

    若thread执行Java方法,则PC保存下一条执行指令的地址。若thread执行native方法,则Pc的值为undefined

  • Nativemethodstack(本地方法栈):保存native方法进入区域的地址

    依赖于本地方法的实现,如某个JVM实现的本地方法借口使用C连接模型,则本地方法栈就是C栈,可以说某线程在调用本地方法时,就进入了一个不受JVM限制的领域,也就是JVM可以利用本地方法来动态扩展本身。

JVM垃圾回收

    Sun的JVMGenerationalCollecting(垃圾回收)原理是这样的:把对象分为年青代(Young)、年老代(Tenured)、持久代(Perm),对不同生命周期的对象使用不同的算法。(基于对对象生命周期分析)

    通常我们说的JVM内存回收总是在指堆内存回收,确实只有堆中的内容是动态申请分配的,所以以上对象的年轻代和年老代都是指的JVM的Heap空间,而持久代则是之前提到的MethodArea,不属于Heap。

    GC的基本原理:将内存中不再被使用的对象进行回收,GC中用于回收的方法称为收集器,由于GC需要消耗一些资源和时间,Java在对对象的生命周期特征进行分析后,按照新生代、旧生代的方式来对对象进行收集,以尽可能的缩短GC对应用造成的暂停

(1)对新生代的对象的收集称为minor GC;

(2)对旧生代的对象的收集称为Full GC;

(3) The GC enforced by actively calling System.gc() in the program is Full GC.

    Different object reference types are collected by GC in different ways. JVM object references are divided into four types:

(1) Strong reference: By default, the object uses a strong reference (the instance of this object has no other object references, and it will only be recycled during GC)

(2) Soft reference: Soft reference is an application provided in Java that is more suitable for caching scenarios (it will be GC only when the memory is not enough)

(3) Weak reference: it will be recycled by GC during GC

(4) Virtual reference: Since virtual reference is only used to know whether the object is GC

  • Young (young generation)

    The young generation is divided into three districts. One Eden area, two Survivor areas. Most objects spawn in the Eden area. When the Eden area is full, the surviving objects will be copied to the Survivor area (one of the two). When the Survivor area is full, the surviving objects in this area will be copied to another Survivor area. When it is full, the objects copied from the first Survivor area and still alive at this time will be copied to the Tenured area. It should be noted that the two areas of the Survivor are symmetrical and have no relationship, so the same area There may be objects copied from Eden and objects copied from the previous Survivor at the same time, and only the objects copied from the first Survivor are copied to the old area. Moreover, one Survivor area is always empty.

  • Tenured (Old Generation)

    The old generation holds objects that survive from the young generation. Generally speaking, the old generation stores objects with long lifespans.

  • Perm (Persistent Generation)

    Used to store static files, now Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, etc. In this case, a relatively large persistent generation space needs to be set to store these newly added classes during operation. The persistent generation size is set with -XX:MaxPermSize=.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325963070&siteId=291194637