public class MemoryAssert extends Object
Object sizes are in an idealized JVM in which pointers are 4 bytes
(realistic even for modern 64-bit JVMs in which -XX:+UseCompressedOops
is the default)
but objects are aligned on 8-byte boundaries (so dropping an int
field does not always save memory).
import static org.jvnet.hudson.test.MemoryAssert.*;
to use.
Modifier and Type | Class and Description |
---|---|
static class |
MemoryAssert.HistogramElement |
Modifier and Type | Method and Description |
---|---|
static void |
assertGC(WeakReference<?> reference)
Deprecated.
|
static void |
assertGC(WeakReference<?> reference,
boolean allowSoft)
Forces GC by causing an OOM and then verifies the given
WeakReference has been garbage collected. |
static void |
assertHeapUsage(Object o,
int max)
Verifies that an object and its transitive reference graph occupy at most a predetermined amount of memory.
|
static List<MemoryAssert.HistogramElement> |
increasedMemory(Callable<Void> callable,
org.netbeans.insane.scanner.Filter... filters)
Counts how much more memory is held in Jenkins by doing some operation.
|
public static void assertHeapUsage(Object o, int max) throws Exception
WeakReference
and the like are ignored.
To use, run your test for the first time with max
of 0
;
when it fails, use the reported actual size as your assertion maximum.
When improving memory usage, run again with 0
and tighten the test to both demonstrate
your improvement quantitatively and prevent regressions.
o
- the object to measuremax
- the maximum desired memory usage (in bytes)Exception
public static List<MemoryAssert.HistogramElement> increasedMemory(Callable<Void> callable, org.netbeans.insane.scanner.Filter... filters) throws Exception
callable
- an actionfilters
- things to excludeException
@Deprecated public static void assertGC(WeakReference<?> reference)
public static void assertGC(WeakReference<?> reference, boolean allowSoft)
WeakReference
has been garbage collected.reference
- object used to verify garbage collection.allowSoft
- if true, pass even if SoftReference
s apparently needed to be cleared by forcing an OutOfMemoryError
;
if false, fail in such a case (though the failure will be slow)Copyright © 2004–2022. All rights reserved.