// Number of times we will try to count the heap in various ways. If we are unlucky and end up in // the middle of a GC we could incorrectly fail. This is expected to be incredibly rare so 10 // retries should be more than sufficient. privatestaticfinalint ITERATE_RETRIES = 10; privatestaticfinalclass Foobar {}
privatestaticvoid testHeapCount() throws Exception {
IllegalStateException lastThrow = new IllegalStateException( "Failed to get consistent counts after " + ITERATE_RETRIES + " retries");
Foobar[] foobars = new Foobar[123]; for (int i = 0; i < foobars.length; i++) {
foobars[i] = new Foobar();
} for (int i = 0; i < ITERATE_RETRIES; i++) { try { int all = iterateThroughHeapCount(0, null, Integer.MAX_VALUE); int tagged = iterateThroughHeapCount(HEAP_FILTER_OUT_UNTAGGED, null, Integer.MAX_VALUE); int untagged = iterateThroughHeapCount(HEAP_FILTER_OUT_TAGGED, null, Integer.MAX_VALUE); int taggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_UNTAGGED, null,
Integer.MAX_VALUE); int untaggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_TAGGED, null,
Integer.MAX_VALUE); int filteredClass = iterateThroughHeapCount(0, Foobar.class, Integer.MAX_VALUE);
privatestatic Object[] GenTs(Class<?> k) throws Exception {
Object[] ret = new Object[new Random().nextInt(100) + 10]; for (int i = 0; i < ret.length; i++) {
ret[i] = k.newInstance();
} return ret;
}
privatestaticvoid checkEq(int a, int b) { if (a != b) {
Error e = new Error("Failed: Expected equal " + a + " and " + b);
System.out.println(e);
e.printStackTrace(System.out);
}
}
publicstaticvoid doTest() throws Exception {
A a = new A();
B b = new B();
B b2 = new B();
C c = new C();
A[] aArray = new A[5];
String s = "Hello World";
public String toString() { return"{tag=" + tag + ", class-tag=" + classTag + ", size=" +
(tag >= 100 ? "<class>" : size) // Class size is dependent on 32-bit vs 64-bit, // so strip it.
+ ", length=" + length + "}";
}
}
privatestatic ArrayList<HeapElem> sort(int n, long classTags[], long sizes[], long tags[], int lengths[]) {
ArrayList<HeapElem> ret = new ArrayList<HeapElem>(n); for (int i = 0; i < n; i++) {
HeapElem elem = new HeapElem();
elem.classTag = classTags[i];
elem.size = sizes[i];
elem.tag = tags[i];
elem.length = lengths[i];
ret.add(elem);
}
Collections.sort(ret); return ret;
}
privatestaticinterface Inf1 { publicfinalstaticint A = 1;
}
privatestaticinterface Inf2 extends Inf1 { publicfinalstaticint B = 1;
}
privatestaticclass IntObject implements Inf1 { byte b = (byte)1; char c= 'a'; short s = (short)2; int i = 3; long l = 4;
Object o = new Object(); staticint sI = 5;
}
privatestaticclass FloatObject extends IntObject implements Inf2 { float f = 1.23f; double d = 1.23;
Object p = new Object(); staticint sI = 6;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.