Memory and arrays

I stumbled into this little question yesterday, but don’t know enough about internal array mechanics to get a correct answer.

I’ve been writing a 3d mesh loader, and storing the vertices, normals etc. in their own arrays. I was basically trying to avoid having one long array of Vertex objects which could eat up memory very quickly and instead limit the number of objects to about 8ish arrays.

But i’ve been using float vertices[numVertices][3]; (storing x,y,z) which i’m not sure how its allocated. If i remember correctly, it’s stored as an array of arrays, not one big array - so i’ll actually end up using more objects :o

Is this correct? If so then i’m wondering if switching to vertices[3][numVertices]; would actually give me the constant number of objects regardless of the number of vertices…

Isn’t it strange you can never remember which way round it goes???

Actually what I’d do is have one single float array of float[3 * numvertices].

Actually what I’d really do is have a FloatBuffer with the vertices in so I could just give it straight to the graphics driver.

Cas :slight_smile:

Ah got to love the Java implementation of arrays. Either go the way Cas recomends, or if you want to keep the verticies logically separate (slightly slower), just create a new Vertex class, with three floats within, even doing this you will be saving space over the 2D array implementation. Every array has a length field. :wink:

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 );
}
}