Hibernate's session cache requires manual action (i.e., evict() and/or clear()) to prevent it from growing without bound when iterating over a large set of entities. This was surprising to me the first time I encountered it, because most people's concept of a "cache" includes the idea of transparency, i.e., the cache is not apparent except in increased performance. Hibernate's cache, even within a read-only transaction, effectively has a built-in memory leak.
But anyway, this problem can be fixed easily. Suggest adding a new feature which would (optionally) allow the session cache to be configured to automatically evict unmodified objects that were no longer referenced.
The session cache would change to use two kinds of references to objects in the cache: normal strong references for objects that are new or modified in any way, and weak references for objects that have not been modified. Hibernate would have to switch a weakly referenced object into a strongly referenced one once it was modified.
This way, unmodified, weakly referenced objects would automatically fall out of the cache when they were no longer referenced by the application, and the "iterate over a large dataset" memory leak would be fixed.
Its more than just a "cache", in fact its not even called a cache, its called a persistence context as in a org.hibernate.engine.PersistenceContext.
Jeez. Why did you close this issue so fast? I think this is a perfectly valid feature request.
So forget I ever said the word "cache". I'm simply asking for a feature that will help people avoid memory leaks in their applications.
If you are going to reject this issue, please give a better reason.