I can think of several options:
- Finding out how much memory your method requires via a microbenchmark (i.e. jmh).
- Building allocation strategies based on heuristic estimation. There are several open source solutions implementing class size estimation i.e. ClassSize. A much easier way could be utilizing a cache which frees rarely used objects (i.e. Guava’s Cache). As mentioned by @EnnoShioji, Guava’s cache has memory-based eviction policies.
You can also write your own benchmark test which counts memory. The idea is to
- Have a single thread running.
- Create a new array to store your objects to allocate. So these objects won’t be collected during GC run.
System.gc(),memoryBefore = runtime.totalMemory() - runtime.freeMemory()- Allocate your objects. Put them into the array.
System.gc(),memoryAfter = runtime.totalMemory() - runtime.freeMemory()
This is a technique I used in my lightweight micro-benchmark tool which is capable of measuring memory allocation with byte-precision.