2

I usually make the classes for everything usually to combine several types together.
For example for combining int and double

public class IntAndDouble{
      public final int INT;
      public final double DOUBLE;
      public IntAndDouble(int i,double d){INT = i; DOUBLE = d;}
}

pretty much like creating oftentimes a small struct.

But I've noticed that for every small class like this, there is a ~1KB (300-1000 bytes) file in the jar
And when creating 10-15 of these small classes there can be an increase of ~30KB in the jar

This wouldn't be a problem in real Java where 30KB is nearly negligible,
but in J2ME where a regular app is usually around 200KB, 30KB is a huge significance.

What is the best way to make a 'small struct' in J2ME?

SmRndGuy
  • 1,719
  • 5
  • 30
  • 49
  • 1
    There is no way. You can use an array if you are using several field of the same type, but I'm sure this is not what you need. JavaME pretty much ask you not be too Object oriented. However, in JavaSE your classes should have meaningful names. You are basically implementing tuples... – Pablo Grisafi Mar 19 '13 at 19:07
  • 1
    I don't think it's correct to say "In JavaME a regular app is usually around 200kb". It's not JavaME. It's (older) devices. Newer JavaME enabled phones can easily run apps of 3-4mb. In other words: Only worry if you insist on supporting older phones. – mr_lou Mar 20 '13 at 05:34

2 Answers2

1

Unless you are using a really old phone with a tiny amount of memory, the amount of memory used to hold the code for a "struct-like" class is unlikely to be significant.

Note: that the size of the ".class" file does not reflect the in-memory footprint of the class. The ".class" file includes lots of stuff (type/method/class names and signatures and so on) that are only used when the class is being loaded and linked. After that it is typically garbage collected.

I don't know what the actual memory cost of a light-weight class is (and I can't measure it) but I would have predicted an order of magnitude less than you are saying.

What is the best way to make a 'small struct' in J2ME?

In most cases, exactly the way you have done it in your IntAndDouble example class.

If the fields all have the same base type, then you could use an array instead; e.g. int[] or Object[], but you would "pay" for that in other ways.

(Beware of premature optimization!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

We are targeting very weak devices in West Africa and other lower-tech areas in the world. We've found a number of devices that can only handle 500k worth of apps and data total (for all apps). There is a significant number of our consumers around the world that use these types of devices. So if you are going super-universal, I'd still consider doing things to keep my code as small as possible.

HalR
  • 11,411
  • 5
  • 48
  • 80