public void initialize(long maxSharedCacheSizeInBytes) { maxCacheSizeInBytes = maxSharedCacheSizeInBytes; // Create estimators if ((maxCacheSizeInBytes > 0) && (sizeEstimators == null)) { sizeEstimators = IncrementalObjectSizeEstimator.createEstimators(SharedCache.class); } }
private static ObjectEstimator getMemorySizeEstimator(Class<?> clazz) { ObjectEstimator estimator = sizeEstimators.get(clazz); if (estimator == null) { IncrementalObjectSizeEstimator.createEstimators(clazz, sizeEstimators); estimator = sizeEstimators.get(clazz); } return estimator; }
public static void addEstimator(String className, HashMap<Class<?>, ObjectEstimator> sizeEstimators, Class<?> topClass) { Class<?> clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { // Ignore and hope for the best. LOG.warn("Cannot find " + className); return; } IncrementalObjectSizeEstimator.createEstimators(clazz, sizeEstimators); } }
public static HashMap<Class<?>, ObjectEstimator> createEstimators(Object rootObj) { HashMap<Class<?>, ObjectEstimator> byType = new HashMap<>(); addHardcodedEstimators(byType); createEstimators(rootObj, byType); return byType; }
private int estimateArrayElements(HashMap<Class<?>, ObjectEstimator> parent, FieldAndType e, Object fieldObj, int len, IdentityHashMap<Object, Boolean> uniqueObjects) { int result = 0; Class<?> lastClass = e.field.getType().getComponentType(); ObjectEstimator lastEstimator = parent.get(lastClass); for (int i = 0; i < len; ++i) { Object element = Array.get(fieldObj, i); if (element == null) continue; if (null != uniqueObjects.put(element, Boolean.TRUE)) continue; Class<?> elementClass = element.getClass(); if (lastClass != elementClass) { lastClass = elementClass; lastEstimator = parent.get(lastClass); if (lastEstimator == null) { createEstimators(lastClass, parent); } lastEstimator = parent.get(lastClass); if (lastEstimator == null) { throw new AssertionError( "Don't know how to measure element " + lastClass.getName() + " from " + e.field); } } result += lastEstimator.estimate(element, parent, uniqueObjects); } return result; }
protected int estimateCollectionElements(HashMap<Class<?>, ObjectEstimator> parent, Collection<?> c, Field field, IdentityHashMap<Object, Boolean> uniqueObjects) { ObjectEstimator lastEstimator = null; Class<?> lastClass = null; int result = 0; for (Object element : c) { if (element == null) continue; if (null != uniqueObjects.put(element, Boolean.TRUE)) continue; Class<?> elementClass = element.getClass(); if (lastClass != elementClass) { lastClass = elementClass; lastEstimator = parent.get(lastClass); if (lastEstimator == null) { createEstimators(lastClass, parent); } lastEstimator = parent.get(lastClass); if (lastEstimator == null) { throw new AssertionError( "Don't know how to measure element " + lastClass.getName() + " from " + field); } } result += lastEstimator.estimate(element, parent, uniqueObjects); } return result; }
keyEstimator = parent.get(lastKeyClass); if (keyEstimator == null) { createEstimators(lastKeyClass, parent); valueEstimator = parent.get(lastValueClass); if (valueEstimator == null) { createEstimators(lastValueClass, parent);
private int runEstimate(Object obj, JavaDataModel memModel, String desc) { HashMap<Class<?>, ObjectEstimator> map = IncrementalObjectSizeEstimator.createEstimators(obj); ObjectEstimator root = map.get(obj.getClass()); int estimate = root.estimate(obj, map); LOG.info("Estimated " + estimate + " for " + (desc == null ? obj.getClass().getName() : desc)); return estimate; }
ObjectEstimator fieldEstimator = parent.get(fieldObj.getClass()); if (fieldEstimator == null) { createEstimators(fieldObj.getClass(), parent);
public static void addEstimator(String className, HashMap<Class<?>, ObjectEstimator> sizeEstimators, Class<?> topClass) { Class<?> clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { // Ignore and hope for the best. LOG.warn("Cannot find " + className); return; } IncrementalObjectSizeEstimator.createEstimators(clazz, sizeEstimators); } }
public static HashMap<Class<?>, ObjectEstimator> createEstimators(Object rootObj) { HashMap<Class<?>, ObjectEstimator> byType = new HashMap<>(); addHardcodedEstimators(byType); createEstimators(rootObj, byType); return byType; }
protected int estimateCollectionElements(HashMap<Class<?>, ObjectEstimator> parent, Collection<?> c, Field field, IdentityHashMap<Object, Boolean> uniqueObjects) { ObjectEstimator lastEstimator = null; Class<?> lastClass = null; int result = 0; for (Object element : c) { if (element == null) continue; if (null != uniqueObjects.put(element, Boolean.TRUE)) continue; Class<?> elementClass = element.getClass(); if (lastClass != elementClass) { lastClass = elementClass; lastEstimator = parent.get(lastClass); if (lastEstimator == null) { createEstimators(lastClass, parent); } lastEstimator = parent.get(lastClass); if (lastEstimator == null) { throw new AssertionError( "Don't know how to measure element " + lastClass.getName() + " from " + field); } } result += lastEstimator.estimate(element, parent, uniqueObjects); } return result; }
private int estimateArrayElements(HashMap<Class<?>, ObjectEstimator> parent, FieldAndType e, Object fieldObj, int len, IdentityHashMap<Object, Boolean> uniqueObjects) { int result = 0; Class<?> lastClass = e.field.getType().getComponentType(); ObjectEstimator lastEstimator = parent.get(lastClass); for (int i = 0; i < len; ++i) { Object element = Array.get(fieldObj, i); if (element == null) continue; if (null != uniqueObjects.put(element, Boolean.TRUE)) continue; Class<?> elementClass = element.getClass(); if (lastClass != elementClass) { lastClass = elementClass; lastEstimator = parent.get(lastClass); if (lastEstimator == null) { createEstimators(lastClass, parent); } lastEstimator = parent.get(lastClass); if (lastEstimator == null) { throw new AssertionError( "Don't know how to measure element " + lastClass.getName() + " from " + e.field); } } result += lastEstimator.estimate(element, parent, uniqueObjects); } return result; }
keyEstimator = parent.get(lastKeyClass); if (keyEstimator == null) { createEstimators(lastKeyClass, parent); valueEstimator = parent.get(lastValueClass); if (valueEstimator == null) { createEstimators(lastValueClass, parent);
ObjectEstimator fieldEstimator = parent.get(fieldObj.getClass()); if (fieldEstimator == null) { createEstimators(fieldObj.getClass(), parent);