For a long time I have been wondering how do frameworks manage memory while caching.

The EclipseLink documentation gave me the answer. The Java SE platform provide special references :

According to the official documentation, soft references are used to provide memory-sensitive properties to cached references. This can be very efficient.

But access to soft-referenced objects needs to be done via specific objects so that freed references can be renewed (e.g. via a persistent storage).

The official API also provides a special map with weak referenced keys. This class can be useful but is not really appropriate for caching as keys are released.