I/O 고급

MemorySegment를 사용하여 결정적 정리로 2GB보다 큰 파일을 매핑합니다.

✕ 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
이 코드에 문제가 있나요? 알려주세요.
📏

크기 제한 없음

우회 방법 없이 2GB보다 큰 파일을 매핑합니다.

🔒

결정적 정리

Arena는 GC 시점이 아닌 스코프 종료 시점에 메모리를 해제합니다.

더 나은 성능

현대 메모리 모델 및 하드웨어와 정렬됩니다.

이전 방식
MappedByteBuffer
모던 방식
Arena를 사용한 MemorySegment
JDK 버전
22
난이도
고급
파일 메모리 매핑
사용 가능

JDK 22 (2024년 3월) 이후 사용 가능

Foreign Function & Memory API(JEP 454)는 안전하고 효율적인 메모리 접근을 위한 MemorySegment를 도입합니다. MappedByteBuffer와 달리 MemorySegment는 2GB(Integer.MAX_VALUE) 이상의 파일을 지원하고, Arena를 통한 결정적 정리를 제공하며, 현대 하드웨어에서 더 나은 성능을 발휘합니다.

공유 𝕏 🦋 in