Friday, May 2, 2014

Memory requirement of a program in java


Consider the class below, and notice the comment telling how much bytes they use.
There is a overhead of 16bytes for maintaining class, 1 Byte for byte and boolean, 2Byte for char and short, 4 bytes for integer, 8 bytes for long and double, and some overhead for padding, to make overall memory even, to sum up:
Java typeBytes required
boolean1
byte
char2
short
int4
float
long8

public class MysteryBox {                     //   16 (object overhead)
 private int x0;                           //    4 (1 int)
 private long y0, y1, y2;                  //   24 (3 long)
 private double z0, z1, z2;                //   24 (3 double)
 private boolean[] a = new boolean[80];    //    8 (reference to array)
     //  104 (boolean array of size 80)
 ...                                             4 (padding)
}                                                ----
      //total size - 184


Here are some good links on java memory objects:

0 comments:

Post a Comment