This is yet another \"please tell me how to force the Java garbage collector to run\" question. In our application, I believe we have good reasons for doing this.
This i
Consider using non-managed memory, i.e., ByteBuffer
s in place of the byte arrays.
I can only offer a hack which will need some tuning and then might or might not work. I'd first try the more sane solutions. When you want to force the GC, do it by allocating a lot of memory. Do this so that the memory can be immediately reclaimed, but so that the whole allocation can't be optimized away (something like sum += new byte[123456].hashCode()
should do). You'll need to find a reliable method for determining when to stop. An object with a finalizer might tell you or maybe watching runtime.getFreeMemory
could help.