什么是脏内存? [英] What is dirty private memory?

查看:664
本文介绍了什么是脏内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在64位Linux系统上开发应用程序.如我所见,我的应用程序正在消耗过多的脏堆内存.谈到堆内存,脏"是什么意思?是什么使它产生的,可以采取什么措施来防止它发生?

I'm developing an application on a 64-bit Linux system. As I could see, my app is eating too much dirty heap memory. Talking about the heap memory, what does "dirty" mean? What makes it arise and what can be done to prevent it to arise?

编辑

我最好解释一下我的应用程序执行的操作.

I'd better explain what operations my application performs.

我的应用程序在两个线程中运行:第一个线程将作业发送到队列,然后在另一个线程中执行.因此,第一个线程分配要排队的页面,第二个线程使它们出队,执行它们的作业并释放它们.所有这些操作都以线程安全的方式执行.

My application runs in two threads: the first thread sends jobs to a queue which are then executed in another thread. So, the first thread allocates pages to be queued and the second thread dequeues them, executes their jobs and free them. All these operations perform in a thread-safe fashion.

因此,我对此进行了测试,使它排队1亿个作业并全部执行.直到特定时刻,内存使用量才会增长.然后,当排队过程完成并且仅剩下一个出队时,内存使用率不会莫名其妙地减少.最后,当所有作业出队并执行时,所有的内存都将释放.因此,似乎在出队过程中发生了内存泄漏,因为当它完成时,所有的内存都被释放了,但是我发现它的代码没有错.

So I took a test on this thing, making it queue 100000000 jobs and execute them all. Until a particular instant, the memory usage grows. Then, when the queuing process finishes and only the dequeuing one remains, memory usage inexplicably doesn't decrease. Finally, when all the jobs are dequeued and executed, all that memory is freed. So, the memory leak seems to be happening in the dequeuing process, since when it finishes all the memory is freed, but I found nothing wrong in its code.

我知道如果我在这里发布代码会更好,但是它太大了.但是,从我添加的内容来看,是否有人猜测可能是什么原因造成的?

I know it would be better if I posted my code here, but it is too large. But, from what I added, does anybody have a guess about what might be causing this?

推荐答案

即使释放一些块,也要考虑内存的减少,最好在匿名模式下使用mmap,如下所示:

Talking about the memory non-decrease even after freeing some chunks, you'd better use mmap in anonymous mode like this:

mmap(NULL, chunck_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

不映射文件描述符,并返回指向内存块的指针,当您取消映射时,该内存块将立即返回到OS.但是,mmap需要系统调用,它比malloc慢.因此,您应该使用mmap分配大页面.

which maps no file descriptor and returns a pointer to a memory chunk which is immediately returned to the OS when you unmap it. However, mmap requires a system call, which is slower than malloc. So, you should use mmap for allocating large pages.

这篇关于什么是脏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