Springboot Apache vim vcpkg 郑州小程序公司 android富文本框架 spark大数据处理技术 js原生点击事件 office配置进度 ubuntu显示隐藏文件夹 java获取字符串 python中的循环 java中string java初学者 java接口实现 java接口的使用 java单继承 java配置文件 java环境下载 ntscan xp画图工具 pushstate 苹果5s降级 workflow中文 tar解压 pr调整图层 跑马灯动态壁纸 c语言从入门到精通 linux系统下载 一键root软件下载 冰冠堡垒单刷路线 主播音效 ssh框架原理及流程 sprutcam su模型交错 豌豆代理 剪影的意思 如何加入工作组 网卡驱动下载 飞鸽传书怎么用
当前位置: 首页 > 学习教程  > 编程语言

JVM入门阅读官网

2020/9/19 16:21:52 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

官网:https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html?spm=a2c4e.10696291.0.0.732b19a4rdAO43

1、what is Java Virtual Machine?

The Java Virtual Machine (JVM) is an abstract computing machine. The JVM is a program that looks like a machine to the programs written to execute in it. This way, Java programs are written to the same set of interfaces and libraries. Each JVM implementation for a specific operating system, translates the Java programming instructions into instructions and commands that run on the local operating system. This way, Java programs achieve platform independence.(Java虚拟机(JVM)是一种抽象的计算机器。JVM是一个程序,对于编写在其中执行的程序来说,它看起来像一台机器。通过这种方式,Java程序被写入相同的接口和库集。针对特定操作系统的每个JVM实现都将Java编程指令转换为在本地操作系统上运行的指令和命令。通过这种方式,Java程序实现了平台独立性。)

[http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-1.html]:

1.1、 JVM Architecture(JVM架构)

1.1.1、Hotspot Architecture

The HotSpot JVM possesses an architecture that supports a strong foundation of features and capabilities and supports the ability to realize high performance and massive scalability. For example, the HotSpot JVM JIT compilers generate dynamic optimizations. In other words, they make optimization decisions while the Java application is running and generate high-performing native machine instructions targeted for the underlying system architecture. In addition, through the maturing evolution and continuous engineering of its runtime environment and multithreaded garbage collector, the HotSpot JVM yields high scalability on even the largest available computer systems.(Java虚拟机(JVM)是一种抽象的计算机器。JVM是一个程序,对于编写在其中执行的程序来说,它看起来像一台机器。通过这种方式,Java程序被写入相同的接口和库集。针对特定操作系统的每个JVM实现都将Java编程指令转换为在本地操作系统上运行的指令和命令。通过这种方式,Java程序实现了平台独立性。)

The main components of the JVM include the classloader, the runtime data areas, and the execution engine.

1.1.2、Key Hotspot Components

The key components of the JVM that relate to performance are highlighted in the following image.

There are three components of the JVM that are focused on when tuning performance. The heap is where your object data is stored. This area is then managed by the garbage collector selected at startup. Most tuning options relate to sizing the heap and choosing the most appropriate garbage collector for your situation. The JIT compiler also has a big impact on performance but rarely requires tuning with the newer versions of the JVM.

1.2、Performance Basics

Typically, when tuning a Java application, the focus is on one of two main goals: responsiveness (响应能力)or throughput(吞吐量). We will refer back to these concepts as the tutorial progresses.

1.2.1、Responsiveness(响应)

Responsiveness refers to how quickly an application or system responds with a requested piece of data. Examples include:

  • How quickly a desktop UI responds to an event
  • How fast a website returns a page
  • How fast a database query is returned

For applications that focus on responsiveness, large pause times are not acceptable. The focus is on responding in short periods of time.

1.2.2、Throughput(吞吐量)

  1. Throughput focuses on maximizing the amount of work by an application in a specific period of time. Examples of how throughput might be measured include:

    • The number of transactions completed in a given time.
    • The number of jobs that a batch program can complete in an hour.
    • The number of database queries that can be completed in an hour.
  2. High pause times are acceptable for applications that focus on throughput. Since high throughput applications focus on benchmarks over longer periods of time, quick response time is not a consideration.

