> or arrange your objects into buckets somehow and have
> soft references to the buckets
En, It's a good idea. but I think it doesn't work.
My problem is:
There are many caches(cache1, cache2.......), they may cache a lot of same object instance.
then, I want to keep a object pool, and try to get the equal object from pool before put a object into a cache, if there is an equal object, put the equal object into cache, if there is no equal object, put the target object into pool and cache. the object pool may very huge because there is lot of objects. So it's necessary to release the object in the pool if no cache referes to the object(the caches contain the object may removed). Then SoftReference come into my mind. I try to create a SoftReference for each object in the pool, but soonly, I find there are two problems:
1, A lot SoftReference instances take much memory.
2, Need some time to new and reclaim the SoftReference intances.
I think SoftReference may work fine to cache the object frequently used,
but the object number can't be too large.
Now, I don't use object pool. replace starting a thread to scan those cache s and combine the same objects intervally. It can scan about 2000,000 objects one second in my pc. It's ok for my project. But I am looking for some better way still.
> my mind. I try to create a SoftReference for each
> object in the pool, but soonly, I find there are two
> problems:
> 1, A lot SoftReference instances take much memory.
> 2, Need some time to new and reclaim the
> SoftReference intances.
>
How will you do object oriented programming if you are obsessed with the number of objects? Your focus is incorrect. This is not the primary focus of OO programming. You can speed things up and reduce resources if you wish by programming in a sequential fashion. But it wont be so manageable.
A soft reference is not that heavy. Plus one hopes its much lighter than the thing it referrs to, else there is really no point. I would not bother caching small easily createable objects.