Fundamentals of Garbage Collection

Fundamentals of Garbage Collection

1、Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.

2、Visual memory can be in three states:

  1)Free. The block of memory has no references to it and is available for allocation.

  2)Reserved. The block of memory is available for your use and cannot be used for any other allocation request. However, you cannot store data to this memory block until it is committed.

  3)Committed. The block of memory is assigned to physical storage.

3、Virtual address space can get fragmented. This means that there are free blocks, also known as holes, in the address space. When a virtual memory allocation is requested, the virtual memory manager has to find a single free block that is large enough to satisfy that allocation request. Even if you have 2 GB of free space, the allocation that requires 2 GB will be unsuccessful unless all of that space is in a single address block.

4、Conditions for a Garbage Collection.

  Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory.(被动)
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.(被动)
  • The GC. Collect method is called. (主动)

5、When a garbage collection is triggered, the garbage collector reclaims the memory that is occupied by dead objects. The reclaiming process compacts live objects so that they are moved together, and the dead space is removed, thereby making the heap smaller. This ensures that objects that are allocated together stay together on the managed heap, to preserve their locality.

6、The heap can be considered as the accumulation of two heaps: the large object heap and the small object heap.

   The large object heap contains objects that are 85,000 bytes and larger. Very large objects on the large object heap are usually arrays. It is rare for an instance object to be extremely large.

Generations

1、There are three generations of objects on the heap: 0、1、2。

2、Newly allocated objects form a new generation of objects and are implicitly generation 0 collections, unless they are large objects, in which case they go on the large object heap in a generation 2 collection.

3、每一次GC幸存下来的对象,会晋升到更高级的Generation。

4、Generation 0、1 也叫作 ephemeral generation。0、1必须在ephemeral segment中。ephemeral segment中可以有2.

5、The amount of freed memory from an ephemeral garbage collection is limited to the size of the ephemeral segment

What Happens During a Garbage Collection

  Before a garbage collection starts, all managed threads are suspended except for the thread that triggered the garbage collection

  

  When a finalizable object is discovered to be dead, its finalizer is put in a queue so that its cleanup actions are executed, but the object itself is promoted to the next generation. Therefore, you have to wait until the next garbage collection that occurs on that generation (which is not necessarily the next garbage collection) to determine whether the object has been reclaimed.

  GC分为2种:Workstation、Server。Server类有专门的GC线程。

  

  Workstation并发GC模型。此模型下,0、1同步完成,2在GC线程中完成。

    

  

  

  

时间: 2024-11-10 11:02:59

Fundamentals of Garbage Collection的相关文章

Tuning Java Garbage Collection for Spark Applicati

This is a guest post from our friends in the SSG STO Big Data Technology group at Intel. Join us at the Spark Summit to hear from Intel and other companies deploying Spark in production.  Use the code Databricks20 to receive a 20% discount! Spark is

从ASP.NET Core 3.0 preview 特性,了解CLR的Garbage Collection

前言 在阅读这篇文章:Announcing Net Core 3 Preview3的时候,我看到了这样一个特性: Docker and cgroup memory Limits We concluded that the primary fix is to set a GC heap maximum significantly lower than the overall memory limit as a default behavior. In retrospect, this choice

[Java] 垃圾回收机制 ( Garbage Collection ) 简介

自动垃圾回收( Automatic Garbage Collection ) 自动垃圾回收,是指在堆(Heap)内存上分辨哪些对象还在被使用,哪些对象没有被使用,并清除没有被使用的对象.所以,这里的垃圾实际上是指,在内存中,无法再被使用没有存在的价值的但还占据内存空间的对象. C 语言的内存分配.回收是需要手动完成的,但在 Java 中,回收内存是由垃圾回收器自动完成的. 垃圾回收分为两步骤:1.标记,2.删除.删除垃圾有两种情况,a. 常规删除,b. 带压缩的删除. 第 1 步. 标记 ( M

Advanced .NET Debugging: Managed Heap and Garbage Collection(转载,托管堆查内存碎片问题解决思路)

原文地址:http://www.informit.com/articles/article.aspx?p=1409801&seqNum=4 Debugging Managed Heap Fragmentation Earlier in the chapter, we described a phenomenon known as heap fragmentation, in which free and busy blocks are arranged and interleaved on th

GC(Garbage Collection)垃圾回收机制

1.在垃圾回收器中,程序员没有执行权,只有通知它的权利. 2.程序员可以通过System.gc().通知GC运行,但是Java规范并不能保证立刻运行. 3.finalize()方法,是java提供给程序员用来释放对象或资源的办法,但是尽量少用. 一.GC的介绍 GC的全称是Garbage Collection (垃圾收集) 在GC中,垃圾所指的是程序在运行过程中,会产生出一些无用的对象,或者说是已经被弃用的对象,而这些对象会占用着一部分的内存空间,如果长时间不去回收这些内存空间,那么最终会导致O

[Java] 垃圾回收 ( Garbage Collection ) 的步骤演示

关于 JVM 垃圾回收机制的基础内容,可参考上一篇博客 垃圾回收机制 ( Garbage Collection ) 简介 上一篇博客,介绍了堆的内存被分为三个部分:年轻代.老年代.永生代.这篇博文将演示这三个部分如何交互,实际也演示了垃圾回收. 1. 首先,所有新创建的对象都会陪分配到年轻代的 Eden 空间,而两个 survior 空间一开始都为空. 下图表示的是运行一段实际后的年轻代内存情况,新创建的对象会被放在 Eden 空间,"from" survior space 里面的数字

Valid page threshold based garbage collection for solid state drive

A method for garbage collection in a solid state drive (SSD) includes determining whether the SSD is idle by a garbage collection module of the SSD; based on determining that the SSD is idle, determining a victim block from a plurality of memory bloc

垃圾回收(garbage collection)介绍

 垃圾回收用来实现内存的自动管理(automatic management),区别于人工管理(manual management).人工管理内存容易出现的问题: 1)悬垂指针,dangling pointer 2)重复回收,Double free 3)内存泄露,memory leak 历史 垃圾回收的概念及技术由John McCarthy于1959年发明,应用于List语言中.1959年,计算机科学应该还处于探索.萌芽阶段,但这位大神已经发明了垃圾回收,大神就是大神. 策略 目前的垃圾回收策

Java Garbage Collection Basics--转载

原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose This tutorial covers the basics of how Garbage Collection works with the Hotspot JVM. Once you have learned how the garbage collector functions, lear