2、Describing Garbage Collection

2.1、What is Automatic Garbage Collection?

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.(自动垃圾收集是检查堆内存、确定哪些对象正在使用、哪些没有使用以及删除未使用的对象的过程。一个使用中的对象,或一个引用的对象,意味着你的程序的某些部分仍然保持一个指向那个对象的指针。未使用的对象或未引用的对象不再被程序的任何部分引用。因此,未引用对象使用的内存可以被回收。)

In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector. The basic process can be described as follows.(在像C这样的编程语言中,分配和释放内存是一个手工过程。在Java中,回收内存的过程是由垃圾收集器自动处理的。基本过程可以描述如下。)

Step 1: Marking

The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.(这个过程的第一步叫做打标。垃圾收集器在这里标识哪些内存正在使用,哪些没有。)

Referenced objects are shown in blue. Unreferenced objects are shown in gold. All objects are scanned in the marking phase to make this determination. This can be a very time consuming process if all objects in a system must be scanned.(引用的对象显示为蓝色。未引用的对象以金色显示。在标记阶段扫描所有对象以做出此判断。如果必须扫描系统中的所有对象,那么这将是一个非常耗时的过程。)

Step 2: Normal Deletion

Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.(通常删除未引用的对象,留下引用的对象和指向空闲空间的指针。)

The memory allocator holds references to blocks of free space where new object can be allocated.(内存分配器保存对可分配新对象的空闲空间块的引用。)

Step 2a: Deletion with Compacting

To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.(为了进一步提高性能,除了删除未引用的对象外,还可以压缩其余的引用对象。通过将引用的对象移动到一起,这使得新的内存分配更加简单和快速。)

2.2、Why Generational Garbage Collection?

As stated earlier, having to mark and compact all the objects in a JVM is inefficient. As more and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time. However, empirical analysis of applications has shown that most objects are short lived.(如前所述,必须标记和压缩JVM中的所有对象是低效的。随着分配的对象越来越多,对象列表会越来越长,导致垃圾收集时间越来越长。然而,对应用程序的实证分析表明,大多数对象都是短暂的。)

Here is an example of such data. The Y axis shows the number of bytes allocated and the X access shows the number of bytes allocated over time.(这里有一个这样的数据例子。Y轴显示分配的字节数,X访问显示随时间分配的字节数。)

As you can see, fewer and fewer objects remain allocated over time. In fact most objects have a very short life as shown by the higher values on the left side of the graph.(可以看到,随着时间的推移,分配的对象越来越少。事实上,大多数对象的寿命都很短,如图左侧较高的值所示。)

2.3、JVM Generations

The information learned from the object allocation behavior can be used to enhance the performance of the JVM. Therefore, the heap is broken up into smaller parts or generations. The heap parts are: Young Generation(新生代), Old or Tenured Generation(老年代), and Permanent Generation(永久代)

The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a *minor garbage collection*. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.(新生代是所有新对象被分配和老化的地方。当新生代填满时,这将导致一个小的垃圾收集。假设较高的对象死亡率,可以优化较小的收集。充满死对象的年轻一代被很快地收集起来。一些存活下来的对象是老的,最终移动到老年代。)

Stop the World Event - All minor garbage collections are “Stop the World” events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.(Stop the World Event——所有小垃圾收集都是“停止世界”事件。这意味着所有应用程序线程都将停止,直到操作完成。小垃圾收集总是Stop the World Event。)

The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a *major garbage collection*.(老年代用于存储长期存活的对象。通常,为年轻代对象设置一个阈值,当满足该年龄时,对象将被移动到老年代。最终需要收集老年代。此事件称为*major garbage collection*。)

