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 中,释放内存的过程由垃圾接纳器主动处理。基本流程可以描述如下
1:标志清除法 Marking
标志:扫描出未被引用的对象
清除:对堆内存重新到尾进行线性遍历,接纳未被引用对象的内存
缺点:碎片化严峻
接纳前:
接纳后:
oracle官网描述:
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.
引用的对象以蓝色表现。未参照的对象以金色表现。在标志阶段扫描全部对象以做出此决定。如果必须扫描体系中的全部对象,这可能是一个非常耗时的过程。
2. 拷贝算法 Normal Deletion
oracle官网描述:
Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.
正常删除会删除未引用的对象,将引用的对象和指针保留到可用空间。
3.标志压缩算法 Deletion with Compacting
标志: 从根集合进行扫描,对存活对象进行标志
整理:移动全部存活的对象,且按照内存地址次序依次排列,然后将末端内存地址以后的内存全部接纳
oracle官网描述:
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.
为了进一步提高性能,除了删除未引用的对象外,还可以压缩剩余的引用对象。通过将引用的对象移动在一起,这使得新的内存分配变得更加轻易和快速。
为什么要进行分代接纳
oracle官网是这么说的:
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.
正如你所看到的,随着时间的推移,分配的对象越来越少。究竟上,大多数物体的寿命都非常短,如图左侧较高的值所示。
JVM 代
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
从对象分配行为中获取的信息可用于增强 JVM 的性能。因此,堆被分解成更小的部分或几代。堆部分是:年轻一代、老一代或终身一代以及永久一代
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.
停止世界变乱 - 全部次要垃圾接纳都是“停止世界”变乱。这意味着全部应用步调线程都将停止,直到操作完成。次要垃圾接纳始终是“停止世界”变乱。
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 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.
主要的垃圾网络也是停止世界变乱。通常,主要集合的速率要慢得多,由于它涉及全部活动对象。因此,对于响应式应用步调,应尽量淘汰主要垃圾接纳。另请注意,主要垃圾接纳的“停止世界”变乱的长度受用于旧代空间的垃圾网络器范例的影响。
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 发现不再需要类,并且可能需要空间来存储其他类,则可能会网络(卸载)类。永久生成包含在完备的垃圾接纳中。
GC的分类
Minor GC:年轻代的垃圾网络,接纳复制算法。实行较为频仍,尽可能快速地接纳生命周期短的对象。一般接纳速率较快。
Major GC: 老年代接纳。Major GC次数和时间指标很能表现体系性能问题。如果这两个指标很大,很大程度上阐明白步调中有问
题,垃圾不停接纳不掉。