标签归档:Garbage Collection

浅谈Java中的垃圾回收机制

1.垃圾回收是一种动态存储管理技术,它自动地释放不再被程序引用的对象,按照特定的垃圾收集算法来实现资源自动回收的功能。当一个对象不再被引用的时候,内存回收它占领的空间,以便空间被后来的新对象使用。
事实上,除了释放没用的对象,垃圾收集也可以清除内存记录碎片。
2.但是任何一种垃圾收集算法一般要做2件基本的事情:(1)发现无用信息对象;(2)回收被无用对象占用的内存空间,使该空间可被程序再次使用。
如何来发现无用的信息对象呢?如何来回收呢?
3.大多数垃圾回收算法使用了根集(root set)这个概念;所谓根集就量正在执行的Java程序可以访问的引用变量的集合(包括局部变量、参数、类变量),程序可以使用引用变量访问对象的属性和调用对象的方法。垃圾收集首选需要确定从根开始哪些是可达的和哪些是不可达的,从根集可达的对象都是活动对象,它们不能作为垃圾被回收,这也包括从根集间接可达的对象。而根集通过任意路径不可达的对象符合垃圾收集的条件,应该被回收。 继续阅读

Pick up performance with generational garbage collection

Garbage collection (GC) reclaims the heap space previously allocated to objects no longer needed. The process of locating and removing those dead objects can stall your Java application while consuming as much as 25 percent of throughput.

Sun Microsystems introduced generational GC in the Java HotSpot VM for Solaris. Generational GC separates older and newer objects into separate heap spaces. With command line parameters, you control how the HotSpot JVM uses that heap space to perform GC. HotSpot’s default parameters are effective for most small applications that require faster startup and a smaller footprint. But you can select parameters that activate the Java HotSpot Server VM to improve the throughput of large, server-side applications, like those running under BEA’s WebLogic, by 20 percent or more. 继续阅读

Inside JVM–GC(Garbage collection)

一、Common Problems in Java SE Application

This message is based on the following materials.
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/#Insufficient_Memory
http://java.sun.com/developer/technicalArticles/javase/troubleshoot//

1. Java Memory Basic

1.1 Heap, Non-Heap, and Native

Heap memory is the runtime data area from which memory for all class instances and arrays is allocated.
Non-heap memory includes the method area and memory required for the internal processing or optimization of the JVM.
It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors.
Native memory is the virtual memory managed by the operating system. When the memory is insufficient for an application to allocate, a java.lang.OutOfMemoryError will be thrown. 继续阅读

Garbage Collection实践总结

Available Collectors

The discussion to this point has been about the serial collector. The Java HotSpot VM includes three different collectors, each with different performance characteristics.

  1. The serial collector uses a single thread to perform all garbage collection work, which makes it relatively efficient since there is no communication overhead between threads. It is best-suited to single processor machines, since it cannot take advantage of multiprocessor hardware, although it can be useful on multiprocessors for applications with small data sets (up to approximately 100MB). The serial collector is selected by default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseSerialGC.
  2. The parallel collector (also known as the throughput collector) performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium- to large-sized data sets that are run on multiprocessor or multi-threaded hardware. The parallel collector is selected by default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseParallelGC.
    • New: parallel compaction is a feature introduced in J2SE 5.0 update 6 and enhanced in Java SE 6 that allows the parallel collector to perform major collections in parallel. Without parallel compaction, major collections are performed using a single thread, which can significantly limit scalability. Parallel compaction is enabled by adding the option -XX:+UseParallelOldGC to the command line.
  3. The concurrent collector performs most of its work concurrently (i.e., while the application is still running) to keep garbage collection pauses short. It is designed for applications with medium- to large-sized data sets for which response time is more important than overall throughput, since the techniques used to minimize pauses can reduce application performance. The concurrent collector is enabled with the option -XX:+UseConcMarkSweepGC.

继续阅读

JVM Garbage Collection

JVM的自动垃圾收集(Garbage Collection)使得开发人员无需关注垃圾收集的细节,不过,当内存问题成为系统瓶颈的时候,我们就需要了解一下JVM的垃圾收集机制了。

应用程序中生成的对象绝大部分都是临时对象,属于那种生的快死的快的,来也匆匆,去也匆匆,当然也有伴随应用程序的生命周期而存在的对象,鉴于对象的生命周期的不同,JVM的内存是分代(Generation)管理的。如果把JVM看作一个战场,把各个对象看作士兵,那么大部分的士兵刚投入战场不久就英勇就义,只有少部分的士兵能够在战场中摸爬滚打能够坚持一段时间。(史上最残忍的战场,阿弥陀佛,善哉!善哉!)。分代情况如下图所示:

default arrangement of generations

其中,年轻代(Young Generation)包含伊甸园(Eden)和2段幸存者空间(Survivor Spaces),伊甸园当然是新生对象的乐园(只在乎曾经拥有,不在乎天长地久。也许看作前线战场更为合适:)(也有部分大对象直接分配在年老代)。而幸存者空间是用来保存上次GC之后依然存活的对象,经过战事的洗礼,能够存活下来实属不易,说明你是一个优秀的士兵,特此晋升一下,并且分配给你一间前线作战指挥所。2段空间保持其中一个为空,后面再详细说明缘由和作用。 继续阅读