【Valgrind】How to check memory leak and where it's in 10 mins

1. Install

sudo apt-get install valgrind

2. If memory leak

example code:

/* memleak.c */
#include <stdlib.h>
void* memleak(int n)
{
    void *p = malloc(n);
    return p;
}

memleak.c

/* main.c */
#include <stdio.h>
#include <stdlib.h>
void* memleak(int n);
int main()
{
     int i=0;
     void *p = NULL;
     for(i=0; i<10; i++)
     {
     p = memleak(100);
         printf("allocate memory address: %p\n", p);
     }
     return 0;
}

main.c

Use ‘valgrind ./exe_binary [cmdline]‘ to check if memory leak happens:

[email protected]:~/test_memleak$ gcc main.c memleak.c -O1 -o main.x
[email protected]-VirtualBox:~/test_memleak$ valgrind ./main.x
==6726== Memcheck, a memory error detector
==6726== Copyright (C) 2002-2011, and GNU GPL‘d, by Julian Seward et al.
==6726== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6726== Command: ./main.x
==6726==
allocate memory address: 0x51f2040
allocate memory address: 0x51f20f0
allocate memory address: 0x51f21a0
allocate memory address: 0x51f2250
allocate memory address: 0x51f2300
allocate memory address: 0x51f23b0
allocate memory address: 0x51f2460
allocate memory address: 0x51f2510
allocate memory address: 0x51f25c0
allocate memory address: 0x51f2670
==6726==
==6726== HEAP SUMMARY:
==6726==     in use at exit: 1,000 bytes in 10 blocks
==6726==   total heap usage: 10 allocs, 0 frees, 1,000 bytes allocated
==6726==
==6726== LEAK SUMMARY:
==6726==    definitely lost: 1,000 bytes in 10 blocks
==6726==    indirectly lost: 0 bytes in 0 blocks
==6726==      possibly lost: 0 bytes in 0 blocks
==6726==    still reachable: 0 bytes in 0 blocks
==6726==         suppressed: 0 bytes in 0 blocks
==6726== Rerun with --leak-check=full to see details of leaked memory
==6726==
==6726== For counts of detected and suppressed errors, rerun with: -v
==6726== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

3. Where memory leak

$ gcc main.c memleak.c -O1 -g -o main.x

$ valgrind --leak-check=full ./main.x

ATTENTION:

  • "-O1" to prevent from inline functions by optimization, "-g" to get line number of memory leak functions.
  • In one word, use "DEBUG" flavour instead of "release" while finding memleak.
[email protected]:~/test_memleak$ valgrind --leak-check=full ./main.x
[email protected]-VirtualBox:~/test_memleak$ valgrind --leak-check=full ./x
==7209== Memcheck, a memory error detector
==7209== Copyright (C) 2002-2011, and GNU GPL‘d, by Julian Seward et al.
==7209== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7209== Command: ./x
==7209==
allocate memory address: 0x51f2040
allocate memory address: 0x51f20f0
allocate memory address: 0x51f21a0
allocate memory address: 0x51f2250
allocate memory address: 0x51f2300
allocate memory address: 0x51f23b0
allocate memory address: 0x51f2460
allocate memory address: 0x51f2510
allocate memory address: 0x51f25c0
allocate memory address: 0x51f2670
==7209==
==7209== HEAP SUMMARY:
==7209==     in use at exit: 1,000 bytes in 10 blocks
==7209==   total heap usage: 10 allocs, 0 frees, 1,000 bytes allocated
==7209==
==7209== 1,000 bytes in 10 blocks are definitely lost in loss record 1 of 1
==7209==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7209==    by 0x4005A3: memleak (memleak.c:5)
==7209==    by 0x400573: main (main.c:12)
==7209==
==7209== LEAK SUMMARY:
==7209==    definitely lost: 1,000 bytes in 10 blocks
==7209==    indirectly lost: 0 bytes in 0 blocks
==7209==      possibly lost: 0 bytes in 0 blocks
==7209==    still reachable: 0 bytes in 0 blocks
==7209==         suppressed: 0 bytes in 0 blocks
==7209==
==7209== For counts of detected and suppressed errors, rerun with: -v
==7209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
[email protected]-VirtualBox:~/test_memleak$
 

You can see the call stack, which means where memory leak happens

