Course contentsShow
← Java
Lesson 1953 of 2,87039. File I/O: Basic Byte StreamsFree lesson

System Call Overhead and User vs Kernel Space

Understand that each I/O operation crosses the user-kernel boundary, making frequent small operations expensive.

System Call Overhead and User vs Kernel Space

What you'll learn: Why crossing the boundary between your Java program and the operating system makes frequent small I/O operations expensive.

The Two Worlds of Your Program

When your Java application runs, it operates in user space—a protected area of memory where normal programs execute. However, actual file I/O operations (reading from disk, writing to files) can only be performed by the operating system kernel, which runs in privileged kernel space.

Every time you call a method like FileInputStream.read() or FileOutputStream.write(), your program must:

  1. Switch from user space to kernel space (a context switch)
  2. Execute the I/O operation in the kernel
  3. Switch back to user space with the result

Why This Matters

This boundary crossing is called a system call, and it's surprisingly expensive—taking hundreds to thousands of CPU cycles. Think of it like needing a security escort every time you want to fetch a single sheet of paper from a locked vault. The escort process itself takes far longer than grabbing the paper.

When you read one byte at a time with unbuffered streams, you're making a system call for every single byte. Reading a 100KB file byte-by-byte means 100,000 system calls—each with its overhead!

Buffering to the Rescue

This is precisely why BufferedInputStream and BufferedOutputStream are so critical. They batch many small operations in user space memory first, then make one system call to transfer a large chunk (typically 8KB). Instead of 100,000 system calls, you might need only 13.

Key Takeaway: Every I/O operation crosses the expensive user-kernel boundary; buffering minimizes costly system calls by batching operations in user space before interacting with the kernel.