banjara
banjara

Reputation: 3890

How much space does an array occupy?

If I create 10 integers and an integer array of 10, will there be any difference in total space occupied?

I have to create a boolean array of millions of records, so I want to understand how much space will be taken by array itself.

Upvotes: 8

Views: 22089

Answers (9)

Ikabot
Ikabot

Reputation: 13

It needn't reflect poorly on the teacher / interviewer.

How much you care about the size and alignment of variables in memory depends on how performant you need your code to be. It matters a lot if your software processes transactions (EFT / stock market) for example.

The size, alignment, and packing of your variables in memory can influence CPU cache hits/misses, which can influence the performance of your code by up to a factor of 100.

It's not a bad thing to know what's happening at a low level, as long as you use performance boosting tricks responsibly.

For example, I came to this thread because I needed to know the answer to exactly this question, so that I can size my arrays of primitives to fill an integer multiple of CPU cache lines because I need the code that is performing calculations over those arrays of primitives to execute quickly because I have a finite window in which I need my calculations to be ready for the consumer of the result.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718708

An array of integers is represented as block of memory to hold the integers, and an object header. The object header typically takes 3 32bit words for a 32 bit JVM, but this is platform dependent. (The header contains some flag bits, a reference to a class descriptor, space for primitive lock information, and the length of the actual array. Plus padding.)

So an array of 10 ints probably takes in the region of 13 * 4 bytes.

In the case on an Integer[], each Integer object has a 2 word header and a 1 word field containing the actual value. And you also need to add in padding, and 1 word (or 1 to 2 words on a 64-bit JVM) for the reference. That is typically 5 words or 20 bytes per element of the array ... unless some Integer objects appear in multiple places in the array.


Notes:

  1. The number of words actually used for a reference on a 64 bit JVM depends on whether "compressed oops" are used.
  2. On some JVMs, heap nodes are allocated in multiples of 16 bytes ... which inflates space usage (e.g. the padding mentioned above).
  3. If you take the identity hashcode of an object and it survives the next garbage collection, its size gets inflated by at least 4 bytes to cache the hashcode value.
  4. These numbers are all version and vendor specific, in addition to the sources of variability enumerated above.

Upvotes: 6

Thilo
Thilo

Reputation: 262474

Some rough lower bounds calculations:

Each int takes up four bytes. = 40 bytes for ten

An int array takes up four bytes for each component plus four bytes to store the length plus another four bytes to store the reference to it. = 48 bytes (+ maybe some padding to align all objects at 8 byte boundaries)

An Integer takes up at least 8 bytes, plus the another four bytes to store the reference to it. = at least 120 for ten

An Integer array takes up at least the 120 bytes for the ten Integers plus four bytes for the length, and then maybe some padding for alignment. Plus four bytes to store the reference to it. (@Marko reports that he even measured about 28 bytes per slot, so that would be 280 bytes for an array of ten).

Upvotes: 4

ramazan polat
ramazan polat

Reputation: 7880

I mean if i create 10 integers and integer array of 10, will there be any difference in total space occupied.

(integer array of 10) = (10 integers) + 1 integer

The last "+1 integer" is for index of array ( arrays can hold 2,147,483,647 amount of data, which is an integer). That means when you declare an array, say:

int[] nums = new int[10];

you actually reserve 11 int space from memory. 10 for array elements and +1 for array itself.

Upvotes: 0

Thihara
Thihara

Reputation: 6969

In light of your comment it will not make much difference if you used an array. Array will use a negligible amount of memory for its functionality itself. All other memory will be used by the stored objects.

EDIT: What you need to understand is that the difference between Boolean wrapper and boolean primitive type. Wrapper types will usually take up more space than the primitives. So for missions of records try to go with the primitives.

Another thing to keep in mind when dealing of missions of record as you said is Java Autoboxing. The performance hit can be significant if you unintentionally use this in a function that traverses the whole array.

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200138

What you can do is measure:

public static void main(String[] args) {
  final long startMem = measure();
  final boolean[] bs = new boolean[1000000];
  System.out.println(measure() - startMem);
  bs.hashCode();
}
private static long measure() {
  final Runtime rt = Runtime.getRuntime();
  rt.gc();
  try { Thread.sleep(20); } catch (InterruptedException e) {}
  rt.gc();
  return rt.totalMemory() - rt.freeMemory();
}

Of course, this goes with the standard disclaimer: gc() has no particular guarantees, so repeat several times to see if you are getting consistent results. On my machine the answer is one byte per boolean.

Upvotes: 2

Bigger
Bigger

Reputation: 1960

If you use an array you have 11 Objects, 10 integers and the array, plus Arrays have other metadata inside. So using an array will take more memory space.

Now for real. This kind of question actually comes up in job interviews and exams, and that shows you what kind of interviewer or teacher you have... with so many layers of abstraction working down there in the VM and in the OS itself, what is the point on thinking on this stuff? Micro-optimizing memory...!

Upvotes: 0

Razvan
Razvan

Reputation: 10093

In java you have both Integer and int. Supposing you are referring to int , an array of ints is considered an object and objects have metadata so an array of 10 ints will occupy more than 10 int variables

Upvotes: 2

Stefano
Stefano

Reputation: 263

In terms of RAM space, there is no real difference

Upvotes: 0

Related Questions