Major garbage collection are also Stop the World events. Often a major collection is much slower because it involves all live objects. So for Responsive applications, major garbage collections should be minimized. Also note, that the length of the Stop the World event for a major garbage collection is affected by the kind of garbage collector that is used for the old generation space.(major garbage collection也是停止世界事件。通常,major garbage collection要慢得多,因为它涉及到所有活动对象。因此,对于响应性应用程序,应该尽量减少major garbage collection。还请注意,major garbage collection的Stop the World事件的长度受老年代空间使用的垃圾收集器类型的影响。)

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.(永久代成包含JVM描述应用程序中使用的类和方法所需的元数据。永久生成是由JVM在运行时根据应用程序所使用的类填充的。另外,Java SE库类和方法可以存储在这里。)

Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.(如果JVM发现不再需要类,并且其他类可能需要空间,则类可能会被收集(卸载)。永久生成包含在一个完整的垃圾收集中。)

Tuning Garbage Collection with the 5.0 Java Virtual Machine

https://www.oracle.com/java/technologies/tuning-garbage-collection-v50-java-virtual-machine.html

3、The Generational Garbage Collection Process

Now that you understand why the heap is separted into different generations, it is time to look at how exactly these spaces interact. The pictures that follow walks through the object allocation and aging process in the JVM.(既然您已经理解了为什么将堆划分为不同的代,现在就来看看这些空间到底是如何交互的。下面的图片介绍了JVM中的对象分配和老化过程。)

1、First, any new objects are allocated to the eden space. Both survivor spaces start out empty.(首先,将所有新对象分配到eden空间。两个幸存者空间一开始都是空的。)

2、When the eden space fills up, a minor garbage collection is triggered.

3、Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.(引用的对象被移动到第一个幸存者空间。当eden空间被清除时,未引用的对象将被删除。)

4、At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.(在下一次小GC中,eden空间也会发生同样的事情。删除未引用的对象,并将引用的对象移动到幸存者空间。但是,在本例中,它们被移动到第二个幸存者空间(S1)。此外,来自第一个幸存者空间(S0)上最后一次小GC的对象的年龄将增加并移动到S1。一旦所有存活的对象被移动到S1, S0和eden都将被清除。注意,我们现在在幸存者空间中使用了不同的old对象。)

5、At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.(在下一个小GC中,重复同样的过程。不过,这次幸存者空间切换了。引用的对象被移动到S0。幸存的对象是陈旧的。Eden和S1被清除。)

6、This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.(这张幻灯片展示了推广。在一次小GC之后,当老年对象达到某个年龄阈值(本例中为8)时,它们将从年轻代提升到老年代。)

7、As minor GCs continue to occure objects will continue to be promoted to the old generation space.(当较小的gc继续运行时,occure对象将继续被提升到旧的代空间。)

8、So that pretty much covers the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.(这几乎涵盖了年轻一代的整个过程。最后,将在旧代上执行一次主GC,以清理和压缩空间。)

4、Start VisualVM

Install Visual GC

5、Java Garbage Collectors

You now know the basics of garbage collection and have observed the garbage collector in action on a sample application. In this section, you will learn about the garbage collectors available for Java and the command line switches you need to select them.(现在您了解了垃圾收集的基本知识,并观察了垃圾收集器在样例应用程序中的作用。在本节中,您将了解Java可用的垃圾收集器和选择它们所需的命令行开关。)

5.1、Common Heap Related Switches(堆相关命令)

There are many different command line switches that can be used with Java. This section describes some of the more commonly used switches that are also used in this OBE

  1. SwitchDescription
    -XmsSets the initial heap size for when the JVM starts.
    -XmxSets the maximum heap size.
    -XmnSets the size of the Young Generation.
    -XX:PermSizeSets the starting size of the Permanent Generation.
    -XX:MaxPermSizeSets the maximum size of the Permanent Generation

5.2、The Serial GC(串行化GC)

The serial collector is the default for client style machines in Java SE 5 and 6. With the serial collector, both minor and major garbage collections are done serially (using a single virtual CPU). In addition, it uses a mark-compact collection method. This method moves older memory to the beginning of the heap so that new memory allocations are made into a single continuous chunk of memory at the end of the heap. This compacting of memory makes it faster to allocate new chunks of memory to the heap.(在Java SE 5和6中,串行收集器是客户端样式的机器的默认值。使用串行收集器,次要和主要垃圾收集都是串行进行的(使用单个虚拟CPU)。此外,它使用标记-压缩集合方法。该方法将较旧的内存移到堆的开头,以便在堆的末尾将新的内存分配成一个连续的内存块。这种内存压缩使分配新内存块给堆的速度更快。)

Usage Cases

The Serial GC is the garbage collector of choice for most applications that do not have low pause time requirements and run on client-style machines. It takes advantage of only a single virtual processor for garbage collection work (therefore, its name). Still, on today’s hardware, the Serial GC can efficiently manage a lot of non-trivial applications with a few hundred MBs of Java heap, with relatively short worst-case pauses (around a couple of seconds for full garbage collections).(对于大多数暂停时间要求不高且运行在客户机类型机器上的应用程序来说,串行GC是首选的垃圾收集器。它只利用一个虚拟处理器来进行垃圾收集工作(因此,它的名字就是这样的)。不过,在今天的硬件上,串行GC可以有效地管理大量具有几百mb Java堆的重要应用程序,最坏情况下暂停时间相对较短(对于完全的垃圾收集,大约只有几秒钟)。)

Another popular use for the Serial GC is in environments where a high number of JVMs are run on the same machine (in some cases, more JVMs than available processors!). In such environments when a JVM does a garbage collection it is better to use only one processor to minimize the interference on the remaining JVMs, even if the garbage collection might last longer. And the Serial GC fits this trade-off nicely.(串行GC的另一个流行用途是在相同机器上运行大量jvm的环境中(在某些情况下,jvm比可用的处理器还要多!)在这样的环境中,当JVM进行垃圾收集时,最好只使用一个处理器,以最大限度地减少对其余JVM的干扰,即使垃圾收集可能持续更长时间。而串行GC很适合这种权衡。)

Finally, with the proliferation of embedded hardware with minimal memory and few cores, the Serial GC could make a comeback.(最后,随着具有最小内存和少数核心的嵌入式硬件的激增,串行GC可能会卷土重来。)

Command Line Switches

To enable the Serial Collector use:
-XX:+UseSerialGC

Here is a sample command line for starting the Java2Demo:
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

5.3、The Parallel GC(并行GC)

The parallel garbage collector uses multiple threads to perform the young genertion garbage collection. By default on a host with N CPUs, the parallel garbage collector uses N garbage collector threads in the collection. (并行垃圾收集器使用多个线程来执行年轻代垃圾收集。默认情况下,在一个有N个cpu的主机上,并行垃圾收集器在收集中使用N个垃圾收集器线程。)

The number of garbage collector threads can be controlled with command-line options:(垃圾回收线程的数量可以通过命令行选项控制:)

-XX:ParallelGCThreads=

On a host with a single CPU the default garbage collector is used even if the parallel garbage collector has been requested. On a host with two CPUs the parallel garbage collector generally performs as well as the default garbage collector and a reduction in the young generationgarbage collector pause times can be expected on hosts with more than two CPUs. The Parallel GC comes in two flavors.(在具有单个CPU的主机上,即使请求了并行垃圾收集器,也会使用默认的垃圾收集器。在有两个cpu的主机上,并行垃圾收集器的性能通常和默认垃圾收集器一样好,在有两个以上cpu的主机上,预计年轻生成垃圾收集器暂停时间会减少。并行GC有两种方式。)

Usage Cases

The Parallel collector is also called a throughput collector. Since it can use multilple CPUs to speed up application throughput. This collector should be used when a lot of work need to be done and long pauses are acceptable. For example, batch processing like printing reports or bills or performing a large number of database queries.(并行收集器也称为吞吐量收集器。因为它可以使用多cpu来提高应用程序吞吐量。当需要完成大量工作并且可以接受长时间暂停时,应该使用此收集器。例如,批处理,如打印报告或账单,或执行大量数据库查询。)

Command Line

-XX:+UseParallelGC

With this command line option you get a multi-thread young generation collector with a single-threaded old generation collector. The option also does single-threaded compaction of old generation.(使用这个命令行选项,您将获得一个多线程年轻代收集器和一个单线程老代收集器。该选项还对旧代进行单线程压缩。)

