memory_detect - yuhannah/skills_map GitHub Wiki
Valgrind 内存检测
准备工作
1.valgrind (https://valgrind.org/)
2.rawlog, grid.gridmap, points.pointsmap
3.PC上的可执行文件test_rawlog, test_mclBestPose
只检测内存泄露,上述已足够。如果需要分析内存占用比例、接口调用热度,对于大型程序,需要KCachegrind可视化软件辅助。
4.Kubuntu 22.10(自带KCachegrind工具)
下载安装
在官网 (https://valgrind.org/)
下载最新源码:valgrind-3.20.0.tar.bz2
我安装了2次,第一次放在/opt目录,安装失败了,后来放在/home/tools目录下,安装成功了。可能是/opt目录需要权限。
安装命令:
tar -jxvf valgrind-3.12.0.tar.bz2
cd valgrind-3.12.0
./configure
make
sudo make install
仿真代码
为了排除其他因素干扰,对test_rawlog和test_mclBestPose做了以下修改:
1.删除画图相关代码,保留纯算法接口(initialize和processxx)
2.代码中屏蔽SSE并行加速,实际跑机用不上
3.CMakeLists添加add_compile_options(-std=c++11 -g -O0 -Wall),编译不优化
-g:调试信息
-O0:不优化
-Wall:全部警告
4.存储rawlog包,确认仿真代码可以正常使用
使用memcheck检测内存
1.按上述准备,编译仿真代码,生成可执行文件
2.检测内存泄露的命令 valgrind --tool=memcheck --leak-check=full ./test
3.测试结果
==17271== HEAP SUMMARY:
==17271== in use at exit: 72,832 bytes in 5 blocks
==17271== total heap usage: 3,627 allocs, 3,622 frees, 3,869,105 bytes allocated
==17271==
==17271== LEAK SUMMARY:
==17271== definitely lost: 0 bytes in 0 blocks
==17271== indirectly lost: 0 bytes in 0 blocks
==17271== possibly lost: 0 bytes in 0 blocks
==17271== still reachable: 72,832 bytes in 5 blocks
==17271== suppressed: 0 bytes in 0 blocks
==17271== Reachable blocks (those to which a pointer was found) are not shown.
==17271== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==17271==
==17271== For lists of detected and suppressed errors, rerun with: -s
==17271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
4.分析结果:
HEAP SUMMARY:统计了全部申请的内存,释放的内存量。
LEAK SUMMARY:统计了三种类型的内存泄露definitely, indirectly, possibly,详细代码指向要看给出的报告内容。slam模块和mcl模块都没有内存泄露,无法提供参考。
ERROR SUMMARY:统计了代码中的错误。
内存优化
使用memcheck的xtree分析内存占用分布--slam
1.按上述准备,编译仿真代码,生成可执行文件
2.分析内存占用的命令:valgrind --xtree-memory=full ./test
3.测试结果
(1).修改前:20帧数据xtree
==18477== xtree memory report: /home/tb/sources/eva/build_pc/apps/test_pc/xtmemory.kcg.18477
==18477== HEAP SUMMARY:
==18477== in use at exit: 72,832 bytes in 5 blocks
==18477== total heap usage: 5,823 allocs, 5,818 frees, 7,517,316 bytes allocated
==18477==
==18477== LEAK SUMMARY:
==18477== definitely lost: 0 bytes in 0 blocks
==18477== indirectly lost: 0 bytes in 0 blocks
==18477== possibly lost: 0 bytes in 0 blocks
==18477== still reachable: 72,832 bytes in 5 blocks
==18477== suppressed: 0 bytes in 0 blocks
==18477== Rerun with --leak-check=full to see details of leaked memory
==18477==
==18477== For lists of detected and suppressed errors, rerun with: -s
==18477== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
(2).修改后:20帧数据xtree
==25649== xtree memory report: /home/tb/sources/eva/build_pc/apps/test_pc/xtmemory.kcg.25649
==25649== HEAP SUMMARY:
==25649== in use at exit: 72,832 bytes in 5 blocks
==25649== total heap usage: 3,608 allocs, 3,603 frees, 3,826,308 bytes allocated
==25649==
==25649== LEAK SUMMARY:
==25649== definitely lost: 0 bytes in 0 blocks
==25649== indirectly lost: 0 bytes in 0 blocks
==25649== possibly lost: 0 bytes in 0 blocks
==25649== still reachable: 72,832 bytes in 5 blocks
==25649== suppressed: 0 bytes in 0 blocks
==25649== Rerun with --leak-check=full to see details of leaked memory
==25649==
==25649== For lists of detected and suppressed errors, rerun with: -s
==25649== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
结论:内存占用降低50%(7,517,316 bytes-->3,826,308 bytes)
4.分析结果
用KCachegrind可视软件打开xtmemory.kcg.xxx文件,View→Primary Event Type→total allocated Bytes,得到如下图表:
修改前:进入processActionObservation()后,内存分配最多的接口是CICP::Align(),里面内存分配最多的接口是determineMatching2D(),里面内存分配最多的地方在vector。
(图略)
修改后:占用的比例发生变化,CICP::Align()从55%->31%。有所改善。
(图略)
一般把占用内存最多的地方优化了,就能达到明显的效果。其他不太需要考虑。
文件生成了仿真代码的内存占用树状图,从图中能看到哪个接口占用内存比例较高,对占用比例较高的代码进行优化,能高效降低内存占用
使用memcheck的xtree分析内存占用分布—mcl
1.按上述准备,编译仿真代码,生成可执行文件
2.分析内存占用的命令:valgrind --xtree-memory=full ./test
3.测试结果
(1).修改前:5帧数据xtree
==18531==
==18531== xtree memory report: /home/tb/sources/eva/build_pc/apps/test_pc/xtmemory.kcg.18531
==18531== HEAP SUMMARY:
==18531== in use at exit: 72,832 bytes in 5 blocks
==18531== total heap usage: 7,384 allocs, 7,379 frees, 14,494,661 bytes allocated
==18531==
==18531== LEAK SUMMARY:
==18531== definitely lost: 0 bytes in 0 blocks
==18531== indirectly lost: 0 bytes in 0 blocks
==18531== possibly lost: 0 bytes in 0 blocks
==18531== still reachable: 72,832 bytes in 5 blocks
==18531== suppressed: 0 bytes in 0 blocks
==18531== Rerun with --leak-check=full to see details of leaked memory
==18531==
==18531== Use --track-origins=yes to see where uninitialised values come from
==18531== For lists of detected and suppressed errors, rerun with: -s
==18531== ERROR SUMMARY: 138 errors from 17 contexts (suppressed: 0 from 0)
(2).修改后:5帧数据xtree
==10032== xtree memory report: /home/tb/sources/eva/build_pc/apps/test_pc/xtmemory.kcg.10032
==10032== HEAP SUMMARY:
==10032== in use at exit: 72,832 bytes in 5 blocks
==10032== total heap usage: 3,028 allocs, 3,023 frees, 3,846,855 bytes allocated
==10032==
==10032== LEAK SUMMARY:
==10032== definitely lost: 0 bytes in 0 blocks
==10032== indirectly lost: 0 bytes in 0 blocks
==10032== possibly lost: 0 bytes in 0 blocks
==10032== still reachable: 72,832 bytes in 5 blocks
==10032== suppressed: 0 bytes in 0 blocks
==10032== Rerun with --leak-check=full to see details of leaked memory
==10032==
==10032== For lists of detected and suppressed errors, rerun with: -s
==10032== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
结论:内存占用降低75%(14,494,661 bytes-->3,846,855 bytes)。且修改前的代码存在ERROR,未初始化的变量,修改后ERROR为0.
用KCachegrind可视软件打开xtmemory.kcg.xxx文件,View→Primary Event Type→total allocated Bytes,得到如下图表:
修改前:进入test()后,内存分配最多的接口是CRelocalization()构造,里面分配了20000个pose。其余接口内存分配总占用58%。
(图略)
修改后:CRelocalization()构造占用内存比例增加,其余接口比例下降,优化在了其他接口中。实际上构造的20000个pose占用是不变的,没有修改这一部分。目前还没有好的方式对此进行优化。
(图略)