引用存储,或生对象 - 在不同的语言数组? [英] Arrays in different languages - store references, or raw objects?

查看:110
本文介绍了引用存储,或生对象 - 在不同的语言数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图环绕使用数组时,什么原始内存看起来就像在不同的语言我的头。

考虑下面的Java code:

 字符串A =HI;
字符串B =那里;
字符串C =每个人;
String []数组= {A,B,C};

显然阵列持有的引用的,而不是物体;也就是说,有在每个点到其他位置在内存中的对象坐在那里三个引用内存的连续阵列。所以对象本身不一定坐在三个连续水桶;而该引用。

现在考虑这个问题:

  String []数组= {喜,有,人人}

我在这种情况下,可以想象字符串与在存储器中的所有其他常量某处存在,然后在阵列保存在内存中的那些常数引用?所以,再次在原始内存阵列并不像 ['H','我','\\ 0','T','H','E','R', 'E'...(等)] 。 (使用C风格的终端只是为了方便)。相反,它更像 ['a83a3edf','a38decd'...(等)] 其中,每个元素都是一个存储位置(参考)。

从这个思维过程我的结论是,在Java中,你永远无法想象数组在内存中连续对象的水桶,而是连续的引用。我想不出什么办法来保证物体将始终连续存储在Java中。

现在考虑C:

 的char * A =HI;
字符* B =没有;
字符* C =每个人;
的char *数组[] = {A,B,C};

上面的code是功能上等同于上述的Java的 - 即,该阵列拥有引用(指针)的一些其它存储器位置。和Java一样,被指向的对象不一定是连续的。

但是,下面的C code:

 结构my_struct阵列[5]; //在内存中分配5 *尺寸(my_struct)!没有空间5
                            //引用/指针,但是房间5 my_structs。

阵列结构被连续位于原始内存。

现在我的具体问题:


  1. 当时我纠正我的假设,即在Java中,数组必须始终坚持的引用,作为程序员永远只能访问在Java中引用?什么原始数据类型?它会工作方式有所不同呢?请问数组 INT S IN的Java活脱脱一个是C的原始内存(除了对象类克鲁夫特的Java将新增)?


  2. 在Java中,有没有办法让程序员以保证物体的连续内存分配?它可能会发生一次偶然的机会,或以很高的概率,但程序员不能保证它会是这样?


  3. 在C,程序员可以创建对象(结构)原阵列连续的记忆,我已经如上图所示,是否正确?


  4. 如何其他语言的处理?我猜测的Python的作品像Java?


有关这个问题的动机是,我想要的是在他原始内存水平这些语言阵列发生有深刻的理解。多为程序员面试问题。我在previous采访时表示,阵列(未在任何语言,只是一般)连续举行像水桶内存中的对象说。我说这个,我才意识到这不是它相当如何在像Java语言之后,这是唯一。所以我想就可以了100%的清楚。

感谢。让我知道,如果有什么需要澄清。


解决方案

  

你永远无法想象数组在内存中连续对象的水桶,而是连续的引用。


在理论上你是正确的,在实践中,JVM不随机化存储器存取。它的内存分配顺序,并拷贝发现顺序GC中的对象(或反向顺序)


  

当时我在假设在Java中,数组必须始终坚持的引用,作为程序员永远只能访问在Java中的引用是否正确?


是的,除非你有当然的原语组成的数组。


  

有关原始数据类型是什么?它会工作方式有所不同呢?


原语和引用在内存中连续的。他们基本上是相同的。


  

将在Java的int数组活脱脱一个是C的原始内存(除了Object类的Java克鲁夫特将增加)?



  

在Java中,有没有办法让程序员以保证物体的连续内存分配?


除非你使用了堆内存。虽然通常这并不像你想象的那么一个问题的的大部分时间的,对象将在内存中连续的。


  

它可能会发生一次偶然的机会,或以很高的概率,但程序员不能保证它会是这样?


正确的。通常当你看到最糟糕的0.1%延迟或以上,你有更大的问题。


  

在C,程序员可以创建对象(结构)原阵列连续的记忆,我已经如上图所示,是否正确?


