I use a large (millions) entries hashmap to cache values needed by an algorithm, the key is a combination of two objects as a long. Since it grows continuously (because keys in
Instead of using a HashMap
or other map implementation as a cache you could try to use a framework specialized in caching. A well known caching framework for Java is Ehcache.
Caching frameworks let you usually configure expiration policies based on time (e.g. time to live, time to idle) or usage (e.g. least frequently used, least recently used), some may even allow you to specify a maximum amount of memory usage.