Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (755)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
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  
  Getting the memory size out of a class?  (Read 1170 times)
0 Members and 1 Guest are viewing this topic.
Offline idFade

Junior Newbie

« Posted 2011-06-15 16:37:48 »

I made a class for a tilemap that I want to get the amount of memory it allocates of.

The class looks like this:
import java.awt.*;

public class Tile {

   protected Image tileImage;
   protected int xposition, yposition;
   public Tile(Image tile, int x, int y){
      tileImage = tile;
      xposition = x;
      yposition = y;
   public Image getImage(){
      return tileImage;
   public int getImageWidth(){
      return tileImage.getWidth(null);
   public int getImageHeight(){
      return tileImage.getHeight(null);

How do I do this?

I need to create a very large 2D array so I need to know how much each reference of this class takes up.
Offline cylab

JGO Kernel

Medals: 180

« Reply #1 - Posted 2011-06-15 19:49:45 »

First: you don't need to know the size of anything to make a 2d array in java.

Despite that, the size of the reference would be the size of a memory pointer, so afaik 32 or 64 bit depending on CPU-Architecture and JVM.

The size of your class' instance would be the size of the reference to the image + the size of the two ints. Could be there is some more overhead that the jvm needs for an object instance, but I don't think so. For the overall memory consumption you need to add the reference to the instance and of course the Image itself.

If you want to dive into this, you could take the time and make yourself comfortable with a java profiler (Netbeans contains one and the standalone VisualVM might have the needed features, too), so you can see exactly what memory your game is using.

As for your tiled engine, it might make more sense to store one image with all tiles and only the coordinates of the rects containing the specific part in your Tile. 

Mathias - I Know What [you] Did Last Summer!
Offline idFade

Junior Newbie

« Reply #2 - Posted 2011-06-15 22:15:33 »

Thanks, now that you mention it, using a strip of tiles is indeed much better than having each tile store a reference to an image file, I didn't think of that.

I wanted to know how much memory one reference of the tile would take up since I plan on creating a very large 2D array of tiles, and I don't want to consume all my system memory. If it indeed only takes up enough for the variables and the image reference then it wouldn't be that much ^^
Pages: [1]
  ignore  |  Print  

DesertCoockie (36 views)
2018-05-13 18:23:11

nelsongames (82 views)
2018-04-24 18:15:36

nelsongames (74 views)
2018-04-24 18:14:32

ivj94 (755 views)
2018-03-24 14:47:39

ivj94 (85 views)
2018-03-24 14:46:31

ivj94 (626 views)
2018-03-24 14:43:53

Solater (101 views)
2018-03-17 05:04:08

nelsongames (182 views)
2018-03-05 17:56:34

Gornova (408 views)
2018-03-02 22:15:33

buddyBro (1068 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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!