Making a Vertex class is possibly the worst mistake you could make, if trying to save memory. Every object in Java takes up a lot more excess space than an aray; you have at least two pointers - the class-pointer and the object-pointer (IIRC you have more like 4-5 extra “hidden” variables). Arrays are ONLY in java BECAUSE they are more efficient than Objects (allegedly; no-one’s ever suggested an other good reasons for them?).
I did a test to compare mem-usage for A:int[ 10,000 ] and for B:int[ 10,000 ][ 10 ] (source code at end), and found:
A: mem-usage = 4.1 * size of array (the 0.1 includes the some String’s I had to make to get output data etc)
B: mem-usage = 5.9 * size of array * size of small-arrays.
From this I can only conclude that an array of arrays is pretty efficient.
I would also point out that different VM’s will implement arrays differently. I suspect that one reason IBM’s 1.1.7 and 1.2.x Java VM’s were SOOOO much faster than Sun’s ones was because they provided more intelligent array handling (that’s just a guess, but I saw speed improvements of up to 300% using IBM JDK’s, same version numbers as Sun’s, and I was only really working with arrays). Sun has (obviously) got a lot more conscientious, and 1.3 and 1.4 VM’s seem pretty good.
I personally wouldn’t bother optimizing this any further - unless you want to auto-detect the VM version, AND the VM-vendor, and hand-optimize for each one?
There are advantages to splitting your data-structure up into smaller ones - you give the VM more flexibility in managing where in memory the array-elements are held. However, sometimes that just results in giving the VM enough rope to hang itself, so you can’t win really ;)). I certainly second PrinceC’s suggestion - Go with Buffer’s, whenever speed really matters. They really really are worth it (even if it’s not going to be until jva 1.5 that Sun realises they “forgot” to include CircularBuffers and a few other frequently used aspects of Buffers. Sigh. Memories of Java 1.1 without a List datatype!)
[begin source]
public class simpleMemUsageTester
{
public static final int BIG_NUMBER = 100000;
public static final int SMALL_NUMBER = 10;
public static void main( String[] args )
{
if( args.length < 1 )
{
out( “Usage: \n Either “java simpleMemUsageTester single”\n Or “java simpleMemUsageTester double”” );
System.exit( 0 );
}
Runtime rt = Runtime.getRuntime();
long totalMemoryStart = rt.totalMemory();
long currentTotalMemory;
long currentMemory = rt.freeMemory();
long lastMemory;
if( args[0].equalsIgnoreCase( "single" ) )
{
int[] bigSingleArray = new int[ BIG_NUMBER ];
lastMemory = currentMemory;
currentMemory = rt.freeMemory();
currentTotalMemory = rt.totalMemory();
out( "mem used by int["+BIG_NUMBER+"] = "+(lastMemory-currentMemory + currentTotalMemory - totalMemoryStart) );
for( int i=0; i<bigSingleArray.length; i++ )
{
bigSingleArray[ i ] = (int)( Math.random() * Integer.MAX_VALUE );
}
currentMemory = rt.freeMemory();
out( "mem used by int["+BIG_NUMBER+"] once filled with ints = "+(lastMemory-currentMemory + currentTotalMemory - totalMemoryStart) );
}
else if( args[0].equalsIgnoreCase( "double" ) )
{
int[][] big2DArray = new int[ BIG_NUMBER ][ SMALL_NUMBER ];
lastMemory = currentMemory;
currentMemory = rt.freeMemory();
currentTotalMemory = rt.totalMemory();
out( "mem used by int["+BIG_NUMBER+"]["+SMALL_NUMBER+"] = "+(lastMemory-currentMemory + currentTotalMemory - totalMemoryStart) );
for( int i=0; i<big2DArray.length; i++ )
{
for( int k=0; k<big2DArray[0].length; k++ )
{
big2DArray[ i ][ k ] = (int)( Math.random() * Integer.MAX_VALUE );
}
}
currentMemory = rt.freeMemory();
out( "mem used by int["+BIG_NUMBER+"]["+SMALL_NUMBER+"] once filled with ints = "+(lastMemory-currentMemory + currentTotalMemory - totalMemoryStart) );
}
else
{
out( "Usage: \n Either \"java simpleMemUsageTester single\"\n Or \"java simpleMemUsageTester double\"" );
System.exit( 0 );
}
}
public static void out( String s )
{
System.out.println( s );
}
}