What is the size开发者_如何学运维 (in bytes) of the Hashtable object in J2ME? I mean what is the overhead for using a Hashtable?
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();
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!
精彩评论