Hi all,

I have a situation where I need to pool a certain type of objects
based on their value, so that for each for each person ID I have at
most one PersonVO living at a certain moment

This way I can always test equality as follows:

personVO1 === personVO2

instead of a custom value based method:

personVO1.equals( personVO2 )

The most obvious way of achieving this is to use Factory backed by a
dictionary or hashmap using personID as key. The problem is that,
after some time, I can potentially deal with tens of thousands of
cummulative distinct persons, but each one is used only for a couple
of minutes and won't occur again.

Having each instance referenced by a hashmap will only result in an
inevitable memory leak.

So, basically, what I need is a dictionary with weak *references*
instead of keys...
I thought about using one dictionary in the middle and store the
PersonVO instance as key

( pseudocode )

to store

var tempDictionary:Dictionary = new Dictionary( true );
tempDictionary[ personVO ] = true;
personIDToPersonVOMap[ personID ] = tempDictionary;

to retrieve

for ( var personVO:PersonVO in personIDToPersonVOMap[ personID ] )
    return personVO;
// if for loop does't hit any key then the value object was garbage
collected, and I can create a new one


This should, in theory, work... at the expense of some obvious
overhead ( one new Dictionary instance per value object ).

A second approach I was thinking about is not so straightforward but
might perform better:

- Use a normal hashmap to store the value objects
- Every now and then, create a dictionary with weak keys and put all
the value objects as KEYS
- delete the hashmap
- wait for GC to run
- after some time, or on demand, iterate over the keys ( that were not
GCCollected ) and repopulate the hashmap

Of course the latter approach will only work if the GC runs during the
no-hashmap interval.
Is there any way to fire the GC manually? ( creating a zillion empty
objects perhaps )

Any ideas?

Thanks,
Aldo



-- 
:::: Aldo Bucchi ::::
+1 858 539 6986
+56 9 8429 8300
+56 9 7623 8653
skype:aldo.bucchi

Reply via email to