Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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  
  hypothetical memory usage question  (Read 2129 times)
0 Members and 1 Guest are viewing this topic.
Offline Noobtastic

Senior Newbie





« Posted 2009-07-10 17:22:49 »

Suppose I had something like 1 million instances of an object like this:

class foo {
 
    byte a;
    byte b;

}

And suppose that the first 500k have values assigned to 'a' and 'b', while the second 500k have 'a' and 'b' specifically set to 'null'.

Do the first 500k and the second 500k use the same amount of memory?

Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #1 - Posted 2009-07-10 17:41:51 »

bytes can't store the value null.

On a more general note, an instance of an Object uses the same amount of memory regardless of the values assigned to it's member variables.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Noobtastic

Senior Newbie





« Reply #2 - Posted 2009-07-10 18:15:52 »

So then, one must conclude that both the first 500k and second 500k in this scenario will require the same amount of memory.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2009-07-10 18:26:22 »

Quote
Do the first 500k and the second 500k use the same amount of memory?
yes

I guess that 1 000 000 object "foo" will use 1 000 000*(4+1+1) KB of memory and so 6 000 000 Bytes, the objects themselve will only use two bytes but you will have at least one reference for each (that can be any kind of reference structure as array/list but you will have one at least)

not 100% sure but I think that reference use 4 Bytes on 32 bits platforms and 8 Bytes on 64 bits

Edit : also you cant set null to a & b they are natives bytes not objects

Offline Noobtastic

Senior Newbie





« Reply #4 - Posted 2009-07-10 18:33:18 »

Your comment brings to mind another question, though it is no longer on the topic of the original post...

Vector vs. ArrayList: which is better in various situations?  I have at times used them interchangeably.

For example, performance, memory use, code style, etc...


Offline DzzD
« Reply #5 - Posted 2009-07-10 18:38:46 »

they are quite equivalent http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html , if you know the size of your structure (and if it diesn't change often) you will get the best performance & memory usage with a native array Foo[] foos;

Offline ewjordan

Junior Duke





« Reply #6 - Posted 2009-07-10 19:18:50 »

they are quite equivalent http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html , if you know the size of your structure (and if it diesn't change often) you will get the best performance & memory usage with a native array Foo[] foos;
...and even if you don't know the size in advance, if you're using the things to store/retrieve primitives like bytes, ints, floats, etc., for such large collections you'll really want to write your own dynamic arrays backed by primitive arrays, since you're probably looping over these things quite a bit.

If you search around the forums a bit, people came up with a very fast and full featured dynamic float array class a long time ago, in case you're not up to writing one yourself.

Ah, and re: Vector vs. ArrayList, Vectors are thread-safe, ArrayLists are not.  That means there's a bit of extra overhead when you use Vectors.  I'd use ArrayLists by default, unless you might be accessing the thing from multiple threads at the same time.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2009-07-10 20:04:33 »

With an array of a million objects, the 4-byte per object overhead will kill you before you've even stored a single byte. Store two parrellel byte[] arrays instead and you'll reduce the overhead from 4 million bytes to 8 bytes.

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

Senior Newbie





« Reply #8 - Posted 2009-07-10 20:48:12 »

Holy heck you're right!    Shocked

Well, that will change some things.

Like this, right?

byte[1m] aFoo;
byte[1m] bFoo;

Much better, memory-wise, than:
ArrayList<Foo>;   // stuffed with 1 million
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #9 - Posted 2009-07-15 02:27:28 »

and if you stick it in a Set and override equals() you'll save the most. 256 * 256 = 65 536

meh, this all reeks of premature optimalisation. If your collection doesn't change much just use copy-on-write variants.

Your comment brings to mind another question, though it is no longer on the topic of the original post...

Vector vs. ArrayList: which is better in various situations?  I have at times used them interchangeably.
ArrayList is not synchronised Vector is. these days there is lock coarsening so that might not matter much

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2009-07-15 10:30:56 »

Only in yer very latest VMs. Stick with ArrayList Smiley

Cas Smiley

Offline Noobtastic

Senior Newbie





« Reply #11 - Posted 2009-07-15 22:13:08 »

I went with two arrays instead of the Vector / ArrayList and saw a massive improvement.  Thanks very much, all!

(btw it was 3.6 million not 1 million  Wink  )


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.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (52 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (76 views)
2014-10-11 22:30:10
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!