main() ==> memleak() ==> malloc()

4. Find memory leak because of invoking 3rd-party library

Use the same method as above.

【Valgrind】How to check memory leak and where it's in 10 mins,布布扣,bubuko.com

【Valgrind】How to check memory leak and where it's in 10 mins

时间: 2024-12-26 09:16:09

【Valgrind】How to check memory leak and where it's in 10 mins的相关文章

【Valgrind】How to check buffer overflow/underflow in 10 mins

Introduction Buffer overflow/underflow frequently happens when we did something wrong with the array index, no matter the array is heap or stack, no matter you are reading the memory or writing the memory. Example 1: heap overflow // head_overflow.c

【Valgrind】How to check if we reading uninitialized memory in 10 min

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(int argc, char** argv) 5 { 6 int i; 7 int a[10]; 8 for (i = 0; i < 9; i++) 9 a[i] = i; 10 11 for (i = 0; i < 10; i++){ 12 printf("%d ", a[i]); 13 } 14 printf("\n"

【转】Git如何Check Out出指定文件或者文件夹

[转]Git如何Check Out出指定文件或者文件夹http://www.handaoliang.com/a/20140506/195406.html 在进行项目开发的时候,有时候会有这样的需求那就是:我们只希望从Git仓库里取指定的文件或者文件夹出来.在SVN里面,这非常容易实现,因为SVN基于文件方式存储,而Git却是基于元数据方式分布式存储文件信息的,它会在每一次Clone的时候将所有信息都取回到本地,即相当于在你的机器上生成一个克隆版的版本库.因此在Git1.7.0以前,这无法实现,但

【hadoop】 running beyond virtual memory错误原因及解决办法

问题描述: 在hadoop中运行应用,出现了running beyond virtual memory错误.提示如下: Container [pid=28920,containerID=container_1389136889967_0001_01_000121] is running beyond virtual memory limits. Current usage: 1.2 GB of 1 GB physical memory used; 2.2 GB of 2.1 GB virtual

【ORACLE】ORA-27102: out of memory报错的处理

************************************************************************ ****原文:blog.csdn.net/clark_xu  徐长亮的专栏 ************************************************************************ 问题描写叙述: 原先SGA 4G.PGA 2G. alter system set sga_max_size=30G scope=s

【Oracle】Exadata Heathy Check工具Exachk的使用

在Exadata中,出现任何与数据库无关的问题的时候最好都运行exachk进行健康检查.exachk收集的信息很全,省去大量人工收集的繁琐步骤.并且收集完成以后,可以在整体上对系统的健康状况做一个评估,该报告包含软件.硬件.固件版本.配置等方面信息,从中发现一些可疑点,进而缩小范围进行下一步的诊断. 这篇文章主要记录了exachk的基本使用方法,exachk可以从MOS文档:1070954.1中下载得到. 首先要声明两个环境变量RAT_ORACLE_HOME和RAT_EXADATA_VERSIO

【SQLSERVER】How to check current pool size

SELECT des.program_name , des.login_name , des.host_name , COUNT(des.session_id) [Connections] FROM sys.dm_exec_sessions des INNER JOIN sys.dm_exec_connections DEC ON des.session_id = DEC.session_id WHERE des.is_user_process = 1 AND des.status != 'ru

【转】程序员写代码时应该反复问自己的10个问题

你想成为一名优秀的程序员吗? 那么,现在是时候放下<24小时学会xxx语言v8.3>超级骗子书,相反,你应当养成每天反问自己以下10个问题的习惯. 你的代码中是否有一种模式存在? 找寻模式中的可行与不可行将发现其中看似无关的想法或基本原则.要对工作达到深入的理解,你必须养成反问自己“是否有一种模式存在?”的习惯. 它不仅仅适用于你的代码.是否有适应各类型商业变化的模式吗?是否有一种适用于技术发展的模式?你有没有看到同类型的错误如雨后春笋般冒出来? 所谓理解就是要理解模式 — 以赛亚·伯林 怎么

【实战】静默安装-oracle 11.2.0.3 on centos 5.10

发现网上静默安装的文章很多,乱七八糟,五花八门!来个扫盲的!   centos 5.10 下安装oracle 11g_r2 ****************************************************************************** 1.系统部分 ****************************************************************************** ------1.安装系统 --applicat