JVM GC Collector

本文介绍了Java中各种垃圾回收器的工作原理及其组合使用方式。包括Serial、ParNew、ParallelScavenge等年轻代回收器和Serial Old、CMS、Parallel Old等老年代回收器,并探讨了新一代垃圾回收器G1的特点与优势。

FROM: https://blogs.oracle.com/jonthecollector/our-collectors

 

I drew this diagram on a white board for some customers recently. They seemed to
like it (or were just being very polite) so I thought I redraw it for your
amusement.

Each blue box represents a collector that is used to collect a generation. The 
young generation is collected by the blue boxes in the yellow region and
the tenured generation is collected by the blue boxes in the gray region.

 


  • "Serial" is a stop-the-world, copying collector which uses a single GC thread.

  • "ParNew" is a stop-the-world, copying collector which uses multiple GC threads. It differs
    from "Parallel Scavenge" in that it has enhancements that make it usable 
    with CMS. For example, "ParNew" does the
    synchronization needed so that it can run during the 
    concurrent phases of CMS.

  • "Parallel Scavenge" is a stop-the-world, copying collector 
    which uses multiple GC threads.

  • "Serial Old" is a stop-the-world,
    mark-sweep-compact collector that uses a single GC thread.

  • "CMS" is a mostly concurrent, low-pause collector.

  • "Parallel Old" is a compacting collector that uses multiple GC threads.

    Using the -XX flags for our collectors for jdk6,

     


  • UseSerialGC is "Serial" + "Serial Old"

  • UseParNewGC is "ParNew" + "Serial Old"

  • UseConcMarkSweepGC is "ParNew" + "CMS" + "Serial Old". "CMS" is used most of the time to collect the tenured generation. "Serial Old" is used when a concurrent mode failure occurs.

  • UseParallelGC is "Parallel Scavenge" + "Serial Old"

  • UseParallelOldGC is "Parallel Scavenge" + "Parallel Old"

    FAQ

    1) UseParNew and UseParallelGC both collect the young generation using
    multiple GC threads. Which is faster?

    There's no one correct answer for
    this questions. Mostly they perform equally well, but I've seen one
    do better than the other in different situations. If you want to use
    GC ergonomics, it is only supported by UseParallelGC (and UseParallelOldGC)
    so that's what you'll have to use.

    2) Why doesn't "ParNew" and "Parallel Old" work together?

    "ParNew" is written in
    a style where each generation being collected offers certain interfaces for its
    collection. For example, "ParNew" (and "Serial") implements
    space_iterate() which will apply an operation to every object
    in the young generation. When collecting the tenured generation with
    either "CMS" or "Serial Old", the GC can use space_iterate() to 
    do some work on the objects in the young generation. 
    This makes the mix-and-match of collectors work but adds some burden
    to the maintenance of the collectors and to the addition of new
    collectors. And the burden seems to be quadratic in the number
    of collectors.
    Alternatively, "Parallel Scavenge"
    (at least with its initial implementation before "Parallel Old")
    always knew how the tenured generation was being collected and
    could call directly into the code in the "Serial Old" collector.
    "Parallel Old" is not written in the "ParNew" style so matching it with
    "ParNew" doesn't just happen without significant work.
    By the way, we would like to match "Parallel Scavenge" only with
    "Parallel Old" eventually and clean up any of the ad hoc code needed
    for "Parallel Scavenge" to work with both.

    Please don't think too much about the examples I used above. They
    are admittedly contrived and not worth your time.

    3) How do I use "CMS" with "Serial"?

    -XX:+UseConcMarkSweepGC -XX:-UseParNewGC.
    Don't use -XX:+UseConcMarkSweepGC and -XX:+UseSerialGC. Although that's seems like 
    a logical combination, it will result in a message saying something about 
    conflicting collector combinations and the JVM won't start. Sorry about that.
    Our bad.

    4) Is the blue box with the "?" a typo?

    That box represents the new garbage collector that we're currently developing called
    Garbage First or G1 for short. G1 will provide


  • More predictable GC pauses

  • Better GC ergonomics

  • Low pauses without fragmentation

  • Parallelism and concurrency in collections

  • Better heap utilization

    G1 straddles the young generation - tenured generation boundary because it is
    a generational collector only in the logical sense. G1 divides the
    heap into regions and during a GC can collect a subset of the regions.
    It is logically generational because it dynamically selects a set of
    regions to act as a young generation which will then be collected at
    the next GC (as the young generation would be).

    The user can specify a goal for the pauses and G1 
    will do an estimate (based on past collections) of how many 
    regions can be collected in that time (the pause goal). 
    That set of regions is called a collection set and G1 will
    collect it during the next GC.

    G1 can choose the regions with the most garbage to collect first (Garbage First, get it?)
    so gets the biggest bang for the collection buck.

    G1 compacts so fragmentation is much less a problem. Why is it a problem at all?
    There can be internal fragmentation due to partially filled regions.

    The heap is not statically divided into 
    a young generation and a tenured generation so the problem of
    an imbalance in their sizes is not there.

    Along with a pause time goal the user can specify a goal on the fraction of
    time that can be spent on GC during some period (e.g., during the next 100 seconds
    don't spend more than 10 seconds collecting). For such goals (10 seconds of
    GC in a 100 second period) G1 can choose a collection set that it expects it can collect in 10 seconds and schedules the collection 90 seconds (or more) from the previous collection. You can see how an evil user could specify 0 collection
    time in the next century so again, this is just a goal, 
    not a promise.

    If G1 works out as we expect, it will become our low-pause collector in place of 
    "ParNew" + "CMS". And if you're about to ask when will it be ready, please don't
    be offended by my dead silence. It's the highest priority project for our team,
    but it is software development so there are the usual unknowns. It will be out
    by JDK7. The sooner the better as far as we're concerned.

    Updated February 4. Yes, I can edit an already posted blog. Here's
    a reference to the G1 paper if you have ACM portal access.

    http://portal.acm.org/citation.cfm?id=1029879

