Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Full Identity Map that has relationships to objects cached in weaker types

The easiest recommendation is to not map the messages from the user but retrieve them separately. You could write a getMessages() method on the User that executed a query. If you wanted to be cleaner you could register for the EclipseLink "postClone" event and query the collection there so Users loaded into a PersistenceContext would always have the collection available. Once the PersistenceContext was closed() the collection would garbage collect.

Your comment on the SoftCacheWeakIdentityMap is not quite correct. The Entities in this map type are still referenced in a Map of weak references and lookups are done against the Map not against the LinkedList. The LinkedList, through the Soft references, provides the caching by preventing a subset of the weak references from being garbage collected. The LinkedList is never searched.

zebhed wrote:
Thanks Jason, thanks Gordon.

Ok, this could be a problem. I want to make an example:

I have an entity User that has a one-to-many relationship to Message (User
(1) -> Message (N)). I have to cache all Users, but I do not want to cache
all Messages.
Let´s say I have 100.000 users and every user has about 100 messages. As I
said, I want to cache all Users but only a few messages (let´s say about
10.000). Now I can think of two approaches.

a) I cache all Users by using a Full Identity Map. Messages are cached by
using a Weak Identity Map. The problem is that Messages will never be
garbage collected (unless they are deleted), because they are hard
referenced by the User. This will lead to extreme memory consumption (and

b) I cache all Users by using a Soft Cache Weak Identity Map. Messages are
cached by using a Weak Identity Map. I doubt a Soft Cache Weak Identity Map
is still performant when its size is very large (100.000). This is because
it is implemented as a LinkedList and every look up of an entity will cost
O(n). This leads to another very important question. What should be the
maximum size of a Soft Cache Weak Identity Map so that it still performs

I wonder what would be the best solution in this case.

Back to the top