MemGuard
为长时间 Minecraft 会话提供轻量级内存行为优化。
MemGuard 是一款面向 Fabric / Forge / NeoForge(Minecraft 1.21.1)的轻量级实用模组,旨在减少可避免的对象分配,并使内存压力更易于观察。
MemGuard 的功能
1. 复用重复的短 NBT 字符串
- Hook:
NbtString.of(...)
- 通过 LRU 缓存(最多 2048 个条目)复用重复的短值。
- 这可以减少大型整合包中常见 ID/标签导致的重复对象分配。
2. 记录周期性堆快照
- 从服务器刻上下文采样堆使用情况。
- 当配置的使用阈值被超出时,会发出警告日志。
- 主要用于提升可见性和故障排查,而非直接的性能声明。
3. 追踪纹理图集生命周期(客户端)
- 在图集上传完成后,MemGuard 会在调试上下文中记录已使用的堆内存值。
- 这有助于将加载阶段与内存行为进行关联分析。
技术说明
- 使用 mixin hook 实现针对性行为(
NbtCacheMixin、ServerChunkManagerMixin / ChunkCacheMixin、TextureCacheMixin)。
- 无原生代码,无不安全内存访问。
- 不使用
System.gc() 强制垃圾回收。
重要准确性说明
- MemGuard 不保证固定的 FPS/MSPT/MB 提升。
- 实际影响取决于整合包大小、实体/世界状态、JVM 厂商/标志以及会话时长。
- 预期收益通常在具有重复 NBT 模式的中/大型整合包中更高。
可复现评估(推荐)
若要针对您的设置评估 MemGuard,请在相同条件下对比基线与启用 MemGuard 的情况:
- 相同的 Java 版本、JVM 参数、模组列表、世界种子和配置。
- 相同的运行窗口(建议每次运行 30-60 分钟)。
- 至少 3 次基线运行 + 3 次 MemGuard 运行。
- 收集可比指标:
- p50/p95 MSPT(使用
spark 或类似工具)
- 来自日志的堆使用趋势
- 分配/GC 事件数据(使用 JFR 或类似工具)
- 比较分布情况(中位数/p95),而非单次峰值。
安装
Fabric
- 安装 Fabric Loader (
0.16+)
- 安装 Fabric API
- 将 MemGuard jar 放入
mods/ 目录
Forge / NeoForge
- 安装与您 Minecraft 版本匹配的加载器
- 将 MemGuard jar 放入
mods/ 目录
兼容性
- 设计用于与常见的优化模组共存。
- 与任何基于 mixin 的模组一样,在生产部署前请测试您的确切整合包。
MemGuard
Lean memory behavior for long Minecraft sessions.
MemGuard is a lightweight utility mod for Fabric / Forge / NeoForge (Minecraft 1.21.1) focused on reducing avoidable allocations and making memory pressure easier to observe.
What MemGuard does
1. Reuses repeated short NBT strings
- Hook:
NbtString.of(...)
- Repeated short values are reused through an LRU cache (up to 2048 entries).
- This reduces duplicate object allocations for common IDs/tags in larger modpacks.
2. Logs periodic heap snapshots
- Heap usage is sampled from server tick context.
- Warning logs are emitted when configured usage thresholds are exceeded.
- Intended for visibility and troubleshooting, not as a direct performance claim.
3. Tracks texture-atlas lifecycle (client)
- On atlas upload completion, MemGuard logs heap-used values in debug context.
- This helps correlate load phases with memory behavior.
Technical notes
- Uses mixin hooks for targeted behavior (
NbtCacheMixin, ServerChunkManagerMixin / ChunkCacheMixin, TextureCacheMixin).
- No native code, no unsafe memory access.
System.gc() is not used to force garbage collection.
Important accuracy notes
- MemGuard does not guarantee fixed FPS/MSPT/MB improvements.
- Real impact depends on modpack size, entity/world state, JVM vendor/flags, and session length.
- Expected benefit is typically higher in medium/large packs with repeated NBT patterns.
Reproducible evaluation (recommended)
To evaluate MemGuard for your own setup, compare baseline vs. MemGuard under identical conditions:
- Same Java version, JVM args, mod list, world seed, and config.
- Same runtime window (recommended: 30-60 minutes per run).
- At least 3 baseline runs + 3 MemGuard runs.
- Collect comparable metrics:
- p50/p95 MSPT (
spark or similar)
- heap usage trend from logs
- allocation/GC event data (JFR or similar)
- Compare distributions (median/p95), not single spikes.
Installation
Fabric
- Install Fabric Loader (
0.16+)
- Install Fabric API
- Place MemGuard jar in
mods/
Forge / NeoForge
- Install the matching loader for your Minecraft version
- Place MemGuard jar in
mods/
Compatibility
- Designed to work alongside common optimization mods.
- As with any mixin-based mod, test your exact modpack before production deployment.
评论 0
登录后参与评分
还没有评论,来抢沙发吧