Mark-and-Sweep Garbage Collection

This section presents the mark-and-sweep   garbage collection algorithm. The mark-and-sweep algorithm was the first garbage collection algorithm to be developed that is able to reclaim cyclic data structures.gif Variations of the mark-and-sweep algorithm continue to be among the most commonly used garbage collection techniques.

When using mark-and-sweep, unreferenced objects are not reclaimed immediately. Instead, garbage is allowed to accumulate until all available memory has been exhausted. When that happens, the execution of the program is suspended temporarily while the mark-and-sweep algorithm collects all the garbage. Once all unreferenced objects have been reclaimed, the normal execution of the program can resume.

The mark-and-sweep algorithm is called a tracing garbage collector because is traces out the entire collection of objects that are directly or indirectly accessible by the program. The objects that a program can access directly are those objects which are referenced by local variables on the processor stack as well as by any static variables that refer to objects. In the context of garbage collection, these variables are called the roots . An object is indirectly accessible if it is referenced by a field in some other (directly or indirectly) accessible object. An accessible object is said to be live . Conversely, an object which is not live is garbage.

The mark-and-sweep algorithm consists of two phases: In the first phase, it finds and marks all accessible objects. The first phase is called the mark phase. In the second phase, the garbage collection algorithm scans through the heap and reclaims all the unmarked objects. The second phase is called the sweep phase. The algorithm can be expressed as follows:

for each root variable r
    mark (r);
sweep ();

In order to distinguish the live objects from garbage, we record the state of an object in each object. That is, we add a special boolean field to each object called, say, marked. By default, all objects are unmarked when they are created. Thus, the marked field is initially false.

An object p and all the objects indirectly accessible from p can be marked by using the following recursive mark method:

void mark (Object p)

if (!p.marked)

p.marked = true; for each Object q referenced by p mark (q);

Notice that this recursive  mark  algorithm does nothing when it encounters an object that has already been marked. Consequently, the algorithm is guaranteed to terminate. And it terminates only when all accessible objects have been marked.

In its second phase, the mark-and-sweep algorithm scans through all the objects in the heap, in order to locate all the unmarked objects. The storage allocated to the unmarked objects is reclaimed during the scan. At the same time, the marked field on every live object is set back to false in preparation for the next invocation of the mark-and-sweep garbage collection algorithm:

void sweep ()

for each Object p in the heap

if (p.marked) p.marked = false else heap.release (p);

Figure gif illustrates the operation of the mark-and-sweep garbage collection algorithm. Figure gif (a) shows the conditions before garbage collection begins. In this example, there is a single root variable. Figure gif (b) shows the effect of the mark phase of the algorithm. At this point, all live objects have been marked. Finally, Figure gif (c) shows the objects left after the sweep phase has been completed. Only live objects remain in memory and the marked fields have all been set to false again.

     

 
Figure: Mark-and-sweep garbage collection.

Because the mark-and-sweep garbage collection algorithm traces out the set of objects accessible from the roots, it is able to correctly identify and collect garbage even in the presence of reference cycles. This is the main advantage of mark-and-sweep over the reference counting technique presented in the preceding section. A secondary benefit of the mark-and-sweep approach is that the normal manipulations of reference variables incurs no overhead.

The main disadvantage of the mark-and-sweep approach is the fact that that normal program execution is suspended while the garbage collection algorithm runs. In particular, this can be a problem in a program that interacts with a human user or that must satisfy real-time execution constraints. For example, an interactive application that uses mark-and-sweep garbage collection becomes unresponsive periodically.

Original address: <LINK>

The download address of the book: <LINK>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java应用程序的监控可以通过Open-Falcon进行配置和实现。下面是一些详细的配置步骤: 1. 下载和安装Open-Falcon Agent 在Java应用程序所在服务器上下载和安装Open-Falcon Agent。 2. 配置Open-Falcon Agent 修改Open-Falcon Agent配置文件,添加如下配置项: ``` { "plugin": { "enabled": true, "dir": "/usr/local/open-falcon/agent/plugin" }, "heartbeat": { "enabled": true, "addr": "127.0.0.1:6030", "interval": 60, "timeout": 1000 }, "transfer": { "enabled": true, "addrs": [ "127.0.0.1:8433" ], "interval": 60, "timeout": 1000 } } ``` 其中,`plugin.enabled`设置为`true`表示启用插件,`plugin.dir`设置插件所在目录。 3. 下载和安装Java插件 在Open-Falcon Agent所在服务器上下载和安装Java插件。 4. 配置Java插件 修改Java插件配置文件,添加如下配置项: ``` { "debug": true, "metric": { "step": 60, "prefix": "java." }, "plugin": { "jvm": { "enabled": true, "bin": "/usr/local/java/bin/java", "option": "-classpath ${plugin_dir}/java-plugin.jar", "jmx_url": "service:jmx:rmi:///jndi/rmi://127.0.0.1:1099/jmxrmi", "metrics": [ { "name": "jvm.mem.heap_used", "type": "GAUGE", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage.used" }, { "name": "jvm.mem.heap_committed", "type": "GAUGE", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage.committed" }, { "name": "jvm.mem.heap_max", "type": "GAUGE", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage.max" }, { "name": "jvm.mem.non_heap_used", "type": "GAUGE", "mbean": "java.lang:type=Memory", "attribute": "NonHeapMemoryUsage.used" }, { "name": "jvm.mem.non_heap_committed", "type": "GAUGE", "mbean": "java.lang:type=Memory", "attribute": "NonHeapMemoryUsage.committed" }, { "name": "jvm.threads.count", "type": "GAUGE", "mbean": "java.lang:type=Threading", "attribute": "ThreadCount" }, { "name": "jvm.threads.daemon_count", "type": "GAUGE", "mbean": "java.lang:type=Threading", "attribute": "DaemonThreadCount" }, { "name": "jvm.threads.peak_count", "type": "GAUGE", "mbean": "java.lang:type=Threading", "attribute": "PeakThreadCount" }, { "name": "jvm.gc.young_count", "type": "COUNTER", "mbean": "java.lang:type=GarbageCollector,name=PS Scavenge", "attribute": "CollectionCount" }, { "name": "jvm.gc.young_time", "type": "COUNTER", "mbean": "java.lang:type=GarbageCollector,name=PS Scavenge", "attribute": "CollectionTime" }, { "name": "jvm.gc.old_count", "type": "COUNTER", "mbean": "java.lang:type=GarbageCollector,name=PS MarkSweep", "attribute": "CollectionCount" }, { "name": "jvm.gc.old_time", "type": "COUNTER", "mbean": "java.lang:type=GarbageCollector,name=PS MarkSweep", "attribute": "CollectionTime" } ] } } } ``` 其中,`plugin.jvm.enabled`设置为`true`表示启用Java插件,`plugin.jvm.bin`设置Java程序所在路径,`plugin.jvm.option`设置Java插件所在路径,`plugin.jvm.jmx_url`设置JMX URL,`plugin.jvm.metrics`设置监控指标。 5. 重启Open-Falcon Agent 修改完Open-Falcon Agent和Java插件的配置文件后,需要重启Open-Falcon Agent。 6. 查看监控数据 在Open-Falcon Web界面上查看Java应用程序的监控数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值