内容概要:文章以“智能网页数据标注工具”为例,深入探讨了谷歌浏览器扩展在毕业设计中的实战应用。通过开发具备实体识别、情感分类等功能的浏览器扩展,学生能够融合前端开发、自然语言处理(NLP)、本地存储与模型推理等技术,实现高效的网页数据标注系统。文中详细解析了扩展的技术架构,涵盖Manifest V3配置、内容脚本与Service Worker协作、TensorFlow.js模型在浏览器端的轻量化部署与推理流程,并提供了核心代码实现,包括文本选择、标注工具栏动态生成、高亮显示及模型预测功能。同时展望了多模态标注、主动学习与边缘计算协同等未来发展方向。; 适合人群:具备前端开发基础、熟悉JavaScript和浏览器机制,有一定AI模型应用经验的计算机相关专业本科生或研究生,尤其适合将浏览器扩展与人工智能结合进行毕业设计的学生。; 使用场景及目标:①掌握浏览器扩展开发全流程,理解内容脚本、Service Worker与弹出页的通信机制;②实现在浏览器端运行轻量级AI模型(如NER、情感分析)的技术方案;③构建可用于真实场景的数据标注工具,提升标注效率并探索主动学习、协同标注等智能化功能。; 阅读建议:建议结合代码实例搭建开发环境,逐步实现标注功能并集成本地模型推理。重点关注模型轻量化、内存管理与DOM操作的稳定性,在实践中理解浏览器扩展的安全机制与性能优化策略。
基于Gin+GORM+Casbin+Vue.js的权限管理系统是一个采用前后端分离架构的企业级权限管理解决方案,专为软件工程和计算机科学专业的毕业设计项目开发。该系统基于Go语言构建后端服务,结合Vue.js前端框架,实现了完整的权限控制和管理功能,适用于各类需要精细化权限管理的应用场景。 系统后端采用Gin作为Web框架,提供高性能的HTTP服务;使用GORM作为ORM框架,简化数据库操作;集成Casbin实现灵活的权限控制模型。前端基于vue-element-admin模板开发,提供现代化的用户界面和交互体验。系统采用分层架构和模块化设计,确保代码的可维护性和可扩展性。 主要功能包括用户管理、角色管理、权限管理、菜单管理、操作日志等核心模块。用户管理模块支持用户信息的增删改查和状态管理;角色管理模块允许定义不同角色并分配相应权限;权限管理模块基于Casbin实现细粒度的访问控制;菜单管理模块动态生成前端导航菜单;操作日志模块记录系统关键操作,便于审计和追踪。 技术栈方面,后端使用Go语言开发,结合Gin、GORM、Casbin等成熟框架;前端使用Vue.js、Element UI等现代前端技术;数据库支持MySQL、PostgreSQL等主流关系型数据库;采用RESTful API设计规范,确保前后端通信的标准化。系统还应用了单例模式、工厂模式、依赖注入等设计模式,提升代码质量和可测试性。 该权限管理系统适用于企业管理系统、内部办公平台、多租户SaaS应用等需要复杂权限控制的场景。作为毕业设计项目,它提供了完整的源码和论文文档,帮助学生深入理解前后端分离架构、权限控制原理、现代Web开发技术等关键知识点。系统设计规范,代码结构清晰,注释完整,非常适合作为计算机相关专业的毕业设计参考或实际项目开发的基础框架。 资源包含完整的系统源码、数据库设计文档、部署说明和毕
03-20
### JVM 垃圾回收调优与性能优化 JVM 的垃圾回收 (GC) 调优对于提升应用程序的吞吐量和降低延迟至关重要。以下是关于如何通过调整堆大小、选择合适的垃圾收集器以及减少内存碎片来实现性能优化的关键点。 #### 1. 堆内存管理 堆是存储对象数据的地方,其大小直接影响垃圾回收的行为。通常情况下,较大的堆可以容纳更多的对象,从而减少 GC 频率;然而,过大的堆会增加每次 GC 所需的时间[^1]。因此,在配置堆大小时需要权衡频率和持续时间之间的关系。 #### 2. 垃圾收集器的选择 不同的应用需求适合不同类型的垃圾收集器: - **Serial Garbage Collector**: 单线程执行,适用于小型单核环境下的简单应用场景。 - **Parallel Garbage Collector (也称为 Throughput Collector)**: 使用多线程并行工作,旨在最大化 CPU 利用率以提高整体吞吐量[^5]。它是一个停止世界型复制收集器,意味着在运行期间整个程序会被暂停以便完成垃圾清理操作。 - **CMS (Concurrent Mark Sweep) Garbage Collector**: 设计用于低延时场景,尝试尽可能多地并发处理标记清扫阶段的工作而不停止应用线程[^4]。 - **G1 (Garbage First) Garbage Collector**: 结合了高吞吐量和可控停顿的优点,特别推荐给那些既追求效率又希望保持响应速度的应用程序[^3]。 每种垃圾收集策略都有各自的优势和局限性,具体选用哪一种取决于实际业务的需求——比如是否更看重系统的反应灵敏度还是总的事务处理能力。 #### 3. 减少内存分配压力 除了合理设置 JVM 参数外,还可以通过对代码本身的改进来减轻 GC 的负担。这包括但不限于重用对象而非频繁创建新实例、避免不必要的大数组或者缓存常用的数据结构等方法。 #### 示例:启用 G1 收集器及其参数设定 下面展示了一个简单的命令行选项例子,演示了如何启动一个带有特定初始及最大堆尺寸,并启用了 G1 垃圾收集算法的 Java 应用程序: ```bash java -Xms512m -Xmx4g -XX:+UseG1GC MyApplication ``` 此脚本设置了最小堆空间为 512MB ,最大可达 4GB 同时指定了采用 G1 进行垃圾管理的方式。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值