• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing objects from a canvas class for min heap memory usage

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a midlet that is using loads of heap memory.

It has a canvas class, and I wondered what the most efficient way is (in terms of heap memory usage) to access the midlet classes fields from the canvas class.

As far as I'm aware, I have three options:

1. I can just pass the fields from the midlet class to the canvas class via the canvas's constructor.

2. When constructing the canvas in the midlet class's code, I can pass 'this' to the canvas's constructor. Then in the canvas class I can do MyMidlet myMidletInstance = this, and use the midlet class's fields by doing myMidletInstance.field1, etc.

3. I could declare the fields I want to access from the canvas class as static in the midlet class and use them in the canvas by doing MyMidlet.field1, etc.

Does anyone know which one of these methods would use the least amount of heap memory? And is there much between them in terms of heap consumption? (The midlet class fields I want to access from the canvas class are Strings, String[]'s, byte[]'s and byte[][]'s.)

Thanks in advance,
James
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try them out? I think #3 uses the least amount of heap memory but #2 will give you more flexibility when you need to extend the program in the future.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anyone's interested, I tested the three options out, with the canvas accessing the following fields from the midlet class:

String, String, String[], byte[], String[], boolean[], byte[][], byte[].

Results:
Option 2 used about 1kb less heap memory that option 1.
Option 3 used about 400b less memory that option 2.

(Michael, The reason I asked was because I thought someone may have known already.)

Cheers,
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic