当前位置:网站首页>One thread gets memory and another thread releases memory, resulting in memory leakage
One thread gets memory and another thread releases memory, resulting in memory leakage
2022-04-22 21:24:00 【Huo Hongpeng】
Using Junzheng x2000 Platform time , The memory footprint of the application will gradually increase , After that, the application will eventually be kill fall . By analyzing the reasons, it is found that , Apply for memory in a thread , The requested memory is released in another thread , After the release is completed, the thread ends , This operation will cause memory leakage .
The producer's problem is simple , In theory, there should be no such problem , So in x86 and arm Next test , No similar situation was found , Then make sure it's Junzheng x2000 Platform issues . Put the question to Junzheng , At present, you are investigating .
Here is my simple test code , You can test your own platform :
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
static pthread_mutex_t addr_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t free_mutex = PTHREAD_MUTEX_INITIALIZER;
static void *addr;
static int is_free;
static void malloc_test(int size)
{
pthread_mutex_lock(&addr_mutex);
addr = malloc(size);
printf("==========================\n");
printf("malloc: %p\n", addr);
pthread_mutex_unlock(&addr_mutex);
}
static void free_test(void)
{
pthread_mutex_lock(&addr_mutex);
printf("free: %p\n", addr);
printf("==========================\n");
free(addr);
pthread_mutex_unlock(&addr_mutex);
}
/*
* There is no memory leak
*/
void test1()
{
while (1) {
malloc_test(260);
free_test();
usleep(100000);
}
}
static void *free2_thread(void *arg)
{
pthread_detach(pthread_self());
free_test();
pthread_mutex_lock(&free_mutex);
is_free = 0;
pthread_mutex_unlock(&free_mutex);
return NULL;
}
/*
* Allocate memory in a thread , Free memory in another thread , After release , End of release thread , As long as the memory allocation is less than 517 There is a memory leak
* cat proc/ Process number /status
*/
void test2()
{
pthread_t thread;
while (1)
{
malloc_test(516);
pthread_mutex_lock(&free_mutex);
is_free = 1;
pthread_mutex_unlock(&free_mutex);
pthread_create(&thread, NULL, free2_thread, NULL);
while (is_free) {
usleep(1000);
}
}
}
static void *free3_thread(void *arg)
{
pthread_detach(pthread_self());
while (1) {
free_test();
pthread_mutex_lock(&free_mutex);
is_free = 0;
pthread_mutex_unlock(&free_mutex);
usleep(10000);
}
return NULL;
}
/*
* Allocate memory in a thread , Free memory in another thread , When the release thread does not end , There is no memory leak
* cat proc/ Process number /status
*/
void test3()
{
pthread_t thread;
pthread_create(&thread, NULL, free3_thread, NULL);
while (1)
{
malloc_test(516);
pthread_mutex_lock(&free_mutex);
is_free = 1;
pthread_mutex_unlock(&free_mutex);
while (is_free) {
usleep(1000);
}
}
}
void main(void)
{
//test1();
test2();
//test3();
}
版权声明
本文为[Huo Hongpeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222122091198.html
边栏推荐
- kubernetes_ How to solve the problem that namespace cannot be deleted
- 新闻速递 I MobTech通过中国信通院“安全专项评测”
- Redis master-slave replication
- Tool class xmlutil (parse and return soap and XML messages to obtain the value of the target node)
- MySQL sorting and paging
- 东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
- Collect excellent blog articles
- News Express I mobtech passed the "special safety evaluation" of China Academy of information and communications
- [database learning 01]
- leetcode-470. Rand10() is implemented with rand7()
猜你喜欢
![[data cleaning and drawing] simple application of dataframe](/img/4b/aa119ada8d3267d180735fc2216bb9.png)
[data cleaning and drawing] simple application of dataframe

2022-4-22 Leetcode 279.完全平方数

EMMC 擦除组的理解

After five years of graduation, how to increase the monthly salary from 5K to 50W +, what core skills do you need to master?

最新版去水印+外卖cps小程序源码

Spark-2.4.2 compilation and installation

在线YAML转XML工具

Experiment 3

. net core uses iasyncexceptionfilter to capture global exceptions and return information uniformly

Hdlbits (10) learning notes - finite state machine (fsm1 - lemmings4)
随机推荐
MySQL deadlock analysis and solution caused by index merging!
How to do a good job in performance pressure measurement: design and construction of pressure measurement environment
[hand pose estimation] [detailed reading of the paper] 3D hand pose estimation with a single infrared camera via domain transfer learning
Adobe series error code solutions summary
BLE---Advertisement data format & service
[what is istio?] You're out before you know it. You can understand it quickly in 40 minutes
在线YAML转XML工具
. net core add Middleware
Summary driven development record
[IPTV]华为悦盒EC6108V9A刷机
Pytoch note57 pytoch visual network structure
Basic design of character animation based on paogd
关于Libffi
M5增强版双系统套餐包含哪些重要配件,该怎么选?
【数据清洗、绘图】Dataframe的简易应用
一个线程获取内存另一个线程释放内存造成内存泄漏
[Istio是什么?] 还不知道你就out了,一文40分钟快速理解
hostapd 创建5G热点
QT qstring stepping pit
Interface test mock practice (II) | complete batch manual mock in combination with JQ