valgrind介紹參考文章:
https://blog.csdn.net/sduliulun/article/details/7732906
使用valgrind工具進行記憶體檢測,執行如下指令:
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all -v ./tlx_process
遇到的記憶體洩漏問題以及解決方案:
1.Linux下正确使用getifaddrs()函數避免記憶體洩露
https://blog.csdn.net/tao_627/article/details/45062781
2.遇到多個可使目前函數退出的情況,在退出前,要釋放已申請的資源,可以考慮使用goto語句來整理代碼結構
此狀況時調用cJSON庫時,對解析出來的資料,進行多個函數傳參的情況下發生
https://blog.csdn.net/shimazhuge/article/details/8448773
https://www.cnblogs.com/heartchord/p/4795337.html
3.對于建立線程,定時器的記憶體洩漏,可以根據是否頻繁的操作而選擇是否進行修改
https://www.xuebuyuan.com/1568906.html
線程頻繁建立,未銷毀,導緻洩漏,需要主動銷毀不使用的線程
https://www.cnblogs.com/lidabo/archive/2012/08/15/2640204.html
https://blog.csdn.net/inuyashaw/article/details/53465294
https://www.cnblogs.com/fushou/p/7269564.html
4.valgrind提示有洩漏,但是實際情況卻沒有的狀況
==27558== 88 bytes in 1 blocks are still reachable in loss record 4 of 15
==27558== at 0x4C29BC3: malloc (vg_replace_malloc.c:299)
==27558== by 0x5357F8A: [email protected]@GLIBC_2.3.3 (in /usr/lib64/librt-2.17.so)
==27558== by 0x4077D6: set_timer_up (client_timer.c:28)
==27558== by 0x40A384: pthread_httpd_server (httpd.c:563)
==27558== by 0x513FE24: start_thread (in /usr/lib64/libpthread-2.17.so)
==27558== by 0x565ABAC: clone (in /usr/lib64/libc-2.17.so)
==27558==
經測試,timer_create 沒有記憶體洩漏,但是打開太多,會造成線程建立失敗,達到可建立句柄的最大值,隻在普通使用者下會有此狀況,是以,建立定時器後,要記得關閉
參考:https://www.ibm.com/developerworks/cn/linux/l-cn-timers/
https://blog.csdn.net/leo9150285/article/details/8271910
==27558== 116 bytes in 1 blocks are definitely lost in loss record 5 of 15
==27558== at 0x4C29BC3: malloc (vg_replace_malloc.c:299)
==27558== by 0x55E6C90: __libc_alloc_buffer_allocate (in /usr/lib64/libc-2.17.so)
==27558== by 0x566D11E: __resolv_conf_allocate (in /usr/lib64/libc-2.17.so)
==27558== by 0x566B0F1: __resolv_conf_load (in /usr/lib64/libc-2.17.so)
==27558== by 0x566C9D7: __resolv_conf_get_current (in /usr/lib64/libc-2.17.so)
==27558== by 0x566B3E1: __res_vinit (in /usr/lib64/libc-2.17.so)
==27558== by 0x566C4FA: maybe_init (in /usr/lib64/libc-2.17.so)
==27558== by 0x566C66D: __resolv_context_get (in /usr/lib64/libc-2.17.so)
==27558== by 0x56753B9: gethostbyname (in /usr/lib64/libc-2.17.so)
==27558== by 0x407FA8: http_tcpclient_create (http.c:22)
==27558== by 0x40850A: http_post (http.c:165)
==27558== by 0x40E8BD: get_http_heart (init_tlx_process.c:1349)
經測試,gethostbyname 沒有記憶體洩漏
參考:http://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html