Java memory anaylsis

Machine addresses

Many data structures involve representation of machine addresses.

The typical x64 architectures we widely used, need 8 bytes to represent address.

Many older machines use a 32-bit architecture that
would involve just 4 bytes per machine address.

Primary type

type bytes
byte 1
boolean 1
char 2
int 4
float 4
double 8
long 8

Object

Object

The overhead includes:

  • a reference to the object’s class
  • garbage collection information
  • synchronization information

List

LinkedList:

Object

Instance :

  • 16 bytes of object overhead
  • 8 bytes for first node reference
  • 4 bytes counter
  • 4 bytes padding

Node :

  • 16 bytes of object overhead
  • 8 bytes for the references to the Item
  • 8 bytes for the next to the Item
  • 8 bytes for the extra overhead

total :
32 + (40 + size(Ojbect)) * N

Array

Object

type bytes
int[N] 24 + 4N
Integer[N] 24 + 32N
int[M][N] 24 + 8 * (24 + 4N)
Integer[M][N] 24 + 8 * (24 + 32N)

String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 16 bytes
public class String {
// reference 8 bytes
// 24 + 2N bytes
private char[] value;
// 4 bytes
private int offset;
// 4 bytes
private int count;
// 4 bytes
private int hash;
...
// padding 4 bytes
}

Total : 64 + 2N


HashMap

Object
array length : M
average node size : N
Total[structure] : 64M + 48N


Tree

Object
Total[structure] : 56N