简体   繁体   中英

Java class vs array memory size?

I have to store millions of X/Y double pairs for reference in my Java program. I'd like to keep memory consumption as low as possible as well as the number of object references. So after some thinking I decided holding the two points in a tiny double array might be a good idea, it's setup looks like so:

double[] node = new double[2];
node[0] = x;
node[1] = y;

I figured using the array would prevent the link between the class and my X and Y variables used in a class, as follows:

class Node {
     public double x, y;
}

However after reading into the way public fields in classes are stored, it dawned on me that fields may not actually be structured as pointer like structures, perhaps the JVM is simply storing these values in contiguous memory and knows how to find them without an address thus making the class representation of my point smaller than the array.

So the question is, which has a smaller memory footprint? And why?

I'm particularly interested in whether or not class fields use a pointer, and thus have a 32-bit overhead, or not.

The latter has the smaller footprint.

Primitive types are stored inline in the containing class. So your Node requires one object header and two 64-bit slots. The array you specify uses one array header (>= an object header) plust two 64-bit slots.

If you're going to allocate 100 variables this way, then it doesn't matter so much, as it is just the header sizes which are different.

Caveat: all of this is somewhat speculative as you did not specify the JVM - some of these details may vary by JVM.

I don't think your biggest problem is going to be storing the data, I think it's going to be retrieving, indexing, and manipulating it.

However, an array, fundamentally, is the way to go. If you want to save on pointers, use a one dimensional array. (Someone has already said that).

First, it must be stated that the actual space usage depends on the JVM you are using. It is strictly implementation specific. The following is for a typical mainstream JVM.

So the question is, which has a smaller memory footprint? And why?

The 2nd version is smaller. An array has the overhead of the 32 bit field in the object header that holds the array's length. In the case of a non-array object, the size is implicit in the class and does not need to be represented separately.

But note that this is a fixed over head per array object . The larger the array is, the less important the overhead is in practical terms. And the flipside of using a class rather than array is that indexing won't work and your code may be more complicated (and slower) as a result.

A Java 2D array is actually and array of 1D arrays (etcetera), so you can apply the same analysis to arrays with higher dimensionality. The larger the size an array has in any dimension, the less impact the overhead has. The overhead in a 2x10 array will be less than in a 10x2 array. (Think it through ... 1 array of length 2 + 2 of length 10 versus 1 array of length 10 + 10 of length 2. The overhead is proportional to the number of arrays.)

I'm particularly interested in whether or not class fields use a pointer, and thus have a 32-bit overhead, or not.

(You are actually talking about instance fields, not class fields. These fields are not static ...)

Fields whose type is a primitive type are stored directly in the heap node of the object without any references. There is no pointer overhead in this case.

However, if the field types were wrapper types (eg Double rather than double ) then there could be the overhead of a reference AND the overheads of the object header for the Double object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM