What is eating my memory?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

What is eating my memory?

Postby matejdro » Fri May 04, 2012 8:26 pm

I'm trying to fill relatively large array with some objects. Here is my code:

Code: Select all
private static Polje[][][] mreza = new Polje[2][30][30];
for (byte x = 0; x < mreza[0].length; x++)
{
   for (byte y = 0; y < mreza[0][0].length; y++)
   {
      mreza[0][x][y] = new Polje(false, x, y);
      mreza[1][x][y] = new Polje(true, x, y);
      System.out.println(Runtime.getRuntime().freeMemory());
      LCD.refresh();
      
      Delay.msDelay(2000);
   }
}
Code: Select all
public class Polje implements Transmittable {
   public TipPolja tip;
   byte x;
   byte y;

   byte razdalja;
   
   BitSet biti;
   
   public Polje(Boolean zgoraj, byte x, byte y)
   {
      biti = new BitSet(8);
      biti.clear();
      
      biti.set(4, zgoraj);
      this.x = x;
      this.y = y;
      
      tip = TipPolja.NERAZISKANO;
   }
Code: Select all
public enum TipPolja {
   NERAZISKANO,
   PREVOZENO,
   CRNO;   
}

(Sorry for weird looking names, they are not in english).

However, it will throw out of memory error. So I have decided to print out free memory to figure out, how much memory each Polje takes.

After each run of loop, there is 88 bytes less free memory. Loop initializes two objects, so that means that size of 1 Polje is 44 bytes :o

Polje one contains enum (1 byte I guess?), 3 bytes and Bitset with 8 bits (I assume it takes 1 byte + some overhead). That means that total size of data in object is around 5 bytes.

Does that mean that there is 39 bytes of object overhead? :o It sounds a lot to me. Or maybe I miscalculated something?
matejdro
Novice
 
Posts: 54
Joined: Wed Mar 14, 2012 9:10 am

Re: What is eating my memory?

Postby gloomyandy » Wed May 09, 2012 8:16 pm

Things do not work the way you think they work.. Firstly any object in leJOS has a 4 byte overhead and has the size rounded up to a 4 byte multiple making the minimum object size 8 bytes. Secondly an enum is actually an object, thirdly the BitSet implementation actually stores the bits in and array, and an array is an object so we have...
1 Your main object contains a ref to the enum (4 bytes), a ref to the BitSet (4 bytes) plus 3 bytes, so 11 bytes in total which then gets rounded up to 12 bytes and has the object header 4 bytes so 16 bytes in total.
2. The BitSet has an array containing 1 byte since this is an object the array will occupy 1 byte rounded up to 4 bytes plus the header making it 8 bytes, the BitSet object will have the reference to the array (4 bytes) plus the header so another 8 bytes making 16 bytes in total.

So I would expect that each or your objects will use 32 bytes of memory. So what about the other bytes you see going down, well the call to print out the amount of free memory will be generating garbage, and as freeMemory does not force a garbage collection the printed amount will include that garbage...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3944
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: What is eating my memory?

Postby matejdro » Wed May 09, 2012 8:25 pm

Yeah, I have replaced bitset and enum with two bytes and improvement is huge. I don't remember how much each class uses (and I don't have NXT near me currently), but after whole array was full, there was around 24kb of RAM left, which is plenty (while before it threw out out of memory error).

It's shame that I should do that, since objects are core of the java. But I guess this is the trade off for running java on 64kb ram machine.

Thanks for your help.
matejdro
Novice
 
Posts: 54
Joined: Wed Mar 14, 2012 9:10 am


Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron
more stuff