Test 2 – Memory Efficiency
One feature of managed code is dynamic, and automatic, memory management. While easing the complexities of the development process by abstracting out memory management significantly, memory managers add overhead. Both .NET and Java use more memory to store the same data in RAM than the venerable C/C++.
In this test, we will pit the memory managers of Java against .NET in comparative memory allocation tasks under Windows XP.
The first comparison compares the memory required to allocate 100,000 floating point elements (double). Please note that this first test is meant more as a baseline to establish how much base memory overhead there is than as a direct comparison:
Baseline Memory Comparison
Memory was measured by computing the total amount of memory used by the process. Here, things are fairly close. The double type in Java 1.5 is a 64-bit IEEE 754 value, while in .NET the double data type is also a 64-bit IEEE-compliant value. The memory was allocated in an array structure. This array was initialized with values to ensure that real rather than "virtual" memory was allocated. Now that we have established an overhead baseline, let’s up the number of elements to 5 million:
Memory Comparison 2
Again, we have a clear winner – Java 1.5. Note that all of our graphs start at zero to give as fair a comparison as possible. .NET is using more than twice the amount of memory Java uses to store the same amount of data – 19.9 MB versus a whopping 47.8.