Efficiently export millions of records using Apache POI's streaming capabilities.
Generating Excel files with millions of records (from a database, etc.) causes the JVM to run out of memory.
The Problem: java.lang.OutOfMemoryError: Java heap space
Apache POI provides the streaming API, **SXSSFWorkbook**, which is designed specifically for large Excel files.
The crucial step is setting the **Random Access Window Size** on the sheet.
SXSSFWorkbook sxssWb = new SXSSFWorkbook(100);
sxssSheet.setRandomAccessWindowSize(100);
This flushes every 100 rows to disk, reducing heap usage.
Use **VisualVM** to get real-time insights into memory usage and JVM performance.
Ensure you include `poi`, `poi-ooxml`, `poi-ooxml-schemas`, and `xmlbeans` in your project dependencies.