将动态分配的内存返回到OS,而不终止程序 [英] Returning dynamically allocated memory back to OS without terminating the program

查看:135
本文介绍了将动态分配的内存返回到OS,而不终止程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个程序,我使用大但有限的内存量。内存被分配并在不同线程的运行时释放。但是,我注意到程序的内存使用不会保持在指定的界限内。它会随着时间的流逝而增加。我写了下面的示例程序来检查内存是否正在释放回操作系统。一半的分配的内存被释放以检查内存使用是否下降。

I am working on a program where I am using large but limited amount of memory. Memory is allocated and freed on run time on different threads. However, I noticed that the memory usage of the program would not remain within specified bounds. It would increase as time passed on. I wrote the following sample program to check whether memory is being freed back to the OS. Half the allocated memory was freed to check if the memory usage went down.

int main()
{
    char *p[COUNT];

    for(int i = 0; i < COUNT; i++)
    {
        p[i] = new char[1048576];
        memset (p[i], 0, 1048576);
        printf("%p\n", p[i]);
    }

    printf("done allocating ... \n");

    sleep(10);

    printf("Freeing\n");
    for(int i; i < COUNT; i++)
    {
        delete[] p[i];
    }

    while(1)
        sleep(1);
}

运行程序后,操作系统似乎不会回收释放的页面。内存使用保持与linux中的top命令相同,分配后和释放后。它只是将页面标记为可供同一程序重复使用的页面。
在我的程序中,malloc和free在不同的线程上运行。当malloc比free更频繁地调用时,这会造成内存管理问题,并且进程数据段变得非常大,导致操作系统将页面交换到磁盘。这使得程序和操作系统,缓慢和无响应。有没有办法让操作系统回收释放的内存?

After running the program, it seemed that the OS would not reclaim the freed pages. The memory usage remains the same as seen in "top" command in linux, after allocating and after freeing. It simply marks the pages as free for reuse by the same program. In my program, malloc and free are running on different threads. This poses memory management problem when malloc is called a lot more frequently than free and the process data segment grows very large, causing OS to swap pages to disk. This makes the program and OS, slow and unresponsive. Is there any way for the OS to reclaim freed memory ?

推荐答案

进程退出后,进程使用的所有内存被回收。操作系统可能会保留数据推测性缓存,以防万一(如在 Linux Ate My RAM 中所述)。

After the process has exited, all memory used by the process will be reclaimed. The OS may keep the data speculatively cached just in case (as explained here on Linux Ate My RAM). The memory will be freed up whenever another process needs it.

编辑:因为你的意思是长时间运行的服务器进程,那么你的担心是内存使用,而进程仍然是运行。我可以建议 valgrind 作为检测应用程序中的内存泄漏的工具, a href =http://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii> RAII 作为防止内存泄漏的编码技术。小心Linux上的内存使用(和一般来说)很难测量/解释,ps和top等工具的输出可能是误导和不直观的。根据此答案

EDIT : since you mean a long-running server process, then your concern is memory usage while the process is still running. May I suggest valgrind as a tool to detect memory leaks in your application, and RAII as a coding technique to prevent memory leaks. Careful that memory usage on Linux (and in general!) is difficult to measure / interpret, the output of tools like ps and top can be misleading and unintuitive e.g. as per this answer.

这篇关于将动态分配的内存返回到OS,而不终止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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