SamplingAllocationMonitorAspect { @AfterReturning(pointcut = "call(*.new(..))", returning = "obj", argNames = "jp,obj") public void afterAllocation(final JoinPoint jp, final Object obj) { MutableInteger counter = ThreadLocalCounter.get(); int value = counter.getValue(); if (value < SAMPLE_COUNT) { counter.setValue(value + 1); } else { counter.setValue(0); StackTrace st = StackTrace.from(Thread.currentThread().getStackTrace(), 2); RECORDER.getRecorder(st).record(InstrumentationHelper.getObjectSize(obj)); } }  @AfterReturning(pointcut = "call(*.new(..))", returning = "obj", argNames = "jp,obj") void afterAllocation(final JoinPoint jp, final Object obj);  }
@Test public void testAfterAllocation() throws InterruptedException { System.setProperty("spf4j.perf.allocations.sampleTimeMillis", "1000"); System.setProperty("spf4j.perf.allocations.sampleCount", "10"); MemoryUsageSampler.start(500); OpenFilesSampler.start(500, 512, 1000, false); for (int i = 0; i < 1000; i++) { LOG.debug("T{}", i); if (i % 100 == 0) { Thread.sleep(500); } } testAllocInStaticContext(); TestClass.testAllocInStaticContext(); }