KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Sizeof


1 // From http://www.javaworld.com/javatips/jw-javatip130_p.html
2

3 /**
4  * A simple class to experiment with your JVM's garbage collector
5  * and memory sizes for various data types.
6  *
7  * @author <a HREF="mailto:vlad@trilogy.com">Vladimir Roubtsov</a>
8  */

9 public class Sizeof {
10   public static void main(String JavaDoc [] args) throws Exception JavaDoc {
11     // "warm up" all classes/methods that we are going to use:
12
runGC();
13     usedMemory();
14         
15     // array to keep strong references to allocated objects:
16
final int count = 10000; // 10000 or so is enough for small ojects
17
Object JavaDoc [] objects = new Object JavaDoc [count];
18         
19     long heap1 = 0;
20
21     // allocate count+1 objects, discard the first one:
22
for (int i = -1; i < count; ++ i) {
23       Object JavaDoc object;
24             
25       // INSTANTIATE YOUR DATA HERE AND ASSIGN IT TO 'object':
26

27       object = new Object JavaDoc(); // 8 bytes
28
//object = new Integer(i); // 16 bytes
29
//object = new Long(i); // same size as Integer?
30
//object = createString(10); // 56 bytes? fine...
31
//object = createString(9)+' '; // 72 bytes? the article explains why
32
//object = new char [10]; // 32 bytes
33
//object = new byte [32][1]; // 656 bytes?!
34

35       if (i >= 0)
36         objects [i] = object;
37       else {
38         object = null; // discard the "warmup" object
39
runGC();
40         heap1 = usedMemory(); // take a "before" heap snapshot
41
}
42     }
43
44     runGC();
45     long heap2 = usedMemory(); // take an "after" heap snapshot:
46

47     final int size = Math.round(((float)(heap2 - heap1))/count);
48     System.out.println("'before' heap: " + heap1 +
49                         ", 'after' heap: " + heap2);
50     System.out.println("heap delta: " + (heap2 - heap1) +
51         ", {" + objects [0].getClass() + "} size = " + size + " bytes");
52   }
53     
54   // a helper method for creating Strings of desired length
55
// and avoiding getting tricked by String interning:
56
public static String JavaDoc createString(final int length) {
57     final char [] result = new char [length];
58     for (int i = 0; i < length; ++ i)
59       result [i] = (char) i;
60         
61     return new String JavaDoc(result);
62   }
63
64   // this is our way of requesting garbage collection to be run:
65
// [how aggressive it is depends on the JVM to a large degree, but
66
// it is almost always better than a single Runtime.gc() call]
67
private static void runGC() throws Exception JavaDoc {
68     // for whatever reason it helps to call Runtime.gc()
69
// using several method calls:
70
for (int r = 0; r < 4; ++ r)
71       _runGC();
72   }
73
74   private static void _runGC() throws Exception JavaDoc {
75     long usedMem1 = usedMemory(), usedMem2 = Long.MAX_VALUE;
76
77     for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++ i) {
78       s_runtime.runFinalization();
79       s_runtime.gc();
80       Thread.currentThread().yield();
81             
82       usedMem2 = usedMem1;
83       usedMem1 = usedMemory();
84     }
85   }
86
87   private static long usedMemory() {
88     return s_runtime.totalMemory() - s_runtime.freeMemory();
89   }
90     
91   private static final Runtime JavaDoc s_runtime = Runtime.getRuntime();
92 }
93
Popular Tags