是的。你可以做到这一点在Java中一样好,但你必须使用关堆内存。有一些支持这个功能,比如Javolution,纪事,SBE库。

I am trying to wrap my head around what the raw memory looks like in different languages when using an array.

Consider the following Java code:

String a = "hi";
String b = "there";
String c = "everyone";
String[] array = {a, b, c};

Obviously the array is holding references, and not objects; that is, there is a contiguous array in memory of three references which each points to some other location in memory where the object sits. So the objects themselves aren't necessarily sitting in three contiguous buckets; rather the references are.

Now consider this:

String[] array = {"hi", "there", "everyone"}

I'd imagine in this situation the Strings exist somewhere with all the other constants in memory, and then the array holds references to those constants in memory? So, again, in raw memory the array doesn't look like ['h', 'i', '\0', 't', 'h', 'e', 'r', 'e'... (etc)]. (using c-style termination just for convenience). Rather, it's more like ['a83a3edf' ,'a38decd' ... (etc)] where each element is a memory location (reference).

My conclusion from this thought process is that in Java, you can never ever imagine arrays as buckets of contiguous objects in memory, but rather as contiguous references. I can't think of any way to guarantee objects will always be stored contiguously in Java.

Now consider C:

char *a = "hi";
char *b = "there";
char *c = "everyone";
char *array[] = {a, b, c};

The code above is functionally equivalent to the Java above -- that is, the array holds references (pointers) to some other memory location. Like Java, the objects being pointed to aren't necessarily contiguous.

HOWEVER, in the following C code:

struct my_struct array[5];  // allocates 5 * size(my_struct) in memory! NOT room for 5
                            // references/pointers, but room for 5 my_structs.

The structs in array ARE contiguously located in raw memory.

Now for my concrete questions:

  1. Was I correct in my assumption that in Java, arrays must ALWAYS hold references, as the programmer only ever has access to references in Java? What about for raw data types? Will it work differently then? Will an array of ints in Java look just like one in C in raw memory (besides the Object class cruft Java will add)?

  2. In Java, is there no way for the programmer to guarantee contiguous memory allocation of objects? It might happen by chance, or with high probability, but the programmer can not GUARANTEE it will be so?

  3. In C, programmers CAN create raw arrays of objects (structs) contiguously in memory, as I have shown above, correct?

  4. How do other languages deal with this? I'm guessing Python works like Java?

The motivation for this question is that I want a solid understanding of what is happening at he raw memory level with arrays in these languages. Mostly for programmer-interview questions. I said in a previous interview that an array (not in any language, just in general) holds objects contiguously in memory like buckets. It was only after I said this that I realized that's not quite how it works in a language like Java. So I want to be 100% clear on it.

Thanks. Let me know if anything needs clarification.

解决方案

you can never ever imagine arrays as buckets of contiguous objects in memory, but rather as contiguous references.

In theory you are right, in practice, the JVM doesn't randomise memory access. It allocates memory sequentially and it copies objects during a GC in order of discovery (or reverse order)

Was I correct in my assumption that in Java, arrays must ALWAYS hold references, as the programmer only ever has access to references in Java?

Yes, Unless you have an array of primitives of course.

What about for raw data types? Will it work differently then?

Primitives and References are continuous in memory. They are basically the same.

Will an array of ints in Java look just like one in C in raw memory (besides the Object class cruft Java will add)?

yes.

In Java, is there no way for the programmer to guarantee contiguous memory allocation of objects?

Not unless you use off heap memory. Though generally this isn't as much of a problem as you might think as most of the time, the objects will be continuous in memory.

It might happen by chance, or with high probability, but the programmer can not GUARANTEE it will be so?

correct. Usually you have bigger problems when you look at the worst 0.1% latencies or above.

In C, programmers CAN create raw arrays of objects (structs) contiguously in memory, as I have shown above, correct?

yes. You can do it in Java as well, but you have to use off heap memory. There is a number of libraries which support this such as Javolution, Chronicle, SBE.

这篇关于引用存储,或生对象 - 在不同的语言数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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