I/O متقدم

عيّن ملفات أكبر من 2 جيجابايت مع تنظيف حتمي باستخدام MemorySegment.

✕ 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
هل ترى مشكلة في هذا الكود؟ أخبرنا.
📏

لا حد للحجم

عيّن ملفات أكبر من 2 جيجابايت دون حلول بديلة.

🔒

تنظيف حتمي

Arena تضمن تحرير الذاكرة عند الخروج من النطاق لا وقت GC.

أداء أفضل

متوافق مع نماذج الذاكرة والأجهزة الحديثة.

الأسلوب القديم
MappedByteBuffer
الأسلوب الحديث
MemorySegment مع Arena
منذ JDK
22
الصعوبة
متقدم
تعيين الملفات في الذاكرة
متاح

متاح منذ JDK 22 (مارس 2024)

يقدّم Foreign Function & Memory API (JEP 454) MemorySegment للوصول الآمن والفعال للذاكرة. بخلاف MappedByteBuffer يدعم MemorySegment ملفات أكبر من 2 جيجابايت (Integer.MAX_VALUE) ويوفر تنظيفاً حتمياً عبر Arena وأداءً أفضل مع الأجهزة الحديثة.

مشاركة 𝕏 🦋 in