1

I have the following piece of code:

public static void main(String[] args) {
    int[] a = new int[Integer.MAX_VALUE / 4];
    while (true);
}

The size of this array is supposed to be 2^29 and each integer occupies 4 bytes. The overall size of this array should be at least approximately 2GB. I run this code on my laptop with 16 Gig RAM.

I then run jstat and see the following output.

S0C    10752.0
S1C    10752.0
S0U    0.0
S1U    5421.8
EC     65536.0
EU     27194.8
OC     175104.0
OU     16.0
MC     14080.0
MU     13695.2
CCSC   1792.0
CCSU   1617.5
YGC    1
YGCT   0.006
FGC    0
FGCT   0.000
GCT    0.006

From the output we can determine that S1 occupies 5421.8KB and eden occupies 27194.8KB. Is there some kind of optimization had been performed so that all entries map to the same value in the memory?

user1745356
  • 4,462
  • 7
  • 42
  • 70
  • You never actually use the array. I'd be suspicious that the entire array got eliminated. – Louis Wasserman May 02 '17 at 20:23
  • 1
    The standard max heap size is 1/4 of physical memory. Did you use the default or did you specifiy a fixed value as command line argument? – k5_ May 02 '17 at 20:27
  • 1
    Try printing `Runtime.getRuntime().maxMemory()` to see what the actual max heap size is, rather than guessing/estimating. – Andreas May 02 '17 at 20:31
  • @Andreas the max size of the heap is indeed 4GB but why S1 and EU are filled up to 5MB and 27MB ? – user1745356 May 02 '17 at 20:37
  • Large arrays are never created in the young generation, so don’t expect them to appear in `S1` nor `EU`. The reason it doesn’t appear in `OU` might be that it has been collected, as @Louis Wasserman suggested, though I never encountered such behavior in a simple `main` method… – Holger May 30 '17 at 13:43

0 Answers0