Test 2 – Memory Efficiency, Continued
To further experiment with memory efficiency, we expanded the memory test to include an array of classes. Classes, as you probably know, are the foundation of object oriented programming, and virtually all modern software uses them in some capacity. As .NET and Java are both heavily object-oriented, the efficiency with which the two platforms can allocate objects is very important.
In this test we allocate and initialize 500,000 objects of type DataTestObject. DataTestObject encapsulates some data about a fictional bank account. The class is simple enough that it is easily portable from C# to Java with only slight modification. We also make use of two system types – strings and times:
public class DataTestObject
{
public double BankBalance;
public string BankAccountID;
public string CustomerFirstName;
public string CustomerLastName;
public DateTime MostRecentWithdrawal;
public DataTestObject()
{
BankBalance = 0.0;
BankAccountID = "DHC116A-111908-5";
CustomerFirstName = "Jon";
CustomerLastName = "Doe";
MostRecentWithdrawal = DateTime.Now;
}
public void UpdateBalance(double d)
{
BankBalance += d;
}
}
This class contains two functions, which perform operations on the object, as well as five fields for storing data. An array of 500,000 of these objects is created and initialized.
Class Memory Usage
Here we see .NET reversing the memory outlook and besting Java by a solid margin. This indicates that since .NET treats native types – such as double – as objects, that this incurs additional overhead of some form. Java, on the other hand, does not treat native types as objects and therefore can save on this overhead. However, when it comes to real-world object allocation, it appears that .NET is more efficient.