问题描述
我想获得对Java堆中所有对象的引用,即使我没有立即在活动线程中引用这些对象。我不需要非引用的对象(那些排队用于垃圾收集),但是想要得到任何仍在使用的对象。
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. 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?
推荐答案
我会查看包。检测您感兴趣的类,以便ctor注册创建的实例。如果您不想使用java.lang.instrument,或者如果通过您可以控制的东西(IoC容器或工厂)创建对象,那么您可以通过AspectJ执行此操作,那么您可以做一些不太神奇的好块。
I'd look into the the instrument package. Instrument the classes you are interested in so the ctor registers the created instance. 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堆对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!