GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — File memory mapping | java.evolved

Home / I/O / File memory mapping
I/O Advanced

File memory mapping

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Map files larger than 2GB with deterministic cleanup using MemorySegment.

I/O Foreign Function & Memory API Performance
Code Comparison
✕ Java 8
try (FileChannel channel =
    FileChannel.open(path,
        StandardOpenOption.READ,
        StandardOpenOption.WRITE)) {
    MappedByteBuffer buffer =
        channel.map(
            FileChannel.MapMode.READ_WRITE,
            0, (int) channel.size());
    // Limited to 2GB
    // Freed by GC, no control
}
✓ Java 22+
FileChannel channel =
    FileChannel.open(path,
        StandardOpenOption.READ,
        StandardOpenOption.WRITE);
try (Arena arena = Arena.ofShared()) {
    MemorySegment segment =
        channel.map(
            FileChannel.MapMode.READ_WRITE,
            0, channel.size(), arena);
    // No size limit
    // ...
} // Deterministic cleanup
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
📏

No size limit

Map files larger than 2GB without workarounds.

🔒

Deterministic cleanup

Arena ensures memory is freed at scope exit, not GC time.

⚡

Better performance

Aligned with modern memory models and hardware.

Old Approach
MappedByteBuffer
Modern Approach
MemorySegment with Arena
Since JDK
22
Difficulty
Advanced
JDK Support
File memory mapping
Available

Available since JDK 22 (March 2024)

How it works

The Foreign Function & Memory API (JEP 454) introduces MemorySegment for safe and efficient memory access. Unlike MappedByteBuffer, MemorySegment supports files larger than 2GB (Integer.MAX_VALUE), provides deterministic cleanup via Arena, and offers better performance with modern hardware.

Related Documentation
MemorySegment (Java 22) ↗ Arena (Java 22) ↗ JEP 454: Foreign Function & Memory API ↗
Proof
View proof source ↗
Related patterns
I/O Beginner

Modern HTTP client

Java 8
URL url = new URL("https://api.com/data");
HttpURLConnection con =
    (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
// read lines, close streams...
Java 11+
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.com/data"))
    .build();
var response = client.send(
    request, BodyHandlers.ofString());
String body = response.body();
Hover to see modern ➜
JDK 11+ →
I/O Beginner

Reading files

Java 8
StringBuilder sb = new StringBuilder();
try (BufferedReader br =
    new BufferedReader(
        new FileReader("data.txt"))) {
    String line;
    while ((line = br.readLine()) != null)
        sb.append(line).append("\n");
}
String content = sb.toString();
Java 11+
String content =
    Files.readString(Path.of("data.txt"));
Hover to see modern ➜
JDK 11+ →
I/O Beginner

Writing files

Java 8
try (FileWriter fw =
        new FileWriter("out.txt");
    BufferedWriter bw =
        new BufferedWriter(fw)) {
    bw.write(content);
}
Java 11+
Files.writeString(
    Path.of("out.txt"),
    content
);
Hover to see modern ➜
JDK 11+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.