Garbage Heap
Mac OS X 10.5.8, Java 1.5.0_22, NetBeans 6.7.1 Profile Memory every 10 allocations. The first chart shows a nearly linear growth in surviving generations, while the second shows that those thousands of instances take almost no memory.
Mac OS X 10.5.5, Java 1.5.0_16, NetBeans 6.1 Profile Memory every 1 allocation:
<pre><code><span style="color:#000">public class MemoryAllocationTest {
public static void main(String[] args) {
while (true) {
long startTime = System.nanoTime();
byte[] bytes = new byte[1024 * 1024];
System.out.println("Time taken in milliseconds : "
+ ((double)(System.nanoTime() - startTime) / 1000000));
}
}
}
Time taken in milliseconds : 1.642
Time taken in milliseconds : 1.566
Time taken in milliseconds : 69.078
Time taken in milliseconds : 1.679
Time taken in milliseconds : 1.704
Time taken in milliseconds : 69.402
Time taken in milliseconds : 1.666
Time taken in milliseconds : 1.562
Time taken in milliseconds : 69.145
Time taken in milliseconds : 2.14
Time taken in milliseconds : 1.677
Time taken in milliseconds : 68.013
</span></code></pre>