向量< string>超出范围后不清除内存 [英] vector<string> does not clear memory after out of scope

查看:53
本文介绍了向量< string>超出范围后不清除内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题,并且我不确定自己是错还是真正奇怪的错误.我填写了大量的字符串,并希望在特定点将其清除.这是一个最小的例子

i've encountered the following problem, and i'm not really sure whether i am wrong or its a really weird bug. I fill a huge array of strings and want it to be cleared at a certain point. Here's a minimal example

#include <string>
#include <vector>
#include <unistd.h> //sleep
#include <iostream>

int main(){
    {
        std::vector<std::string> strvec;
        for(long i = 0; i < 1000000; ++i){
            std::string out = "This is gonna be a long string just to fill up the memory used by this fucking pthread\n";
            strvec.push_back(out);
        }
        std::cout << "finished loading 1st\n";
        sleep(10); // to monitor any changes    
    }
    std::cout << "out of scope\n";
    sleep(10);
    return 0;
}

我的问题是,如果我使用"top"监视内存使用情况,则内存使用量仅会减少很少的量(我认为这可能是向量开销),但大多数似乎没有释放.怎么会?我用'long long'测试了相同的场景,但是在这里一切正常.

My Problem is, if i monitor memory usage with 'top', the memory usage decreases just by a very small amount (i think its probably the vector overhead), but the most seems not freed. How comes? I tested the same scenario with 'long long', but here everything went right.

std :: vector引用指出,如果所包含的值不是指针,则将调用析构函数.似乎对于字符串不是正确的...

The std::vector reference states, that, if the contained values are no pointers, the destructors are invoked. Seems not true for string though...

我很感谢每个答案.

(为方便起见:我将Debian Linux 64Bit与g ++ 4.7.2一起使用)

(for convenience: i'm using debian linux 64Bit with g++ 4.7.2)

谢谢您到目前为止的回答.

thanks for the answers so far.

现在,我已经使用vagrind断层分析了堆的使用情况,并且(是的,实际上正如预期的那样)在应有的位置正确释放了堆使用情况.但是为什么我实际上看到一个巨大整数的用法发生了变化,而不是字符串(也在顶部)?

by now i have profiled heap usage with vagrind massif, and (yeah, actually as expected) it gets properly freed at the point it should. But why do i in fact see a change in usage with a huge integer, but not with the strings (also whithin top)?

我需要对此有所了解,因为我需要能够在特定时间释放我的内存以用于多线程服务器应用程序,该应用程序可能会运行数周或更长时间而无需重新启动.我什么时候才能真正知道C ++内存管理器何时决定将一些内存还给操作系统?

I need to be a little considerable about that, because i need to be able to free my memory at certain times for a multithreaded server application, which will probably run several weeks or more without being restarted. When do i actually know when the C++ memory manager decides to give some mem back to the OS?

推荐答案

这是使用 top 命令而不是 std :: vector 的特定命令.问题是由数据结构释放的内存不会释放回操作系统,即 top 命令监视内存使用的级别.操作系统为您的程序提供的内存将一直保留在您的程序中,直到C ++的内存管理器决定是时候将一些内存释放回操作系统了.

This is a specific of using the top command, not of the std::vector. The issue is that the memory freed by data structures is not released back to the operating system, the level at which the top command monitors memory usage. The memory the OS has given your program remains with your program until the memory manager of C++ decides that it's time to free some memory back to the operating system.

这样做的原因是从操作系统分配内存是相对昂贵的,并且需要以相对较大的块来完成.C ++运行时库的内存管理器从操作系统批发"中获取内存,然后根据需要将其打包到程序的各个部分.

The reason for this is that allocating memory from the operating system is relatively expensive, and it needs to be done in relatively large chunks. The memory manager of the C++ runtime library obtains memory from the OS "wholesale", and then parcels it out to the parts of your program as needed.

如果您想验证是否确实回收了内存,请使用一个工具来监视较低级别的内存使用,例如 valgrind .

If you would like to verify that the memory does indeed get reclaimed, use a tool that monitors memory usage at a lower level, such as valgrind.

这篇关于向量&lt; string&gt;超出范围后不清除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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