File memory mapping
Map files larger than 2GB with deterministic cleanup using MemorySegment.
Porównanie kodu
✕ 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
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
MappedByteBuffer
Nowoczesne podejście
MemorySegment with Arena
Od JDK
22
Poziom trudności
Zaawansowany
Wsparcie JDK
File memory mapping
Dostępne
Available since JDK 22 (March 2024)
Jak to działa
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.
Powiązana dokumentacja