You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

垃圾回收中对象引用详情获取方法咨询

Can I Get Object Reference Information from gc.log?

Great question! Let's break this down clearly for you:

Default gc.log Doesn't Include Object Reference Details

Out of the box, the standard gc.log only captures high-level garbage collection statistics—things like:

  • Timestamps of GC events
  • Amount of memory reclaimed in each GC phase
  • Pause durations
  • Heap size before/after GC
  • Generational memory usage (Young/Old Gen breakdown)

It won't show you specific relationships between objects (e.g., "Object X is referenced by Object Y") or detailed reference chains that prevent objects from being collected.

How to Get Object Reference Information

To track object references, you'll need to combine additional JVM configurations with specialized heap analysis tools:

1. Generate Heap Dump Files

Configure your JVM to create heap dumps (.hprof format) at key GC points using these parameters:

  • -XX:+HeapDumpBeforeFullGC: Creates a heap dump right before a Full GC runs
  • -XX:+HeapDumpAfterFullGC: Creates a heap dump immediately after a Full GC completes
  • -XX:+HeapDumpOnOutOfMemoryError: Generates a heap dump when an OutOfMemoryError occurs (incredibly useful for debugging memory leaks)

These dumps are full snapshots of all objects in the heap, including their reference relationships.

2. Analyze Heap Dumps with Tools

Once you have a .hprof file, use these tools to inspect object references:

  • VisualVM: A free JDK-included tool. Load your heap dump, then use features like the "Dominator Tree" to see which objects are holding the most memory, or "Find References" to trace exactly which objects are keeping a target object alive.
  • Eclipse Memory Analyzer (MAT): A powerful, dedicated heap analysis tool. It generates leak suspect reports, shows full reference chains for any object, and helps you pinpoint why objects aren't being garbage collected.
  • jhat: A command-line tool (deprecated in newer JDKs, but still works for older versions). Run jhat your-heap-dump.hprof and access the web interface to browse object hierarchies and references.

3. Optional: Track Reference Type Statistics in gc.log

If you only need high-level stats about reference types (e.g., how many soft/weak references were collected), add the -XX:+PrintReferenceGC flag to your JVM arguments. This will append lines like this to your gc.log:

[SoftReference, 0 refs, 0 bytes]
[WeakReference, 0 refs, 0 bytes]
[FinalReference, 0 refs, 0 bytes]
[PhantomReference, 0 refs, 0 bytes]
[JNI Weak Reference, 0 refs, 0 bytes]

Note: This still doesn't give you per-object reference details—just aggregate counts.

Summary

You can't get detailed object reference information directly from a standard gc.log, but by enabling heap dumps and using analysis tools, you can fully inspect object relationships and understand why certain objects are being retained or collected.

内容的提问来源于stack exchange,提问作者Bhranee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:58:43