示例工程 23-gc-and-weak-tables

23-gc-and-weak-tables/02-weak-cache/README.md

markdown · 0.5 KB

# 02 · weak-cache
 
**演示**:`__mode = "v"`,对象离开强引用后,GC 清掉弱表条目。
**运行时**:`lua54`、`luajit`。
**运行**:
 
```sh
.tools/bin/lua54 scripts/run-example.lua examples/23-gc-and-weak-tables/02-weak-cache
.tools/bin/lua54 scripts/test-example.lua examples/23-gc-and-weak-tables/02-weak-cache
```
 
**预期输出**(lua 5.4.9 / macOS arm64):
 
```text
present=true
collected=true
```
 
**看点**:弱表适合缓存,不阻止对象回收。
**注意**:GC 时机不确定,测试要强制 collect 两轮。