Skip to content

Commit

Permalink
JDK-8244297
Browse files Browse the repository at this point in the history
Added some javadoc to the testutility
Some methods/fields are now private
  • Loading branch information
FlorianKirmaier committed May 5, 2020
1 parent e7d9ad6 commit 72d1228
Showing 1 changed file with 49 additions and 11 deletions.
Expand Up @@ -39,14 +39,13 @@

public class JMemoryBuddy {

static int steps = 10;
static int overallTime = 1000;
static int sleepTime = overallTime / steps;
static boolean createHeapdump = false;
static int garbageAmount = 999999;
private static int steps = 10;
private static int overallTime = 1000;
private static int sleepTime = overallTime / steps;
private static boolean createHeapdump = false;
private static int garbageAmount = 999999;
private static String MX_BEAN_PROXY_TYPE = "com.sun.management:type=HotSpotDiagnostic";

static String outputFolderString = ".";
private static String outputFolderString = ".";

static {
outputFolderString = System.getProperty("jmemorybuddy.output",".");
Expand All @@ -56,7 +55,7 @@ public class JMemoryBuddy {
garbageAmount = Integer.parseInt(System.getProperty("jmemorybuddy.garbageAmount", "10"));
}

public static void createGarbage() {
private static void createGarbage() {
LinkedList list = new LinkedList<Integer>();
int counter = 0;
while(counter < garbageAmount) {
Expand All @@ -65,6 +64,11 @@ public static void createGarbage() {
}
}

/**
* Checks whethr the content of the WeakReference can be collected.
* @param weakReference
* @return It throws an excpetion when the weakReference was not collectable.
*/
public static void assertCollectable(WeakReference weakReference) {
if(!checkCollectable(weakReference)) {
AssertCollectable assertCollectable = new AssertCollectable(weakReference);
Expand All @@ -73,6 +77,11 @@ public static void assertCollectable(WeakReference weakReference) {
}
}

/**
* Checks whethr the content of the WeakReference can be collected.
* @param weakReference
* @return Returns true, when the provided WeakReference can be collected.
*/
public static boolean checkCollectable(WeakReference weakReference) {
return checkCollectable(steps, weakReference) > 0;
}
Expand Down Expand Up @@ -104,17 +113,33 @@ private static int checkCollectable(int stepsLeft, WeakReference weakReference)
return counter;
}

/**
* Checks whethr the content of the WeakReference can not be collected.
* @param weakReference
* @return It throws an excpetion when the weakReference was collectable.
*/
public static void assertNotCollectable(WeakReference weakReference) {
if(!checkNotCollectable(weakReference)) {
throw new AssertionError("Content of WeakReference was collected!");
}
}

/**
* Checks whethr the content of the WeakReference can not be collected.
* @param weakReference
* @return Returns true, when the provided WeakReference can be collected.
*/
public static boolean checkNotCollectable(WeakReference weakReference) {
createGarbage();
System.gc();
return weakReference.get() != null;
}

/**
* A standard method to define a test which checks code for specific memory semantic.
* The parameter of the lambda provides an API to define the required memory semantic.
* @param f
*/
public static void memoryTest(Consumer<MemoryTestAPI> f) {
LinkedList<WeakReference> toBeCollected = new LinkedList<WeakReference>();
LinkedList<AssertNotCollectable> toBeNotCollected = new LinkedList<AssertNotCollectable>();
Expand Down Expand Up @@ -176,7 +201,7 @@ public void setAsReferenced(Object ref) {
}


public static void createHeapDump() {
private static void createHeapDump() {
if(createHeapdump) {
try {
String dateString = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
Expand Down Expand Up @@ -206,8 +231,21 @@ private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException {
}

public static interface MemoryTestAPI {
/**
* After executing the lambda, the provided ref must be collectable. Otherwise an Exception is thrown.
* @param ref
*/
public void assertCollectable(Object ref);
/**
* After executing the lambda, the provided ref must be not collectable. Otherwise an Exception is thrown.
* @param ref
*/
public void assertNotCollectable(Object ref);

/**
* The provided reference won't be collected, until memoryTest finishes all it's tests.
* @param ref
*/
public void setAsReferenced(Object ref);
}

Expand All @@ -229,7 +267,7 @@ public String toString() {
}
}

static class AssertNotCollectable {
private static class AssertNotCollectable {
WeakReference<Object> assertNotCollectable;
String originalResultOfToString;

Expand All @@ -248,7 +286,7 @@ public String toString() {
}
}

static class SetAsReferenced {
private static class SetAsReferenced {
Object setAsReferenced;

SetAsReferenced(Object ref) {
Expand Down

0 comments on commit 72d1228

Please sign in to comment.