I’m writing an app the generates a big xlsx file using apache-POI.
At a certain time I get OutOfHeapSpace exception.
I want to solve it by writing the content to the xlsx file when I determine that I’ll soon be out of space, and thus freeing the memory, and then reading the file from the disk and continuing the writing.
A better solution might be to predefine the number of cells I’ll write in each “block” , and then write the block to disk, but in any case this made me wonder if there is a way to determine the heap space that is left at runtime?
Freeing unused memory at runtime is probably not very reliable since the JVM has considerable freedom in deciding when to garbage collect.
POI recently introduced the SXSSF API that uses streaming and thereby significantly reduces memory footprint for writing spreadsheets. This should help even with very big xsls files. There are a couple of downsides which are shown here. But if you can live with them, this should alleviate you of heap related problems.