使用sun.misc.Unsafe获取Java数组项的地址?
我正在努力理解 sun.misc.Unsafe 的文档——我想因为它不适合一般用途,所以没有人真正费心让它可读——但我实际上真的需要一种方法来查找中元素的地址一个数组(以便我可以将指向它的指针传递给本机代码)。有没有人有任何可以执行此操作的工作代码?可靠吗?
I'm struggling to understand the documentation of sun.misc.Unsafe -- I guess as it's not intended for general use, nobody's really bothered with making it readable -- but I actually really need a way to find the address of an element in an array (so that I can pass a pointer to it to native code). Has anyone got any working code that does this? Is it reliable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个工作示例。但请小心,因为不恰当地使用
Unsafe
类可能很容易使 JVM 崩溃。Here is a working sample. Please be careful however as you may easily crash your JVM with unappropriate usage of
Unsafe
class.您可以使用 ByteBuffer.allocateDirect() 直接缓冲区,而不是使用数组。它的地址位于一个字段中,并且该地址在 ByteBuffer 的生命周期内不会改变。直接 ByteBuffer 使用最小的堆空间。您可以使用反射来获取地址。
你可以使用Unsafe来获取地址,问题是GC可以随时移动它。对象并不固定在内存中。
在 JNI 中,您可以使用特殊方法将数据复制到 Java 对象或从 Java 对象复制数据,以避免此问题(以及其他问题)。如果您想使用 C 代码在对象之间交换数据,我建议您使用这些方法。
Instead of using an array you can use a ByteBuffer.allocateDirect() direct buffer. This has the address in a field and this address doesn't change for the life of the ByteBuffer. A direct ByteBuffer uses minimal heap space. You can get the address using reflection.
You can use Unsafe to get an address, the problem is that the GC can move it at any time. Objects are not fixed in memory.
In JNI you can use special methods to copy data to/from Java objects to avoid this issue (and others) I suggest you use these if you want to exchange data between Objects with C code.
为什么? JNI 中有很多工具可用于处理 Java 数组的内容。您不需要使用下周可能不存在的未记录的内部 Sun 类。
Why? There are plenty of facilities in JNI for dealing with the contents of Java arrays. You don't need to use undocumented internal Sun classes that mightn't be there next week.