bufferedreader: read a file line by line module (BufferedReader vs Scanner vs Files.lines vs readAllLines on a 1 GB file; heap at -Xmx64m, timings, read-call counts, one-huge-line OOM, charset/BOM/line-terminator pitfalls, Scanner swallowing IOException), 10 contract tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3pDoGLiWk6LLBeHjW
This commit is contained in:
Claude
2026-11-15 01:20:37 +05:30
parent 5faa57bb2d
commit f8c9e244a3
25 changed files with 809 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
$ java -Xmx64m LineCount reader big.log
reader lines=9,623,347 chars=990,376,717 time=1,288 ms gc=37 collections / 31 ms (-Xmx64 MB)
$ java -Xmx64m LineCount scanner big.log
scanner lines=9,623,347 chars=990,376,717 time=6,233 ms gc=140 collections / 116 ms (-Xmx64 MB)
$ java -Xmx64m LineCount lines big.log
lines lines=9,623,347 chars=990,376,717 time=1,179 ms gc=37 collections / 32 ms (-Xmx64 MB)
$ java -Xmx64m LineCount readAllLines big.log
readAllLines OutOfMemoryError: Java heap space (-Xmx64 MB)
How much heap does readAllLines need for this file?
$ java -Xmx1g LineCount readAllLines big.log
readAllLines OutOfMemoryError: Java heap space (-Xmx1024 MB)
$ java -Xmx2g LineCount readAllLines big.log
readAllLines lines=9,623,347 chars=990,376,717 time=2,872 ms gc=24 collections / 922 ms (-Xmx2048 MB)
$ java -Xmx3g LineCount readAllLines big.log
readAllLines lines=9,623,347 chars=990,376,717 time=2,729 ms gc=23 collections / 949 ms (-Xmx3072 MB)
$ java -Xmx4g LineCount readAllLines big.log
readAllLines lines=9,623,347 chars=990,376,717 time=2,862 ms gc=23 collections / 905 ms (-Xmx4096 MB)
And the streaming reader at a tiny heap:
$ java -Xmx16m LineCount reader big.log
reader lines=9,623,347 chars=990,376,717 time=1,347 ms gc=171 collections / 101 ms (-Xmx16 MB)