| 1 |
// This is adapted from a benchmark written by John Ellis and Pete Kovac |
| 2 |
// of Post Communications. |
| 3 |
// It was modified by Hans Boehm of Silicon Graphics. |
| 4 |
// |
| 5 |
// This is no substitute for real applications. No actual application |
| 6 |
// is likely to behave in exactly this way. However, this benchmark was |
| 7 |
// designed to be more representative of real applications than other |
| 8 |
// Java GC benchmarks of which we are aware. |
| 9 |
// It attempts to model those properties of allocation requests that |
| 10 |
// are important to current GC techniques. |
| 11 |
// It is designed to be used either to obtain a single overall performance |
| 12 |
// number, or to give a more detailed estimate of how collector |
| 13 |
// performance varies with object lifetimes. It prints the time |
| 14 |
// required to allocate and collect balanced binary trees of various |
| 15 |
// sizes. Smaller trees result in shorter object lifetimes. Each cycle |
| 16 |
// allocates roughly the same amount of memory. |
| 17 |
// Two data structures are kept around during the entire process, so |
| 18 |
// that the measured performance is representative of applications |
| 19 |
// that maintain some live in-memory data. One of these is a tree |
| 20 |
// containing many pointers. The other is a large array containing |
| 21 |
// double precision floating point numbers. Both should be of comparable |
| 22 |
// size. |
| 23 |
// |
| 24 |
// The results are only really meaningful together with a specification |
| 25 |
// of how much memory was used. It is possible to trade memory for |
| 26 |
// better time performance. This benchmark should be run in a 32 MB |
| 27 |
// heap, though we don't currently know how to enforce that uniformly. |
| 28 |
// |
| 29 |
// Unlike the original Ellis and Kovac benchmark, we do not attempt |
| 30 |
// measure pause times. This facility should eventually be added back |
| 31 |
// in. There are several reasons for omitting it for now. The original |
| 32 |
// implementation depended on assumptions about the thread scheduler |
| 33 |
// that don't hold uniformly. The results really measure both the |
| 34 |
// scheduler and GC. Pause time measurements tend to not fit well with |
| 35 |
// current benchmark suites. As far as we know, none of the current |
| 36 |
// commercial Java implementations seriously attempt to minimize GC pause |
| 37 |
// times. |
| 38 |
|
| 39 |
class Node { |
| 40 |
Node left, right; |
| 41 |
int i, j; |
| 42 |
Node(Node l, Node r) { left = l; right = r; } |
| 43 |
Node() { } |
| 44 |
} |
| 45 |
|
| 46 |
public class gcbench { |
| 47 |
|
| 48 |
public static final int kStretchTreeDepth = 18; // about 16Mb |
| 49 |
public static final int kLongLivedTreeDepth = 16; // about 4Mb |
| 50 |
public static final int kArraySize = 500000; // about 4Mb |
| 51 |
public static final int kMinTreeDepth = 4; |
| 52 |
public static final int kMaxTreeDepth = 16; |
| 53 |
|
| 54 |
// Nodes used by a tree of a given size |
| 55 |
static int TreeSize(int i) { |
| 56 |
return ((1 << (i + 1)) - 1); |
| 57 |
} |
| 58 |
|
| 59 |
// Number of iterations to use for a given tree depth |
| 60 |
static int NumIters(int i) { |
| 61 |
return 2 * TreeSize(kStretchTreeDepth) / TreeSize(i); |
| 62 |
} |
| 63 |
|
| 64 |
// Build tree top down, assigning to older objects. |
| 65 |
static void Populate(int iDepth, Node thisNode) { |
| 66 |
if (iDepth<=0) { |
| 67 |
return; |
| 68 |
} else { |
| 69 |
iDepth--; |
| 70 |
thisNode.left = new Node(); |
| 71 |
thisNode.right = new Node(); |
| 72 |
Populate (iDepth, thisNode.left); |
| 73 |
Populate (iDepth, thisNode.right); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Build tree bottom-up |
| 78 |
static Node MakeTree(int iDepth) { |
| 79 |
if (iDepth<=0) { |
| 80 |
return new Node(); |
| 81 |
} else { |
| 82 |
return new Node(MakeTree(iDepth-1), |
| 83 |
MakeTree(iDepth-1)); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static void PrintDiagnostics() { |
| 88 |
long lFreeMemory = Runtime.getRuntime().freeMemory(); |
| 89 |
long lTotalMemory = Runtime.getRuntime().totalMemory(); |
| 90 |
|
| 91 |
System.out.print(" Total memory available=" |
| 92 |
+ lTotalMemory + " bytes"); |
| 93 |
System.out.println(" Free memory=" + lFreeMemory + " bytes"); |
| 94 |
} |
| 95 |
|
| 96 |
static void TimeConstruction(int depth) { |
| 97 |
Node root; |
| 98 |
long tStart, tFinish; |
| 99 |
int iNumIters = NumIters(depth); |
| 100 |
Node tempTree; |
| 101 |
|
| 102 |
System.out.println("Creating " + iNumIters + |
| 103 |
" trees of depth " + depth); |
| 104 |
tStart = System.currentTimeMillis(); |
| 105 |
for (int i = 0; i < iNumIters; ++i) { |
| 106 |
tempTree = new Node(); |
| 107 |
Populate(depth, tempTree); |
| 108 |
tempTree = null; |
| 109 |
} |
| 110 |
tFinish = System.currentTimeMillis(); |
| 111 |
System.out.println("\tTop down construction took " |
| 112 |
+ (tFinish - tStart) + "msecs"); |
| 113 |
tStart = System.currentTimeMillis(); |
| 114 |
for (int i = 0; i < iNumIters; ++i) { |
| 115 |
tempTree = MakeTree(depth); |
| 116 |
tempTree = null; |
| 117 |
} |
| 118 |
tFinish = System.currentTimeMillis(); |
| 119 |
System.out.println("\tBottom up construction took " |
| 120 |
+ (tFinish - tStart) + "msecs"); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public static void main(String args[]) { |
| 125 |
Node root; |
| 126 |
Node longLivedTree; |
| 127 |
Node tempTree; |
| 128 |
long tStart, tFinish; |
| 129 |
long tElapsed; |
| 130 |
|
| 131 |
System.out.println("Garbage Collector Test"); |
| 132 |
System.out.println( |
| 133 |
" Stretching memory with a binary tree of depth " |
| 134 |
+ kStretchTreeDepth); |
| 135 |
PrintDiagnostics(); |
| 136 |
tStart = System.currentTimeMillis(); |
| 137 |
|
| 138 |
// Stretch the memory space quickly |
| 139 |
tempTree = MakeTree(kStretchTreeDepth); |
| 140 |
tempTree = null; |
| 141 |
|
| 142 |
// Create a long lived object |
| 143 |
System.out.println( |
| 144 |
" Creating a long-lived binary tree of depth " + |
| 145 |
kLongLivedTreeDepth); |
| 146 |
longLivedTree = new Node(); |
| 147 |
Populate(kLongLivedTreeDepth, longLivedTree); |
| 148 |
|
| 149 |
// Create long-lived array, filling half of it |
| 150 |
System.out.println( |
| 151 |
" Creating a long-lived array of " |
| 152 |
+ kArraySize + " doubles"); |
| 153 |
double array[] = new double[kArraySize]; |
| 154 |
for (int i = 0; i < kArraySize/2; ++i) { |
| 155 |
array[i] = 1.0/i; |
| 156 |
} |
| 157 |
PrintDiagnostics(); |
| 158 |
|
| 159 |
for (int d = kMinTreeDepth; d <= kMaxTreeDepth; d += 2) { |
| 160 |
TimeConstruction(d); |
| 161 |
} |
| 162 |
|
| 163 |
if (longLivedTree == null || array[1000] != 1.0/1000) |
| 164 |
System.out.println("Failed"); |
| 165 |
// fake reference to LongLivedTree |
| 166 |
// and array |
| 167 |
// to keep them from being optimized away |
| 168 |
|
| 169 |
tFinish = System.currentTimeMillis(); |
| 170 |
tElapsed = tFinish-tStart; |
| 171 |
PrintDiagnostics(); |
| 172 |
System.out.println("Completed in " + tElapsed + "ms."); |
| 173 |
} |
| 174 |
} // class JavaGC |