如何找出确切的年轻/老年人在记忆中的位置? [英] How to find out where exact young/old gen is located in memory?

查看:142
本文介绍了如何找出确切的年轻/老年人在记忆中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我能够使用sun.misc.Unsafe类获取对象的地址。

Recently I was able to get an object's address using sun.misc.Unsafe class.

现在我试图以编程方式找到我的对象所在的实际生成。为此,我想知道每一代的起点和终点。
如果Java(Oracle JVM)提供了解决此问题的任何工具?我不相信,因为即使不同的GC也需要不同的内存结构(例如G1),这使得任务更加有趣:)

And now I am trying to find programmatically an actual generation where my object is located. For this I want to know the starting and ending points of each generation. If Java (Oracle JVM) provides any tools to resolve this issue? I believe not, since even different GCs require different memory structures (e.g. G1), and this makes the task even more interesting :)

我想知道的只是代表内存中几代边界的几个数字,如下所示:

What I want to know here is just a couple of numbers representing the borders of generations in memory, like this:

   young gen: start point - 8501702198   
              end point   - 9601256348

愿意听到关于黑魔法的最疯狂的想法,可以确定哪里不同的生成区域被放置在内存中。

Would love to hear even your craziest ideas about a black magic that allows to determine where different generation areas are placed in memory.

推荐答案

这可以通过HotSpot JVM实现,但有些复杂。

This is possible with HotSpot JVM though somehow complicated.

关键的想法是使用 VMStructs - 有关HotSpot内部的信息常量类型 emb编辑到JVM共享库。

The key idea is to use VMStructs - the information about HotSpot internal constants and types embedded right into JVM shared library.

例如, ParallelScavengeHeap :: _ young_gen VM全局变量包含指向<的指针code> PSYoungGen 具有 _virtual_space 成员的结构,其中包含Parallel collector年轻代的边界。类似地, GenCollectedHeap :: _ gch 全局指向描述CMS收集器代的结构。

For example, ParallelScavengeHeap::_young_gen VM global variable contains the pointer to PSYoungGen structure that has _virtual_space member with the boundaries of Parallel collector's young generation. Similarly, GenCollectedHeap::_gch global points to the structure describing CMS collector generations.

我做了一个< a href =https://github.com/apangin/helfy =nofollow>概念验证项目,以演示VMStructs的用法。它是纯Java,不需要额外的库,但它非常依赖于未记录的JDK内部,并且可能不适用于所有Java版本。我在Windows和Linux上对JDK 8u40和JDK 7u80进行了测试。

I've made a proof-of-concept project to demonstrate the usage of VMStructs. It is pure Java, no extra libraries required, but it deeply relies on the undocumented JDK internals and may not work on all Java versions. I've tested this on JDK 8u40 and JDK 7u80 on Windows and Linux.

  • JVM.java - the code for reading VMStructs;
  • HeapInfo.java - the sample program to get addresses of Heap generations.

这篇关于如何找出确切的年轻/老年人在记忆中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