内存泄漏动态检测(valgrind)

news/2024/10/26 11:21:58/

初步判断是否有泄漏

Linux 内存性能优化 —— 高内存使用及内存泄漏排查
比如该文的介绍,需要初步了解top free -h等命令;
主要看free
在这里插入图片描述
在这里插入图片描述

内存泄漏检测方法:

静态我常用的是cppcheck;

动态的 Linux下内存泄漏定位方法

这个文章介绍的就不错
如:valgrind

按照该教程,大概复现了一下:

valgrind

比如下面这段代码;

1.需要添加头文件#include <mcheck.h>
2.怀疑泄漏的地方,两端成对使用
mtrace();
//怀疑出问题的代码
//int * hhh = new int; * hhh =1;  --加了一个new,未 delete
//pthread_join(ppid[i], 0);  --屏蔽了线程的资源阻塞回收
muntrace();
3.编译;
4.运行
valgrind --log-file='./log1' --leak-check=full --track-fds=yes ./test

前提你的系统支持valgrind,可用 valgrind --version查看是否支持
其中 选项有 输出日志目录;检测选项 ;运行程序等

// filename: test.c
#define _GNU_SOURCE
#include <unistd.h>  
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <mcheck.h>
// 用来打印当前的线程信息:调度策略是什么?优先级是多少?
void get_thread_info(const int thread_index)
{int policy;struct sched_param param;printf("\n====> thread_index = %d \n", thread_index);pthread_getschedparam(pthread_self(), &policy, &param);if (SCHED_OTHER == policy)printf("thread_index %d: SCHED_OTHER \n", thread_index);else if (SCHED_FIFO == policy)printf("thread_index %d: SCHED_FIFO \n", thread_index);else if (SCHED_RR == policy)printf("thread_index %d: SCHED_RR \n", thread_index);printf("thread_index %d: priority = %d \n", thread_index, param.sched_priority);
}// 线程函数,
void *thread_routine(void *args)
{// 参数是:线程索引号。四个线程,索引号从 1 到 4,打印信息中使用。int thread_index = *(int *)args;// 为了确保所有的线程都创建完毕,让线程睡眠1秒。sleep(1);// 打印一下线程相关信息:调度策略、优先级。get_thread_info(thread_index);long num = 0;for (int i = 0; i < 2; i++){for (int j = 0; j < 5000000; j++){// 没什么意义,纯粹是模拟 CPU 密集计算。float f1 = ((i+1) * 345.45) * 12.3 * 45.6 / 78.9 / ((j+1) * 4567.89);float f2 = (i+1) * 12.3 * 45.6 / 78.9 * (j+1);float f3 = f1 / f2;}usleep(1000);// 打印计数信息,为了能看到某个线程正在执行printf("thread_index %d: num = %ld \n", thread_index, num++);}// 线程执行结束printf("thread_index %d: exit \n", thread_index);return 0;
}int main(void)
{mtrace();//int * hhh = new int; * hhh =1;// 一共创建四个线程:0和1-实时线程,2和3-普通线程(非实时)int thread_num = 4;// 分配的线程索引号,会传递给线程参数int index[4] = {1, 2, 3, 4};// 用来保存 4 个线程的 id 号pthread_t ppid[4];// 用来设置 2 个实时线程的属性:调度策略和优先级pthread_attr_t attr[2];struct sched_param param[2];// 实时线程,必须由 root 用户才能创建if (0 != getuid()){printf("Please run as root \n");//exit(0);}// 创建 4 个线程for (int i = 0; i < thread_num; i++){cpu_set_t mask;int cpus = sysconf(_SC_NPROCESSORS_CONF);CPU_ZERO(&mask);CPU_SET(0, &mask);if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0){printf("set thread affinity failed! \n");}	if (i <= 1)    // 前2个创建实时线程{// 初始化线程属性pthread_attr_init(&attr[i]);// 设置调度策略为:SCHED_FIFO SCHED_RRint res = pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);if(res != 0) printf("i =1 or 2 \n");// 设置优先级为 51,52。param[i].__sched_priority = 51 + i;res = pthread_attr_setschedparam(&attr[i], &param[i]);if(res != 0) printf("i =1 or 2 \n");// 设置线程属性:不要继承 main 线程的调度策略和优先级。pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);// 创建线程pthread_create(&ppid[i], &attr[i],thread_routine, (void *)&index[i]);}else        // 后两个创建普通线程{pthread_create(&ppid[i], 0, thread_routine, (void *)&index[i]);}}//pthread_join(ppid[0], 0);
//pthread_join(ppid[1], 0);
//pthread_join(ppid[2], 0);
//pthread_join(ppid[3], 0);// 等待 4 个线程执行结束//for (int i = 0; i < 2; i++)//pthread_join(ppid[i], 0);for (int i = 0; i < 2; i++)//pthread_attr_destroy(&attr[i]);muntrace();
}

