views:

130

answers:

2

What is the size (in bytes) of the Hashtable object in J2ME? I mean what is the overhead for using a Hashtable?

A: 

A hashtable is 24 bytes for the basic object + 2 ints (4 bytes each) for the _numberOfKeys and the _threshold. The _hash, _key and _value (internal hashtable variables) will be determined by the capacity of the hashtable and the size of the objects in the hashtable. The capacity is set to 11 if you don't pass it in the constructor, and the hashtable has logic to increase capacity if more is required.

The _hash is an array of ints (the hashs) and therefore equals the hashtable capacity (notice: capacity not number of keys) * 4 bytes. The _key and _value are arrays of Object type, so even if they're all null, they take the 4 bytes for empty pointers.

Hope this helps anyone!

Tamar
Obviously, that depends on who wrote the Hashtable.java class in the J2ME implementation on your phone. I assume this answer refers to the open source version from Sun Ltd PhoneME.
QuickRecipesOnSymbianOS
You are correct I should include the phone I'm writing for, which is a blackberry. I believe the Hashtable class was implemented by RIM.
Tamar
+2  A: 

For an empty hashtable this will probably vary widely by device.

You can get a ballpark measurement yourself as follows:

Runtime rt = Runtime.getRuntime();
long freeMem = rt.freeMemory();
Hashtable ht = new Hashtable();
long sizeofHashtable = freeMem - rt.freeMemory();
funkybro