Here is a sample command line for starting the Java2Demo:
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

-XX:+UseParallelOldGC

With the -XX:+UseParallelOldGC option, the GC is both a multithreaded young generation collector and multithreaded old generation collector. It is also a multithreaded compacting collector. HotSpot does compaction only in the old generation. Young generation in HotSpot is considered a copy collector; therefore, there is no need for compaction.(使用-XX:+UseParallelOldGC选项,GC既可以是多线程年轻代收集器,也可以是多线程老代收集器。它也是一个多线程压缩收集器。HotSpot只在老一代中进行压缩。HotSpot中的年轻一代被认为是拷贝收集器;因此,没有必要进行压缩。)

Compacting describes the act of moving objects in a way that there are no holes between objects. After a garbage collection sweep, there may be holes left between live objects. Compacting moves objects so that there are no remaining holes. It is possible that a garbage collector be a non-compacting collector. Therefore, the difference between a parallel collector and a parallel compacting collector could be the latter compacts the space after a garbage collection sweep. The former would not.(压缩描述了以在对象之间不存在空隙的方式移动对象的行为。在垃圾收集清理之后,活动对象之间可能会留下一些洞。压缩移动物体,使其没有剩余的孔。垃圾收集器可能是非压缩收集器。因此,并行收集器和并行压缩收集器之间的区别可能是后者在垃圾收集清理后压缩空间。前者不会。)

Here is a sample command line for starting the Java2Demo:
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelOldGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

5.4、The Concurrent Mark Sweep (CMS) Collector(并发标记清除)

The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause collector) collects the tenured generation. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.(并发标记清理(CMS)收集器(也称为并发低暂停收集器)收集保留期生成。它试图通过与应用程序线程并发地执行大部分垃圾收集工作来最小化垃圾收集造成的暂停。通常,并发低暂停收集器不会复制或压缩活动对象。在不移动活动对象的情况下进行垃圾收集。如果碎片成为一个问题,分配一个更大的堆。)

Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.(年轻代的CMS收集器与并行收集器使用相同的算法。)

Usage Cases

The CMS collector should be used for applications that require low pause times and can share resources with the garbage collector. Examples include desktop UI application that respond to events, a webserver responding to a request or a database responding to queries.(CMS收集器应该用于需要较低暂停时间并且可以与垃圾收集器共享资源的应用程序。示例包括响应事件的桌面UI应用程序、响应请求的web服务器或响应查询的数据库。)

Command Line Switches

To enable the CMS Collector use:
-XX:+UseConcMarkSweepGC
and to set the number of threads use:
-XX:ParallelCMSThreads=

Here is a sample command line for starting the Java2Demo:
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=2 -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

5.5、The G1 Garbage Collector

The Garbage First or G1 garbage collector is available in Java 7 and is designed to be the long term replacement for the CMS collector. The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector that has quite a different layout from the other garbage collectors described previously. However, detailed discussion is beyond the scope of this OBE.(Garbage First或G1垃圾收集器在Java 7中可用,其设计目的是长期替代CMS收集器。G1收集器是一个并行、并发、增量压缩的低暂停垃圾收集器,它的布局与前面描述的其他垃圾收集器完全不同。然而,详细的讨论超出了这个OBE的范围。)

Command Line Switches

  1. To enable the G1 Collector use:
    -XX:+UseG1GC

  2. Here is a sample command line for starting the Java2Demo:
    java -Xmx12m -Xms3m -XX:+UseG1GC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

6、Summary

  1. In this tutorial, you have learned:

    • The components of the Java JVM
    • How automatic garbage collection works
    • The generational garbage collection process
    • How to monitor your JVM with Visual VM
      c\Java2D\Java2demo.jar`

6、Summary

  1. In this tutorial, you have learned:

    • The components of the Java JVM
    • How automatic garbage collection works
    • The generational garbage collection process
    • How to monitor your JVM with Visual VM
    • The types of garbage collectors available on the JVM

本文链接: http://www.dtmao.cc/news_show_200400.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?