Bra*_*rdy 15 optimization garbage-collection haskell
我在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
Run Code Online (Sandbox Code Playgroud)
没有 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
Run Code Online (Sandbox Code Playgroud)
performMinorGC现在没有比我昨天因某种原因测试它的整体生产率低- 在它总是> 90%之前.
你们有很大的老一代。它大约有 100Mb 大。
默认情况下GHC,当堆大小达到上次主要 GC 后其大小的 2 倍时,将执行主要 GC。这意味着在某个时刻 GC 必须扫描并复制 50Mb 的数据。如果您的处理器有 10Gb 内存吞吐量限制,则加载和复制 50Mb 将至少花费 0.01 秒(与 gen1 平均和最大暂停相比。)
(我假设您检查了事件日志以确保主要 GC 在 0.05 秒暂停期间实际工作。因此,当 GC 等待其他线程而不是执行实际工作时,这不是线程同步的问题。)
因此,为了最大限度地减少 GC 暂停,您应该确保老年代很小。如果这 50Mb 的大部分是在最开始分配并一直存在到最后的静态数据(例如纹理或网格),那么您就会陷入困境。我知道的唯一解决方法是将数据打包到例如可存储向量中,并在需要时再次解包其中的一部分。
如果数据在执行期间分配并且生存时间有限(但足以生存几个主要代),那么请尝试重新考虑您的管道。通常没有数据可以保存一帧,所以你做错了什么。例如,您在不应该保留数据时保留了数据。
其他不好的迹象——gen0 最大暂停 0.02 秒。这很奇怪。默认情况下gen0分配区域是0.5Mb,所以gen0 GC应该很快。也许你有大量的记忆集。可能的原因:可变结构(IORef、可变 Vector 等)或大量惰性 thunk 更新。
还有一个小(可能不相关)问题:看起来您正在使用隐式并行性,但只有 1/3 的 Spark 被转换。您分配了太多的 spart,其中 1/2 被 GC 了。
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |