Im primarily using Hibernate to reduce the memory consumption of my application because I can store data in the database instead of in memory. Essentially there is one main entity Song that represents a Music File loaded from a directory, but the first thing I do after adding a song to the database (using hibernate) is add it to a pipeline of executors for further processing, so that instances of object representing each song are still in memory and remain so until the song has been processed so Im not saving much memory.
My first thought was for me to just store the ids of the songs when I add them to the pipleline and only get the actual entity when it is required to be processed but this seems to be working against rather than with Hibernate.
Instead I wondered if I used Ehcache with Hibernate configured to use a Disk Cache would that mean the instances Im using wouldn’t be using much memory or would they use as much memory as before once I’d actually had a reference to them in my application.
If you have a reference to an entity, then obviously the object is in memory (unless the reference is a reference to a lazy-loaded proxy).
EHCache is used to avoid going to the database too often, and get the entities from the in-memory or on-disk cache. Using it will increase the memory, not decrease it.