views:

154

answers:

3

I am working on querying the address book via J2ME and returning a custom Hashtable which I will call pimList. The keys in pimList {firstname, lastname} maps to an object (we'll call this object ContactInfo) holding (key, value) pairs e.g. work1 -> 44232454545, home1 -> 44876887787

Next I take firstName and add it into a tree. The nodes of the tree contains the characters from the firstName. e.g. "Tom" would create a tree with nodes:

"T"->"o"->"m"-> ContactInfo{ "work1" -> "44232454545", "home1" -> "44876887787" }

So the child of the last character m points to the same object instance in pimList. As I understand it, the purpose of WeakReferences is to that its pointer is weak and the object it points to can be easily GC'ed. In a memory constraint device like mobile phones, I would like to ensure I don't leak or waste memory. Thus, is it appropriate for me to make:

  1. pimList's values to be a WeakReference
  2. The child of node "m" to point to WeakReference

?

+1  A: 

It should work. You will need to handle the case where you are using the returned Hashtable and the items are collected however... which might mean you want to rethink the whole thing.

If the Hashtable is short lived then there likely isn't a need for the weak references.

You can remove the items out of the Hashtable when you are done with them if you want them to be possibly cleaned up while the rest of the Hashtable is stll being used.

TofuBeer
*pimList* is basically maintained by a separate background thread that monitors changes to the addressbook. If a new contact is added, or a contact is removed; *pimList* will be updated. If cleaning up the Hashtable during app exit will avoid memory leaks, then I won't need WeakReferences. Right?
ashitaka
Shouldn't an application exit clear the memory, even on J2ME devices?
ReneS
I don't think you want a weakreference though... the point is that the references can disappear whenever something isn't referencing them... and I do not think that is what you want.
TofuBeer
A: 

I am not sure if the WeakMap is the right thing here. If you do not hold strong references anywhere in your application, the data in the map will disappear nearly immediately, because nobody is referencing it.

A weak map is a nice thing, if you want to find things again, that are still in use elsewhere and you only want to have one instance of it.

But I might not get your data setup right... to be honest.

ReneS
+1  A: 

Not sure I exactly understood what you try to do but an objects reachability is determined by the strongest reference to it (hard reference is stronger than soft reference which is stronger than weak reference which is stronger than phantom reference).

Hard referenced objects won't be garbage collected. Soft referenced objects will be garbage collected only if JVM runs out of memory, weak referenced objects will be garbage collected as soon as possible (this is theory it depends on the JVM and GC implementation).

So usually you use softreference to build a cache (you want to reference information as long as possible). You use weakreference to associate information to an object that is hard referenced somewhere, so if the hardreferenced object is no longer referenced the associated information can be garbage collected - use weakhashmap for that.

hope this helps...

pgras