【发布时间】:2015-11-05 13:43:42
【问题描述】:
我在 Haskell 中编写了一个软实时应用程序,它处理模拟物理、碰撞检测以及所有这些好东西。在做所有这些时,我分配了大量内存,如果我愿意,我可能会优化我的内存使用,但由于我很好地坐在 40% 的 CPU 上,而且无论如何只使用了 1% 的 RAM,这似乎没有必要。不过,我看到的是,很多时候,当垃圾收集器启动时,会跳过帧。我已经通过使用threadscope 进行分析验证了这是问题的原因:在垃圾收集器执行其业务时,有时最多 0.05 秒内没有发生有用的计算,导致最多 3 个跳过帧,这非常明显并且很烦人。
现在,我尝试通过在每一帧手动调用 performMinorGC 来解决这个问题,这似乎缓解了这个问题,使它更加流畅,除了整体 CPU 使用率急剧上升到 70% 左右。显然我宁愿避免这种情况。
我尝试的另一件事是使用 -H64k 将 GC 的分配空间从 512k 减少到 64k,我还尝试设置 -I0.03 以尝试更频繁地收集它。这两个选项都改变了我在threadscope 中看到的垃圾收集模式,但它们仍然导致跳帧。
任何有 GC 优化经验的人都可以在这里帮助我吗?我是否注定要手动调用performMinorGC 并忍受由此带来的巨大性能损失?
编辑
我尝试在这些测试中运行它类似的时间,但由于它是实时的,因此没有“完成”的时间。
performMinorGC 每 4 帧的运行时统计信息:
9,776,109,768 bytes allocated in the heap
349,349,800 bytes copied during GC
53,547,152 bytes maximum residency (14 sample(s))
12,123,104 bytes maximum slop
105 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 15536 colls, 15536 par 3.033s 0.997s 0.0001s 0.0192s
Gen 1 14 colls, 13 par 0.207s 0.128s 0.0092s 0.0305s
Parallel GC work balance: 6.15% (serial 0%, perfect 100%)
TASKS: 20 (2 bound, 13 peak workers (18 total), using -N4)
SPARKS: 74772 (20785 converted, 0 overflowed, 0 dud, 38422 GC'd, 15565 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 9.773s ( 7.368s elapsed)
GC time 3.240s ( 1.126s elapsed)
EXIT time 0.003s ( 0.004s elapsed)
Total time 13.040s ( 8.499s elapsed)
Alloc rate 1,000,283,400 bytes per MUT second
Productivity 75.2% of total user, 115.3% of total elapsed
gc_alloc_block_sync: 29843
whitehole_spin: 0
gen[0].sync: 11
gen[1].sync: 71
没有performMinorGC
12,316,488,144 bytes allocated in the heap
447,495,936 bytes copied during GC
63,556,272 bytes maximum residency (15 sample(s))
15,418,296 bytes maximum slop
146 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 19292 colls, 19292 par 2.613s 0.950s 0.0000s 0.0161s
Gen 1 15 colls, 14 par 0.237s 0.165s 0.0110s 0.0499s
Parallel GC work balance: 2.67% (serial 0%, perfect 100%)
TASKS: 17 (2 bound, 13 peak workers (15 total), using -N4)
SPARKS: 100714 (29688 converted, 0 overflowed, 0 dud, 47577 GC'd, 23449 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 13.377s ( 9.917s elapsed)
GC time 2.850s ( 1.115s elapsed)
EXIT time 0.000s ( 0.006s elapsed)
Total time 16.247s ( 11.039s elapsed)
Alloc rate 920,744,995 bytes per MUT second
Productivity 82.5% of total user, 121.4% of total elapsed
gc_alloc_block_sync: 68533
whitehole_spin: 0
gen[0].sync: 9
gen[1].sync: 147
由于某种原因,现在没有performMinorGC 的整体生产力似乎比我昨天测试它时要低 - 之前它总是 >90%。
【问题讨论】:
-
请粘贴运行时统计信息 (
+RTS -s) -
幼稚的建议,但是如果您只是每隔 10 帧就调用
performMinorGC怎么办? -
你在分配什么?如果你可以避免分配,GC 就不是问题了。
-
@PaulJohnson 我正在执行碰撞检测比这更天真,将区域分成固定大小的网格,然后循环遍历每个网格单元中的所有内容。它是 2D,而不是 3D,所以这对我来说非常好。但是,是的,避免分配将非常困难,至少在不牺牲大量优雅的情况下。我试图让代码对初学者来说非常易读,所以我宁愿不放弃我的许多不错的抽象。
-
每秒分配 1GB?哇...模拟有多大?无论如何,有可能是某种懒惰/严格的问题导致你吃掉了不必要的内存。除此之外,我不知道该建议什么......
标签: haskell optimization garbage-collection