日志

hann@hann-virtual-machine:~/linux_app/pthread$ cat log1
==8015== Memcheck, a memory error detector
==8015== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8015== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8015== Command: ./test
==8015== Parent PID: 7229
==8015== 
==8015== 
==8015== FILE DESCRIPTORS: 4 open at exit.
==8015== Open file descriptor 3: /home/hann/linux_app/pthread/log1
==8015==    <inherited from parent>
==8015== 
==8015== Open file descriptor 2: /dev/pts/1
==8015==    <inherited from parent>
==8015== 
==8015== Open file descriptor 1: /dev/pts/1
==8015==    <inherited from parent>
==8015== 
==8015== Open file descriptor 0: /dev/pts/1
==8015==    <inherited from parent>
==8015== 
==8015== 
==8015== HEAP SUMMARY:
==8015==     in use at exit: 1,156 bytes in 5 blocks
==8015==   total heap usage: 10 allocs, 5 frees, 205,124 bytes allocated
==8015== 
==8015== 4 bytes in 1 blocks are definitely lost in loss record 1 of 3
==8015==    at 0x4C3217F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8015==    by 0x108EA3: main (in /home/hann/linux_app/pthread/test)
==8015== 
==8015== 576 bytes in 2 blocks are possibly lost in loss record 2 of 3
==8015==    at 0x4C33B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8015==    by 0x4013646: allocate_dtv (dl-tls.c:286)
==8015==    by 0x4013646: _dl_allocate_tls (dl-tls.c:530)
==8015==    by 0x4E46227: allocate_stack (allocatestack.c:627)
==8015==    by 0x4E46227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==8015==    by 0x109168: main (in /home/hann/linux_app/pthread/test)
==8015== 
==8015== 576 bytes in 2 blocks are possibly lost in loss record 3 of 3
==8015==    at 0x4C33B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8015==    by 0x4013646: allocate_dtv (dl-tls.c:286)
==8015==    by 0x4013646: _dl_allocate_tls (dl-tls.c:530)
==8015==    by 0x4E46227: allocate_stack (allocatestack.c:627)
==8015==    by 0x4E46227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==8015==    by 0x1091AF: main (in /home/hann/linux_app/pthread/test)
==8015== 
==8015== LEAK SUMMARY:
==8015==    definitely lost: 4 bytes in 1 blocks
==8015==    indirectly lost: 0 bytes in 0 blocks
==8015==      possibly lost: 1,152 bytes in 4 blocks
==8015==    still reachable: 0 bytes in 0 blocks
==8015==         suppressed: 0 bytes in 0 blocks
==8015== 
==8015== For counts of detected and suppressed errors, rerun with: -v
==8015== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

其中有问题的点:
1.8015 4 bytes in 1 blocks are definitely lost in loss record 1 of 3
8015 at 0x4C3217F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
8015 by 0x108EA3: main (in /home/hann/linux_app/pthread/test)

