Hi,
I've been using Hibernate for 2,5 months (currently using 2.1beta6) and when i was making carge tests to my application database, i got several memory problems. I run some memory profiles programs and I checked that The problem is related with Hibernate cache.
As long as I understood, Hibernate has 3 cache levels (correct me if i'm wrong):
- JCS cache, which is shared by all hibernate sessions;
- Session cache;
- Query cache.
JCS cache is optional, and is not causing any problems.
The problem is releated with session and query caches. When I load an object I do the following code:
private Object obj;
obj = (Object) session.load(Object.class, 1);
The loaded object will be duplicated in memory: in variable obj, and in Hibernate internal session cache. If insted of 1 object, I loaded a large number of objects with a large number of associations each one, I will run out of memory.
As a solution, I try to evict the object after loading it, but this as two problems:
- Evict doesn't work beacuse the memory never decreases;
- If it worked, it still would be a bad solution, because I was loading an object in cache to download it later. It's an waste of processor resources.
If I load an object by query, it's worst, beacuse that object and a few others will be loaded in query caches, and can not be evicted. Session.clear() doesn't work too, beacuse the memory never decreases.
In the worst case, if I load an object individualy, and next, load it within a collection, I will have the object replicated 3 times in memory: my java variable, hibernate session cache and hibernate query cache.
Other thing I don't understand is the diference between queries and criteria queries, in the cache context. Is there any diference between them? Or they both use cache in the same way?
I would like to NOT use any session and/or query cache. Is it possible? Hibernate cache it doesn't improve performance on my application, and it's giving me a lot of problems.
I read several posts about cache system and I saw that there are another users with the same problems, but the questions were spread by many posts and the answers were incomplete. I tryed to join everything in a same (long) post, and I would like to ask for hibernate team assistence.
Thanks in advance,
Joao Rangel
|