Java 25 原生内存映射库
推荐指数 63.0 NO. 016 · 2026.05.12
发布2026/05/11Score108Comments25
为什么值得看
TypedMemory 用 Java FFM API 将 record 类型直接映射到堆外连续内存,消除手动 layout 和 offset 管理。做高性能计算、图形渲染、跨语言互操作的 Java 团队终于不用在 Unsafe 和繁琐的 MemorySegment 之间二选一了。
编辑判断
Java 做系统级编程的痛点一直是:用 Unsafe 性能够但代码像 C 指针噩梦,用 MemorySegment 安全但样板代码爆炸。TypedMemory 的聪明之处在于把 record 的紧凑布局语义和 FFM API 的内存安全绑定,相当于给 Java 补上了 C 语言 struct 的零成本抽象。
之前类似需求要么用 Panama 的 jextract 生成胶水代码(维护负担重),要么用 Chronicle Bytes 这类第三方库(额外依赖、不透明)。TypedMemory 纯标准库、无反射、layout 编译期确定,对需要直接操作 GPU buffer、文件 mmap 或 C 库互操作的场景是刚需。
如果你团队还在用 ByteBuffer.allocateDirect() 手写协议解析,或者因为 Panama 太繁琐而没迁移,这个库值得作为 Java 25 升级的核心动机之一。
社区反馈
意见分歧 21 条评论
核心争论:TypedMemory 能否在零分配场景下真正替代 Unsafe,还是仍有对象分配开销抵消性能优势
This is interesting. Java desperately needs an array of struct for type safe sugar over high performance arenas, but the areas you’d turn to this would be in a zero allocation effort where the cost of the this library’s off-heap and the object allocation in the getters and setters etc largely negat
Yup. Totally agree. Java does needs an array of structs. Hopefully value classes will help out through flattened array. But in future, one can use value records with this library with probable zero cost allocation. But the library doesn't use any reflection calls for get and set hence high performan
The thing I coded where I felt the weight of the GC the most was a chess engine in Java that needed transposition tables. Like using regular HashMap(s) or anything similar it was too slow to really speed up the engine. If my son had stayed interested in chess I would have coded up an off-heap tran