public DefaultSizeOfEngine(long maxObjectGraphSize, long maxObjectSize) { this.maxObjectGraphSize = maxObjectGraphSize; this.maxObjectSize = maxObjectSize; this.sizeOf = SizeOf.newInstance(new SizeOfFilterSource(true).getFilters()); this.onHeapKeyOffset = sizeOf.deepSizeOf(new CopiedOnHeapKey<>(new Object(), new IdentityCopier<>())); this.chmTreeBinOffset = sizeOf.deepSizeOf(ConcurrentHashMap.FAKE_TREE_BIN); }
@Override public <K, V> long sizeof(K key, Store.ValueHolder<V> holder) throws LimitExceededException { try { return sizeOf.deepSizeOf(new EhcacheVisitorListener(maxObjectGraphSize, maxObjectSize), key, holder) + this.chmTreeBinOffset + this.onHeapKeyOffset; } catch (VisitorListenerException e) { throw new LimitExceededException(e.getMessage()); } }
/** * {@inheritDoc} */ public long visit(Object object) { return sizeOf(object); } }
public static SizeOf newInstance(final SizeOfFilter... filters) { return newInstance(true, true, filters); }
/** * Builds a new SizeOf that will filter fields according to the provided filter * * @param fieldFilter The filter to apply * @param caching whether to cache reflected fields * @param bypassFlyweight whether "Flyweight Objects" are to be ignored * @see org.ehcache.sizeof.filters.SizeOfFilter */ public SizeOf(SizeOfFilter fieldFilter, boolean caching, boolean bypassFlyweight) { ObjectGraphWalker.Visitor visitor; if (caching) { visitor = new CachingSizeOfVisitor(); } else { visitor = new SizeOfVisitor(); } this.walker = new ObjectGraphWalker(visitor, fieldFilter, bypassFlyweight); }
/** * Walk the graph and call into the "visitor" * * @param root the roots of the objects (a shared graph will only be visited once) * @return the sum of all Visitor#visit returned values */ long walk(Object... root) { return walk(null, root); }
public Builder addFilters(SizeOfFilter... filters) { for (SizeOfFilter filter : filters) { addFilter(filter); } return this; }
@Override public void internalize(Object... obj) { walker.walk(obj); }
@Test public void testPutUpdate() throws StoreAccessException { OnHeapStoreForTests<String, String> store = newStore(); store.put(KEY, VALUE); String otherValue = "otherValue"; store.put(KEY, otherValue); long delta = SIZEOF.deepSizeOf(otherValue) - SIZEOF.deepSizeOf(VALUE); assertThat(store.getCurrentUsageInBytes(), is(SIZE_OF_KEY_VALUE_PAIR + delta)); }
public static SizeOf newInstance(final SizeOfFilter... filters) { return newInstance(true, true, filters); }
/** * Builds a new SizeOf that will filter fields according to the provided filter * * @param fieldFilter The filter to apply * @param caching whether to cache reflected fields * @param bypassFlyweight whether "Flyweight Objects" are to be ignored * @see org.ehcache.sizeof.filters.SizeOfFilter */ public SizeOf(SizeOfFilter fieldFilter, boolean caching, boolean bypassFlyweight) { ObjectGraphWalker.Visitor visitor; if (caching) { visitor = new CachingSizeOfVisitor(); } else { visitor = new SizeOfVisitor(); } this.walker = new ObjectGraphWalker(visitor, fieldFilter, bypassFlyweight); }
/** * Measures the size in memory (heap) of the objects passed in, walking their graph down * Any overlap of the graphs being passed in will be recognized and only measured once * * @param listener A listener to visited objects * @param obj the root objects of the graphs to measure * @return the total size in bytes for these objects * @see #sizeOf(Object) */ public long deepSizeOf(VisitorListener listener, Object... obj) { return walker.walk(listener, obj); }
/** * {@inheritDoc} */ public long visit(Object object) { return sizeOf(object); } }
public Builder addFilters(SizeOfFilter... filters) { for (SizeOfFilter filter : filters) { addFilter(filter); } return this; }
@Test public void testCompute() throws StoreAccessException { OnHeapStoreForTests<String, String> store = newStore(); store.getAndCompute(KEY, (a, b) -> VALUE); assertThat(store.getCurrentUsageInBytes(), is(SIZE_OF_KEY_VALUE_PAIR)); final String replace = "Replace the original value"; long delta = SIZEOF.deepSizeOf(replace) - SIZEOF.deepSizeOf(VALUE); store.getAndCompute(KEY, (a, b) -> replace); assertThat(store.getCurrentUsageInBytes(), is(SIZE_OF_KEY_VALUE_PAIR + delta)); }
/** * Walk the graph and call into the "visitor" * * @param root the roots of the objects (a shared graph will only be visited once) * @return the sum of all Visitor#visit returned values */ long walk(Object... root) { return walk(null, root); }
@Test public void testReplace() throws StoreAccessException { OnHeapStoreForTests<String, String> store = newStore(); store.put(KEY, VALUE); long beforeReplace = store.getCurrentUsageInBytes(); store.replace("Another Key", "Another Value"); assertThat(store.getCurrentUsageInBytes(), is(beforeReplace)); String toReplace = "Replaced Value"; store.replace(KEY, toReplace); long delta = SIZEOF.deepSizeOf(toReplace) - SIZEOF.deepSizeOf(VALUE); long afterReplace = store.getCurrentUsageInBytes(); assertThat(afterReplace - beforeReplace, is(delta)); //when delta is negative store.replace(KEY, VALUE); assertThat(afterReplace - store.getCurrentUsageInBytes(), is(delta)); }
/** * Measures the size in memory (heap) of the objects passed in, walking their graph down * Any overlap of the graphs being passed in will be recognized and only measured once * * @param listener A listener to visited objects * @param obj the root objects of the graphs to measure * @return the total size in bytes for these objects * @see #sizeOf(Object) */ public long deepSizeOf(VisitorListener listener, Object... obj) { return walker.walk(listener, obj); }
@Test public void testReplaceThreeArg() throws StoreAccessException { OnHeapStoreForTests<String, String> store = newStore(); store.put(KEY, VALUE); long beforeReplace = store.getCurrentUsageInBytes(); store.replace(KEY, "Another Value", VALUE); assertThat(store.getCurrentUsageInBytes(), is(beforeReplace)); String toReplace = "Replaced Value"; store.replace(KEY, VALUE, toReplace); long delta = SIZEOF.deepSizeOf(toReplace) - SIZEOF.deepSizeOf(VALUE); long afterReplace = store.getCurrentUsageInBytes(); assertThat(afterReplace - beforeReplace, is(delta)); //when delta is negative store.replace(KEY, toReplace, VALUE); assertThat(afterReplace - store.getCurrentUsageInBytes(), is(delta)); }
public long deepSizeOf(Object... obj) { return walker.walk(null, obj); }