访问正在运行的 JVM 原始内存
如果这是一个无知的问题,请原谅我,我不完全是一个专家程序员,只是好奇。是否可以从正在运行的程序内部读取工作内存(例如类的实例)?
我知道您可以执行类似 println(theInstance.getClass());
的操作,它会给您实例的内存地址(我假设这就是它)。我想知道是否可以执行类似 byte[]memory = theInstance.getClass().getMemory(); 的操作println(toString(memory));
我知道这都是编出来的,只是为了说明。
Forgive me if this is an ignorant question, I'm not exactly an expert programmer just curious. Is it possible to read the working memory, say for an instance of a class, from inside a running program?
I know you can do something like println(theInstance.getClass());
and it will give you the memory address of the instance (I'm assuming thats what it is). I'm wondering if can do something like byte[]memory = theInstance.getClass().getMemory(); println(toString(memory));
I know thats all made up but just to illustrate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不使用标准 Java API。但是,您可以通过sun.misc.Unsafe访问内存(您可能需要使用反射来获取此类的实例)。当然,您也可以通过 JNI 来完成此操作。
但你为什么要这么做呢?
Not using standard Java APIs. However, you can access memory through
sun.misc.Unsafe
(you will probably need to use reflection to get the instance of this class). You can, of course, also do this through JNI.But why would you want to?
不,打印 getClass() 的结果不会给出“内存地址”;它给出了一个类的字符串表示(类似于“class Argyle”)。
从你的问题来看,尚不清楚你期望这样的事情的内容是什么。如果您想了解 Java 如何表示内存中的对象,请阅读 虚拟机规范。再说一遍,Java 虚拟机不强制对象的任何特定内部结构。
No, printing the result of getClass() doesn't give a "memory address"; it gives a string representation of a class (which would be something like "class Argyle").
It's not clear, from your question, what you'd expect the contents of such a thing to be. If you'd like to understand how Java represents objects in memory, then read the VM specification. Then again, the Java virtual machine does not mandate any particular internal structure for objects.
我最近没有看过这个,但如果在 Linux 上你可以寻找/读取
/proc/PID/mem
。I haven't looked at this lately but if on Linux you could seek/read
/proc/PID/mem
.