Java对象和数组的内存位置 [英] Java Object and array memory location

查看:165
本文介绍了Java对象和数组的内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Java中,其中的密钥和值的类型是对象的数组支持的哈希表;没有其他保障。

I'm writing an array-backed hashtable in Java, where the type of key and value are Object; no other guarantee.

对我来说,最简单的方法code-明智的就是创建一个对象来保存它们:

The easiest way for me code-wise is to create an object to hold them:

public class Pair { 
    public Object key;
    public Object value;
}

然后创建一个数组

And then create an array

公众对[] =存储一双新[8];

但如何对待JVM在内存中?这就是说,将数组实际上是:

But how does the jvm treat that in memory? Which is to say, will the array actually:


  • 是一个指针数组对()对象坐在别处,或

  • 包含实际的数据?

修改

由于对象实例化以后作为新的对(),他们随意放置在堆中。在那里,以确保他们在堆顺序什么好办法?我会需要做一些挂羊头卖狗肉与sun.misc.unsafe,使这项工作?

Since the objects are instantiated later as new Pair(), they're randomly placed in the heap. Is there any good way to ensure they're sequential in the heap? Would I need to do some trickery with sun.misc.unsafe to make that work?

解释我的动机,如果我想尝试,并确保连续的项目是在内存中的同一页,有没有办法在Java中做到这一点?

推荐答案

该阵列将在包含指向了对对象也将在堆(但是从阵列本身分开)堆中的对象。

The array will be an object on the heap containing pointers to the Pair objects which will also be on the heap (but separate from the array itself).

这篇关于Java对象和数组的内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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