CodeshipStephen Connolly2 min readintermediate
FileInputStream / FileOutputStream Considered Harmful
Summary
FileInputStream and FileOutputStream implement finalize, which forces objects onto the finalizer queue and can cause long GC pauses in programs that open many files. Switching to Files.newInputStream and Files.newOutputStream (Java 7+) eliminates the finalizer overhead and improves I/O‑heavy workloads.
- FileInputStream/FileOutputStream override finalize, so even after close they remain finalizable objects.
- Heavy file‑creation workloads (e.g., Hadoop) suffer noticeable GC pauses due to these finalizable streams.
- Java 7+ provides Files.newInputStream / Files.newOutputStream which return streams without a finalize method.
- Replacing the old constructors with the Files APIs is a one‑line change that reduces GC pressure.
Java developers who perform high‑volume file I/O should care because finalizer‑induced GC pauses can degrade performance.
6/10

