初步判断是否有泄漏
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, ¶m);
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_RR
int 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], ¶m[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