1 21 22 package org.apache.derby.iapi.services.cache; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import java.lang.Class ; 27 import java.lang.reflect.Field ; 28 import java.lang.Runtime ; 29 import java.lang.InterruptedException ; 30 import java.lang.reflect.Modifier ; 31 32 public class ClassSize 33 { 34 public static final int refSize; 35 private static final int objectOverhead = 2; private static final int booleanSize = 4; 37 private static final int charSize = 4; private static final int shortSize = 4; 39 private static final int intSize = 4; 40 private static final int longSize = 8; 41 private static final int floatSize = 4; 42 private static final int doubleSize = 8; 43 private static final int minObjectSize; 44 45 private static boolean dummyCatalog = false; 47 static boolean noGuess = false; 48 50 static boolean unitTest = false; 51 53 private static final int[] wildGuess = {0,16}; 54 57 58 62 private static java.util.Hashtable catalog; 63 static 64 { 65 try 66 { 67 catalog = (java.util.Hashtable ) 68 Class.forName( "org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance(); 69 } 70 catch( Exception e){}; 71 72 Runtime runtime = Runtime.getRuntime(); 74 long memBase = runtime.totalMemory() - runtime.freeMemory(); 75 Object [] junk = new Object [10000]; 76 long memUsed = runtime.totalMemory() - runtime.freeMemory() - memBase; 77 int sz = (int)((memUsed + junk.length/2)/junk.length); 78 refSize = ( 4 > sz) ? 4 : sz; 79 minObjectSize = 4*refSize; 80 } 81 82 85 public static void setDummyCatalog() 86 { 87 dummyCatalog = true; 88 } 89 94 public static int getRefSize() 95 { 96 return refSize; 97 } 98 99 102 public static int getIntSize() 103 { 104 return intSize; 105 } 106 107 121 public static int[] getSizeCoefficients( Class cl) 122 { 123 int[] coeff = {0, objectOverhead}; 124 125 126 127 for( ; null != cl; cl = cl.getSuperclass()) 128 { 129 Field [] field = cl.getDeclaredFields(); 130 if( null != field) 131 { 132 for( int i = 0; i < field.length; i++) 133 { 134 if( ! Modifier.isStatic( field[i].getModifiers())) 135 { 136 Class fieldClass = field[i].getType(); 137 if( fieldClass.isArray() || ! fieldClass.isPrimitive()) 138 coeff[1]++; 139 else { 141 String name = fieldClass.getName(); 142 143 if( name.equals( "int") || name.equals( "I")) 144 coeff[0] += intSize; 145 else if( name.equals( "long") || name.equals( "J")) 146 coeff[0] += longSize; 147 else if( name.equals( "boolean") || name.equals( "Z")) 148 coeff[0] += booleanSize; 149 else if( name.equals( "short") || name.equals( "S")) 150 coeff[0] += shortSize; 151 else if( name.equals( "byte") || name.equals( "B")) 152 coeff[0] += 1; 153 else if( name.equals( "char") || name.equals( "C")) 154 coeff[0] += charSize; 155 else if( name.equals( "float") || name.equals( "F")) 156 coeff[0] += floatSize; 157 else if( name.equals( "double") || name.equals( "D")) 158 coeff[0] += doubleSize; 159 else coeff[1]++; } 162 } 163 } 164 } 165 } 166 return coeff; 167 } 169 177 public static int estimateBaseFromCoefficients( int[] coeff) 178 { 179 int size = coeff[0] + coeff[1]*refSize; 180 size = (size + 7)/8; 182 size *= 8; 183 return (size < minObjectSize) ? minObjectSize : size; 184 } 186 197 public static int estimateBaseFromCatalog( Class cls) 198 { 199 return estimateBaseFromCatalog( cls, false); 200 } 201 202 private static int estimateBaseFromCatalog( Class cls, boolean addToCatalog) 203 { 204 if( dummyCatalog) 205 return 0; 206 207 if( SanityManager.DEBUG) 208 SanityManager.ASSERT( catalog != null, "The class size catalog could not be initialized."); 209 210 int[] coeff = (int[]) catalog.get( cls.getName()); 211 if( coeff == null) 212 { 213 try 214 { 215 coeff = getSizeCoefficients( cls); 216 } 217 catch( Throwable t) 218 { 219 if( noGuess) 220 return -2; 221 coeff = wildGuess; 222 } 223 if( addToCatalog) 224 catalog.put( cls.getName(), coeff); 225 } 226 return estimateBaseFromCoefficients( coeff); 227 } 229 230 242 public static int estimateAndCatalogBase( Class cls) 243 { 244 return estimateBaseFromCatalog( cls, true); 245 } 247 261 public static int estimateBase( Class cl) 262 { 263 return estimateBaseFromCoefficients( getSizeCoefficients( cl)); 264 } 266 270 public static int estimateArrayOverhead() 271 { 272 return minObjectSize; 273 } 274 275 281 public static int estimateHashEntrySize() 282 { 283 return objectOverhead + 3*refSize; 284 } 285 286 291 public static int estimateMemoryUsage( String str) 292 { 293 if( null == str) 294 return 0; 295 return 2*str.length(); 297 } 298 } 299 | Popular Tags |