2.8015 by 0x4E46227: allocate_stack (allocatestack.c:627)
8015 by 0x4E46227: pthread_create@@GLIBC_2.2.5


http://www.ppmy.cn/news/50789.html

相关文章

php+vue+mysql医院医护人员医生排班系统

本医护人员排班系统管理员&#xff0c;医护。管理员功能有个人中心&#xff0c;医院信息管理&#xff0c;医护信息管理&#xff0c;医护类型管理&#xff0c;排班信息管理&#xff0c;排班类型管理&#xff0c;科室信息管理&#xff0c;投诉信息管理。医护人员可以修改自己的个…

数据湖Iceberg-FlinkSQL-kafka类型表数据无法成功写入(6)

数据湖Iceberg-简介(1) 数据湖Iceberg-存储结构(2) 数据湖Iceberg-Hive集成Iceberg(3) 数据湖Iceberg-SparkSQL集成(4) 数据湖Iceberg-FlinkSQL集成(5) 数据湖Iceberg-FlinkSQL-kafka类型表数据无法成功写入(6) 数据湖Iceberg-Flink DataFrame集成(7) 数据湖Iceberg-FlinkSQL-…

数字中国建设2522整体框架

作为影响中国未来发展的重磅文件&#xff0c;《数字中国建设整体布局规划》明确了两个重要时间节点&#xff1a; 到 2025 年&#xff0c;基本形成横向打通、纵向贯通、协调有力的一体化推进格局&#xff0c;数字中国建设取得重要进展&#xff1b; 到 2035 年&#xff0c;数字化…

中医诊所一定要去尝试软文营销,效果简直不要太好

中医诊所是一种传统的医疗机构&#xff0c;随着互联网时代的发展&#xff0c;软文营销已经成为了中医诊所宣传推广的一种重要方式。通过撰写高质量的软文&#xff0c;中医诊所可以提升品牌知名度、增加患者数量、提高医疗服务质量等方面取得良好的效果。今天结合我10年营销经验…

国产数字温度传感芯片M117 Pin to Pin替代PT100和PT1000

高精度数字温度传感芯片 - M117&#xff0c;可Pin to Pin替代PT100/PT1000&#xff0c;且具功能差异化优势&#xff0c;支持行业应用的定制化需求。高测温精度0.1℃&#xff0c;用户无需进行校准。芯片感温原理基于CMOS半导体PN节温度与带隙电压的特性关系&#xff0c;经过小信…

【历史上的今天】3 月 24 日:苹果推出 Mac OS X;微软前任 CEO 出生;Spring 1.0 正式发布

整理 | 王启隆 透过「历史上的今天」&#xff0c;从过去看未来&#xff0c;从现在亦可以改变未来。 今天是 2023 年 3 月 24 日&#xff0c;在 2016 年的今天&#xff0c;暴雪娱乐公司发布了第一人称射击多人游戏《守望先锋》。根据评分汇总网站 Metacritic 的统计&#xff0c…

设计模式 - 责任链模式

设计模式 - 责任链模式 1、责任链模式的应用1.1、啥是责任链模式1.2、责任链模式的优缺点 2、责任链模式小试牛刀2.1、实现场景描述2.2、常规实现2.3、责任链模式实现2.3.1、请求方2.3.2、处理方2.3.3、业务方法2.3.4、执行结果 3、总结 1、责任链模式的应用 1.1、啥是责任链模…

RocketMQ的学习历程(二)----MQ基本构架

文章目录 1.MQ的基本要素1.1.消息&#xff08;Message&#xff09;1.2.主题&#xff08;Topic&#xff09;1.3.标签&#xff08;Tag&#xff09;1.4.队列&#xff08;MessageQueue&#xff09;1.5.消息标识&#xff08;MessageId&#xff09; 2.MQ中的主要角色和相关联系2.1.Pr…