简体   繁体   English

如何在没有引用的情况下访问Java堆对象?

[英]How can I access Java heap objects without a reference?

I would like to get a reference to all objects in the Java heap, even if I don't immediately have a reference to those objects in my active thread. 我想获得对Java堆中所有对象的引用,即使我没有立即在我的活动线程中引用这些对象。 I don't need non-referenced objects (those "queued" for garbage collection), but would like to get anything that's still in use. 我不需要非引用的对象(那些“排队”用于垃圾收集),但是想要得到任何仍在使用的对象。

The goal is to serialize and store all the objects to implement a poor-man's persistence of execution state. 目标是序列化和存储所有对象,以实现穷人的执行状态持久性。 I realize that the rabbit hole goes deep when it comes to different types of transient state, but simply persisting objects & loaded class definitions would be useful to me. 我意识到兔子洞在涉及不同类型的瞬态时会变深,但简单地持久化对象和加载的类定义对我来说会很有用。

Is there a way to access the heap in order to make this happen? 有没有办法访问堆才能实现这一目标? Am I overlooking a more straightfoward approach? 我是否会忽视更直接的方法?

I'd look into the the instrument package. 我会查看仪器包。 Instrument the classes you are interested in so the ctor registers the created instance. 检测您感兴趣的类,以便ctor注册创建的实例。 You might be able to do this via AspectJ should you not want to use the java.lang.instrument or if the objects are created via something you can control (an IoC container or factories) then you can do something a good chunk less magical. 如果您不想使用java.lang.instrument,或者如果对象是通过您可以控制的东西(一个IoC容器或工厂)创建的,那么您可以通过AspectJ执行此操作,那么您可以做一些不太神奇的好块。

If you want to take a heap dump programmatically, you'll not find suitable APIs in the java.* or javax.* namespace. 如果要以编程方式进行堆转储,则无法在java。*或javax。*命名空间中找到合适的API。 However, the Sun runtime comes with the HotSpotDiagnosticMXBean which will enable you to take a heap dump by writing the contents of the heap on to a specified file in disk. 但是,Sun运行时附带了HotSpotDiagnosticMXBean ,它允许您通过将堆的内容写入磁盘中的指定文件来进行堆转储。

I suggest you take a heap dump and then inspect it using the Eclipse Memory Analyser . 我建议您进行堆转储 ,然后使用Eclipse Memory Analyzer进行检查。

The views available allow you to drill down to instance level, view object properties. 可用视图允许您深入查看实例级别,查看对象属性。 You can even query objects using OQL - and SQL-like query language for objects. 您甚至可以使用OQL查询对象 - 对象也可以使用类似SQL的查询语言。

The left panel in the below screenshot demonstrates inspecting field values. 下面屏幕截图中的左侧面板演示了检查字段值。

screenshot http://img181.imageshack.us/img181/4013/dominatortreegrouped.png 截图http://img181.imageshack.us/img181/4013/dominatortreegrouped.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何引用或访问Java Swing中用其他方法创建的对象(例如按钮)? - How can I reference or access objects such as buttons made in another method in Java Swing? 当我在jvisualvm中转储堆时,如何删除对Runnable的“Java Frame”GC根引用? - How can I remove a “Java Frame” GC Root reference to a Runnable when I dump a heap in jvisualvm? 在Java的堆空间中创建的对象如何具有全局访问权限? - How objects created in java's heap space have global access? Java如何直接在最小堆中访问对象 - How to access objects in a min-heap directly, java Java对象,引用变量和垃圾收集堆 - Java objects, reference variables and the garbage collection heap 如何在没有堆错误的情况下将大型Java对象编组为xml? - How to marshall large java objects to xml without heap error? 如何从Java引用伴随对象? - How can I make reference to companion objects from Java? 没有包时如何引用Java文件? - How can I reference Java files without packages? 如何在没有垃圾收集的情况下在Java 5上进行堆转储? - How can I take a heap dump on Java 5 without garbage collecting first? 多个对象如何在没有实例引用的情况下向对象发送通知(在Java中)? - How multiple objects can send a notification to an object without having its instance reference (in java)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM