Memory Optimization Observation

I was looking at reducing the memory consumed by some of the full document fidelty models we have for WSDL and XML Schema. I noticed that one of the representations was storing an unnecessary object reference. I was expecting the object reference multiplied by the large number of objects created to have an impact on the memory size, but removing this reference did not have any impact. The basic code to illustrate this issue is below (I ran this using 1.5.0_06). What I noticed was that removing the member variable i from class T did not have any impact in terms of memory usage. However, uncommenting the j variable from Class T had the expected impact of increasing the memory size. The test code import java.lang.management.ManagementFactory; public class MemoryTest { public static long getCurrentMemoryUsage() { ManagementFactory.getMemoryMXBean().gc(); return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); } static class Super { public Super(String s) { value = s; } public String getValue() { return value; } private String value; } static class T extends Super { public T() { super(T.class.getName()); } private int i; //private int j; } public static void testNormalRef(Object[] list) { for (int i = 0; i < list.length; i++) { list[i] = new T(); } } public static void main(String[] args) { final int numObjects = 100000; Object[] refs = new Object[numObjects]; testNormalRef(refs); long curUsage = getCurrentMemoryUsage(); System.out.printf("MemoryUsed %d\\n", curUsage); } }
Comments:

I think it is just because objects's are allocated as multiple of 8 bytes. java.lang.Object is of size 24 bytes (on my sun jdk 1.5.0_06) - so anything more would be multiple of 8 bytes. Hence every second reference or second int/short/char/etc would be 'free'. Ofcourse , I would expect this to be implementation detail of sun's jre :-)

Posted by Mridul on March 12, 2006 at 07:05 AM PST #

Post a Comment:
Comments are closed for this entry.
About

cwebster

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today