Run-Time Data Areas
Class loader loads all the required class files into different runtime data areas:
Method Area, Heap, Java Stack, Native Method Stack, Program Counter Register.
Program Counter (PC) Register
The Program Counter (PC) Register is used as line number indicator of current thread.
The Java Virtual Machine can support many threads of execution at once. To support multiple thread execution, CPU processes requests of multiple threads in turns, but one CPU can only execute the command for one single thread at a specific time.
So in case that processor can still remember the last executing line number for current thread after thread switch, each Java Virtual Machine thread has its own pc (program counter) register to keep that line number.
Java Virtual Machine Stacks
Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames.
It holds local variables and partial results, and plays a part in method invocation and return.
Each Java stack frame consists of three components, although at any given time one or more of the components may be empty:
- the local variables
- the operand stack
-
the execution environment
A stack frame will be put into stack when a method invocation happens, and popped up when it returns. When another method is called within the method a new stack frame will be put into stack. Therefore, the unlimited recursion would cause a StackOverflowError.
In JVM specification, there are two kinds of exceptional conditions happened with Java Stacks:
If the current thread requires a Stack Frame which is located deeper than Java Stack permitted, the Java Virtual Machine throws a StackOverflowError.
Insufficient memory can be made available to create the initial JavaVirtual Machine stack for a new thread, the Java Stack throws an OutOfMemoryError.
Native Method Stacks
It works similar as Java Virtual Machine Stacks, except it handles the methods which are implemented by native language such as C. Like Java Stack, native method stacks are typically allocated per thread when each thread is created
The StackOverflowError and OutOfMemoryError can also be thrown if same exceptional conditions satisfied.
Heap
For most applications, Heap is the largest memory in JVM memory management.
Heap is shared among all Java Virtual Machine threads. And heap is the run-time data area from which memory for all class instances and arrays is allocated.
JVM Heap Memory Structure
JVM Heap includes three parts:
· The Young Generation
· The Old Generation
· The Permenate Generation
Virtual Memory
System memory can be any of three status: Free, Reserved, Committed.
As we all know, the JVM memory is a big block of continuous memory. Before JVM allocates memory to Heap, JVM would reserve a big block memory as its Vitual Memory. For the part which it has been using is called Committed memory.
Based on Vitual Memory, the above three part are created.
Young Generation
The Young Generation consists of three parts:
· The Eden Space
· Survivor 0 Space
· Survivor 1 Space
Eden Space
Eden Space is the region where most objects are initially allocated.
Although Eden Space is shared among all the threads, JVM still assign a Thread Local Allocation Buffer (TLAB) for each thread for its object allocation to avoid contention and improve allocation efficency.Object allocation via a TLAB is a very cheap operation; it simply bumps a pointer for the object size which takes roughly 10 instructions on most platforms.
Because the objects allocation on TLAB does not need to lock memory, JVM would try to allocate object memory on TLAB for each thread firstly. When the object is too big or TLAB is fully occupied, the object allocation will be on the other parts of Heap. Hence, many small objects are more efficent than one big object.
-XX:TLABWasteTargetPercent : To set the percentage per TLAB on Eden, default value is 1%
-XX:+PrintTLAB :To check the TLAB status
Survivor 0 and Survivor 1 Space
These two spaces are also called: From Space and To Space.
These two space have the same size, but they are smaller than Eden Space. Compared with Eden Space, each Survivor Space are 1/8 smaller by default setting.
Survivor Spaces are used for Minor GC, more information will be coverred in Minor GC section.
Old Generation
Objects that live long enough are eventually promoted to the tenured space.
-Xms<size> 设置初始 Java 堆大小
-Xmx<size> 设置最大 Java 堆大小
-Xss<size> 设置 Java 线程堆栈大小
-XX:NewSize=n 设置年轻代大小
-XX:NewRatio=n 设置年轻代和年老代的比值。如:为3,表示年轻代与年老代比值为1:3,年轻代占整个年轻代年老代和的1/4
-XX:SurvivorRatio=n 年轻代中Eden区与两个Survivor区的比值。注意Survivor区有两个。如:3,表示Eden:Survivor=3:2,一个Survivor区占整个年轻代的1/5
-XX:MaxPermSize=n 设置持久代大小
Method Area
Method Area (also called: Perm Gen) is also shared among all Java Virtual Machine threads.
It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods used in class and instance initialization and interface initialization.
The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.
In hotspot JVM, Method area is called Permanent Generation. 永久代
When system loads too much class information, Permanent Generation could reach its maximum size (-XX:MaxPermSize=n), System will trigger a full GC if GC strategy is not set as CMS GC. If memory in the method area still cannot be made available to satisfy an allocation request after full GC, the Java Virtual Machine throws an OutOfMemoryError.
Run-Time Constant Pool
A run-time constant pool is a per-class or per-interface run-time representation of the constant pool table in a class file.
It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.
The run-time constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.
Each run-time constant pool is allocated from the Java Virtual Machine's Method Area. The run-time constant pool for a class or interface is constructed when the class or interface is created by the Java Virtual Machine.
Direct Memory
Direct Memory does not either belong to Java Run-Time Data Areas, or defined in memory management of JVM specification, but still it is used frequently, and also could trigger OutOfMemoryError.
When NIO is introduced in JDK1.4, developer can use an approach based on Channel and Buffer to manage I/O. It uses native lib methods to direct access memory out of JVM management, and manage the memory by using DirectByteBuffer object.
Such Direct Memory access approach is not limited by Heap size, but still it is limited by system memory. When Direct Memory plus Heap exceeded the max size of total memory, it will cause OutOfMemoryError.
VirtualGC in VirtualVM
To see the dynamic nature of GC, launch JVisualVM and install the Visual GC plugin. This will enable you to see the GC in action for your application as below.
Object Representation
One possible heap design divides the heap into two parts: a handle pool and an object pool. An object reference is a native pointer to a handle pool entry. A handle pool entry has two components: a pointer to instance data in the object pool and a pointer to class data in the method area.
l The advantage of this scheme is that it makes it easy for the virtual machine to combat heap fragmentation. When the virtual machine moves an object in the object pool, it need only update one pointer with the object's new address: the relevant pointer in the handle pool.
l The disadvantage of this approach is that every access to an object's instance data requires dereferencing two pointers.
Another design makes an object reference a native pointer to a bundle of data that contains the object's instance data and a pointer to the object's class data.
l This approach requires dereferencing only one pointer to access an object's instance data, which is quicker to access instance.
l But makes moving objects more complicated. When the virtual machine moves an object to combat fragmentation of this kind of heap, it must update every reference to that object anywhere in the runtime data areas.
相关推荐
【标题】"nginx-upstream-jvm-route-1.15" 涉及的核心知识点是Nginx的upstream模块与JVM路由的整合,特别针对Nginx 1.15版本。这个项目旨在解决在配置Nginx时遇到的特定错误提示“nginx: [emerg] invalid parameter ...
### JVM调优总结:Xms、Xmx、Xmn、Xss 在Java虚拟机(JVM)的运行过程中,合理的参数配置对于提高程序性能至关重要。本文将对JVM调优中的几个关键参数进行深入解析,包括-Xms、-Xmx、-Xmn和-Xss等,帮助开发者更好...
1. `-Xms`: 这个参数用于设置JVM启动时初始的堆内存大小。默认情况下,JVM会从小到大动态地调整堆空间,但设置`-Xms`可以确保程序启动时就分配足够的内存,避免频繁的内存扩展操作,从而提高性能。例如,`-Xms256m`...
JVM调优总结 -Xms -Xmx -Xmn -Xss JVM 调优是 Java virtual machine 的性能优化,通过调整 JVM 的参数来提高 Java 应用程序的性能。其中,-Xms、-Xmx、-Xmn、-Xss 是四个重要的参数,分别控制 JVM 的初始堆大小、...
赠送jar包:metrics-jvm-3.1.5.jar; 赠送原API文档:metrics-jvm-3.1.5-javadoc.jar; 赠送源代码:metrics-jvm-3.1.5-sources.jar; 赠送Maven依赖信息文件:metrics-jvm-3.1.5.pom; 包含翻译后的API文档:...
代码如下:failed to create jvm error code -4 这一般应是内存不够用所致,解决方法参考如下。 打开 Android Studio 安装目录下的bin目录,查找并打开文件 studio.exe.vmoptions,修改代码: 代码如下:-Xmx512m 为...
and flexible syntax, and the use of immutable data and function literals to solve prob‐ lems. Ruby and Python developers will be familiar with the use of function literals (aka closures or blocks) to...
JVM内核的各个组成部分 Runtime data areas shared among all threads: Method area: holds the details of each class loaded by the class loader subsystem. Heap: holds every object being created by the ...
Moonbox(月光宝盒)是JVM-Sandbox生态下的,基于jvm-sandbox-repeater重新开发的一款流量回放平台产品。在jvm-sandbox-repeater基础上提供了更加丰富功能,同时便于线上部署和使用,更多对比参考。 使用场景 你...
赠送jar包:metrics-jvm-3.1.5.jar; 赠送原API文档:metrics-jvm-3.1.5-javadoc.jar; 赠送源代码:metrics-jvm-3.1.5-sources.jar; 赠送Maven依赖信息文件:metrics-jvm-3.1.5.pom; 包含翻译后的API文档:...
jvm知识点整理-脑图
### JVM调优总结 #### 1. JVM配置 在Java应用程序的运行过程中,JVM(Java虚拟机)扮演着至关重要的角色。为了确保应用程序能够高效稳定地运行,正确配置JVM参数至关重要。以下是一些常见的JVM配置参数及其含义: ...
JVM调优是优化Java应用程序性能的关键环节,而"jvm-monitor"则提供了一种工具集来帮助开发者监控和分析JVM的状态。本文将深入探讨JVM调优的概念、方法以及如何利用"jvm-monitor"进行有效的监控。 一、JVM调优基础 ...
《JVM性能调优:深入理解JVM内存模型与优化》 在Java开发中,JVM(Java Virtual Machine)性能调优是提升应用程序效率的关键环节。JVM内存模型的理解和优化,对于解决性能瓶颈、避免内存泄漏以及提高系统稳定性至关...
1. JVM参数设置:包括内存分配、GC策略等。 2. 日志打印:通过特定参数输出JVM内部信息和GC日志。 3. 垃圾收集算法的理解与选择:如ParNew、CMS、G1等。 4. SpringBoot应用中的JVM优化配置。 5. 使用监控工具进行...
第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行时数据区-02第3节: 揭秘JVM运行...
标题 "框架->java语言->jvm->os->汇编->硬件" 描述了一条技术学习路径,从上至下深入理解软件开发的核心技术栈。这个路径涵盖了从应用框架到底层硬件的关键环节,让我们逐步解析其中涉及的知识点: 1. **框架**:...
jvm全面知识详解-全网最全(JVM面试题)
1. JVM结构: - 类装载器(ClassLoader):负责加载类文件到JVM,包括Bootstrap ClassLoader(引导类加载器)、Extension ClassLoader(扩展类加载器)和AppClass ClassLoader(应用程序类加载器)。 - 运行时数据...