How much is the space array

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

I asked this question: I need to create a Boolean array of millions of records, so I want to understand how much space the array will occupy.
+5
source share
9 answers

An array of integers is represented as a memory block for storing integers and the title of an object. An object header usually takes 3 32-bit words for a 32-bit JVM, but this is platform dependent. (The header contains some flag bits, a reference to the class descriptor, space for information about elementary locking, and the length of the actual array. Plus an addition.)

, 10 int, , 13 * 4 .

Integer[] Integer 2 1 , . 1 ( 1 2 64- JVM) . 5 20 ... Integer .


:

  • , 64- JVM, , " oops".
  • JVM 16 ... (, ).
  • - , 4 -.
  • , .
+5

java Integer, int. , int, ints , , 10 ints 10 int

+2

:

int . = 40

int . = 48 (, 8 )

8 , . = 120

Integer 120 , , , . , . (@Marko , 28 , 280 ).

+2

, . . .

EDIT: , Boolean wrapper boolean primitive type. , . .

, , , - Java Autoboxing. , , .

+2

, 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();
}

, : gc() , , , . boolean.

+1

/.

, , . , (, EFT/ ).

, / CPU, 100.

, , , , , .

, , , , , CPU, , , , , , .

+1

0

, 11 , 10 , , . , .

. , , ... , , ? - ...!

0

, 10 10, .

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

"+1 " ( 2,147,483,647 , ). , , :

int[] nums = new int[10];

11 int space . 10 +1 .

0

All Articles