Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  Java object size overhead...  (Read 2390 times)
0 Members and 1 Guest are viewing this topic.
Offline Zer Gut Shnitzel

Senior Newbie





« Posted 2007-07-11 15:45:09 »

I did some tests creating lots of objects, I calculated that an empty Java object  carries around 22 bytes of overhead. My test was to instantiate 20 million objects  (class that has only a default constructor) on the heap and then watch windows process size.
I know this is not a rocket science kind of benchmark but it really bothers me that when I Instantiate lets say a million Point3fs two thirds of the memory consumed goes somewhere. Now, can somebody shed some light and provide more details on why is this normal and can this be worked around.
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #1 - Posted 2007-07-11 16:45:07 »

It can be more than 22bytes; depends on VM implementation.

I'm sure there must be some public documentation from Sun explaining the in-memory representation of an Object instance.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2007-07-11 17:39:43 »

32-bit OS: 2x4=8 bytes overhead per object
64-bit OS: 2x8=16 bytes overhead per object

arrays take like 12 more bytes.

(Sun Hotspot VM)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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 #3 - Posted 2007-07-11 18:41:29 »

Work around it by not creating a million objects. Use the flyweight pattern instead.

Cas Smiley

Offline Zer Gut Shnitzel

Senior Newbie





« Reply #4 - Posted 2007-07-11 19:30:17 »

Quote
32-bit OS: 2x4=8 bytes overhead per object
64-bit OS: 2x8=16 bytes overhead per object

arrays take like 12 more bytes.

(Sun Hotspot VM)

In this case 20 million objects would eat up around 160 Megs, references for these guys should be additional 80 Megs. Instantiating all these objects and storing into array end up using 436Megs according to windows task manager and using  (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) on windows xp (32-bit).  Am I wrong assuming that this should average in the range of 260-280 Megs.

Cheers

Offline Zer Gut Shnitzel

Senior Newbie





« Reply #5 - Posted 2007-07-11 19:38:26 »

Quote
Work around it by not creating a million objects. Use the flyweight pattern instead.

How would this pattern work for 20 million unique points?
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2007-07-11 20:46:45 »

Sorta like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
class LotsaPoints {

   private float[] x, y;

   LotsaPoints(int size) {
      x = new float[size];
      y = new float[size];
   }

   Point getPoint(int index) {
      return new ImmutablePoint(x[index], y[index]);
   }

   void setPoint(int index, ImmutablePoint p) {
      x[index] = p.getX();
      y[index] = p.getY();
   }
}


Cas Smiley

Offline Zer Gut Shnitzel

Senior Newbie





« Reply #7 - Posted 2007-07-11 21:54:34 »

Kinda; Not at all, too bad...  Grin

Thanks
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 (50 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (84 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!