Montag, 23. November 2009

Weak RuntimeStore?

The BlackBerry RuntimeStore is a great mechanism to support Interprocess Communication: You can store any Object and retrieve it from a different process, even a different application.
It is normally used for communication between a listener (i.e. PushListener) and a running application. But it also could be used for implementing system wide Singletons.

But using the RuntimeStore can be dangerous because it is a typical cause for memory leaks.
Let's assume an application put's in a reference to an object. The application exists (normally or abnormal) without removing that object. The Garbage Collector can not decide if that instance can be deleted. It remains in the RuntimeStore till it will be removed or the device reboots and so will every instance that is referenced from that object which can be a lot of "garbage".

Quite unknown is the BlackBerry (actually it's part of JME) implementation of WeakReferences. If the only remaining references to an objects are WeakReferences, the object will be removed by the garbage collector.

A colleague of mine had the idea to combine this with the RuntimeStore and according to a guy from RIM I asked at DevCon this should work:

Put it a WeakReference instead of a normal object to the store and if you application exists without removing the instance it should be garbage collected.

I did not try this myself but it should be worth a try.

Keine Kommentare:

Kommentar veröffentlichen