Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (615)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Memory and arrays  (Read 2724 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Posted 2002-10-19 06:49:59 »

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  Shocked

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..

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline princec

« JGO Spiffy Duke »

Medals: 567
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #1 - Posted 2002-10-19 09:14:11 »

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 Smiley

Offline Exocet

Senior Newbie

« Reply #2 - Posted 2002-10-20 19:24:33 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder

Medals: 1

« Reply #3 - Posted 2002-12-13 15:28:11 »

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

malloc will be first against the wall when the revolution comes...
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (15 views)
2015-09-03 00:41:28

Andrew_3ds (24 views)
2015-09-01 19:08:10

afikri (16 views)
2015-08-31 09:30:22

afikri (24 views)
2015-08-31 09:30:07

afikri (12 views)
2015-08-31 09:27:24

afikri (16 views)
2015-08-31 09:26:40

Roquen (23 views)
2015-08-29 11:30:54

GamerC4 (34 views)
2015-08-22 20:38:50

GamerC4 (32 views)
2015-08-22 20:37:18

GamerC4 (37 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!