Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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 2507 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel


Medals: 56
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..

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

« JGO Spiffy Duke »


Medals: 422
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


http://t-machine.org


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

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Gibbo3771 (12 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!