Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 Variable Size of another Object?  (Read 1000 times)
0 Members and 1 Guest are viewing this topic.
Offline xslaught

Junior Newbie





« Posted 2012-08-06 18:46:25 »

If I have an object:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Element {
   public int id; // 4 byte
 
   public Element(int id) {
      this.id = id;
   }
}
// This would be a total of 12 bytes from my understanding (8 for header, 4 for int id)

// and then have another class
public class Unit {
   public int uid; // 4 byte
  public Element element; // ?? byte
}


would the the second object's size be: 16 bytes?
8 header, 4 int uid, 4 Element (reference?)
or would it contain the size of the Element object as well thus end up being 24 bytes total?
8 header, 4 int uid, 12 Element
What I'm asking is how many bytes is ?? byte within the Unit object?

I'll end up using a lot of objects so I need to figure out the best way to store an object.
Would it be better to store variable element within Unit as an int elementId field or use Element element? With int I'd be doing a search to grab the element but I will be storing all of these into a HashMap so it wouldn't be a linear search.

How it'll work is both are stored in 2 separate HashMap, but when I need to read the Element of Unit, I can use just unit.element to return Element or do elementHashMap.get(unit.elementId) depending on how I structure the data
Offline Roquen
« Reply #1 - Posted 2012-08-06 19:02:25 »

The size of element in your second class will be 4 bytes (the cost of the reference or pointer...however you want to look at it.)
Online Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-08-06 20:18:27 »

It can just as well be 8 bytes per reference, on a 64 bit JVM where pointers are not compressed.

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
Online Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-08-06 20:23:39 »

The sizeof an instance of a class is also rounded up to a multiple of 16 bytes.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #4 - Posted 2012-08-06 20:27:46 »

Which brings up a question I've never bothered to figure out:  Is pointer compression the default?
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2012-08-06 20:31:57 »

Uncompressed OOPs is the default I think according to this.

Cas Smiley

Offline ReBirth
« Reply #6 - Posted 2012-08-07 04:08:35 »

So your Unit object will be 8 + 4 + 8 = 20 bytes.

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.

CopyableCougar4 (5 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!