C++ 使用太多的内存(我想)

C++ 使用太多的内存(我想),c++,memory,memory-management,slowdown,C++,Memory,Memory Management,Slowdown,我试图运行一个程序,分析一堆包含数字的文本文件。文本文件的总大小约为12MB,我从360个文本文件中的每个文件中提取1000个副本,并将它们放入一个向量中。我的问题是,我的文本文件列表完成了一半,然后我的计算机速度减慢,直到不再处理任何文件。这个程序不是无限循环的,但是我认为我有一个问题,就是使用了太多的内存。有没有更好的方法来存储这些数据,而不会占用太多内存 其他可能相关的系统信息: 运行Linux 8GB内存 已安装Cern根框架(但我不知道如何减少内存占用) 英特尔至强四核处理器 如果您需

我试图运行一个程序,分析一堆包含数字的文本文件。文本文件的总大小约为12MB,我从360个文本文件中的每个文件中提取1000个副本,并将它们放入一个向量中。我的问题是,我的文本文件列表完成了一半,然后我的计算机速度减慢,直到不再处理任何文件。这个程序不是无限循环的,但是我认为我有一个问题,就是使用了太多的内存。有没有更好的方法来存储这些数据,而不会占用太多内存

其他可能相关的系统信息:

运行Linux

8GB内存

已安装Cern根框架(但我不知道如何减少内存占用)

英特尔至强四核处理器

如果您需要其他信息,我将更新此列表

编辑:我跑在最前面,我的程序使用了更多的内存,一旦超过80%,我就杀死了它。这里有很多代码,所以我将挑选内存分配的位,以及共享的位。 编辑2:我的代码:

void FileAnalysis::doWork(std::string opath, std::string oName)
{
//sets the ouput filepath and the name of the file to contain the results
outpath = opath;
outname = oName;
//Reads the data source and writes it to a text file before pushing the filenames into a vector
setInput();
//Goes through the files queue and analyzes each file
while(!files.empty())
{
    //Puts all of the data points from the next file onto the points vector then deletes the file from the files queue
    readNext();
    //Places all of the min or max points into their respective vectors
    analyze();
    //Calculates the averages and the offset and pushes those into their respective vectors
    calcAvg();
}
makeGraph();
}

//Creates the vector of files to be read
void FileAnalysis::setInput()
{
string sysCall = "", filepath="", temp;
filepath = outpath+"filenames.txt";
sysCall = "ls "+dataFolder+" > "+filepath;
system(sysCall.c_str());
ifstream allfiles(filepath.c_str());
while (!allfiles.eof())
{
    getline(allfiles, temp);
    files.push(temp);
}
}
//Places the data from the next filename into the files vector, then deletes the filename from the vector
void FileAnalysis::readNext()
{
cout<<"Reading from "<<dataFolder<<files.front()<<endl;
ifstream curfile((dataFolder+files.front()).c_str());
string temp, temptodouble;
double tempval;
getline(curfile, temp);
while (!curfile.eof())
{

    if (temp.size()>0)
    {
        unsigned long pos = temp.find_first_of("\t");
        temptodouble = temp.substr(pos, pos);
        tempval = atof(temptodouble.c_str());
        points.push_back(tempval);
    }
    getline(curfile, temp);
}
setTime();
files.pop();
}
//Sets the maxpoints and minpoints vectors from the points vector and adds the vectors to the allmax and allmin vectors
void FileAnalysis::analyze()
{
for (unsigned int i = 1; i<points.size()-1; i++)
{
    if (points[i]>points[i-1]&&points[i]>points[i+1])
    {
        maxpoints.push_back(points[i]);
    }
    if (points[i]<points[i-1]&&points[i]<points[i+1])
    {
        minpoints.push_back(points[i]);
    }
}
allmax.push_back(maxpoints);
allmin.push_back(minpoints);
}
//Calculates the average max and min points from the maxpoints and minpoints vector and adds those averages to the avgmax and avgmin vectors, and adds the offset to the offset vector
void FileAnalysis::calcAvg()
{
double maxtotal = 0, mintotal = 0;
for (unsigned int i = 0; i<maxpoints.size(); i++)
{
    maxtotal+=maxpoints[i];
}
for (unsigned int i = 0; i<minpoints.size(); i++)
{
    mintotal+=minpoints[i];
}
avgmax.push_back(maxtotal/maxpoints.size());
avgmin.push_back(mintotal/minpoints.size());
offset.push_back((maxtotal+mintotal)/2);

}
void FileAnalysis::doWork(std::string opath,std::string oName)
{
//设置要包含结果的输出文件路径和文件名
outpath=opath;
outname=oName;
//读取数据源并将其写入文本文件,然后将文件名推送到向量中
setInput();
//遍历文件队列并分析每个文件
而(!files.empty())
{
//将下一个文件中的所有数据点放到点向量上,然后从文件队列中删除该文件
readNext();
//将所有最小或最大点放置到各自的向量中
分析();
//计算平均值和偏移量,并将其推送到各自的向量中
calcAvg();
}
makeGraph();
}
//创建要读取的文件向量
void FileAnalysis::setInput()
{
字符串sysCall=“”,filepath=“”,temp;
filepath=outpath+“filenames.txt”;
sysCall=“ls”+数据文件夹+“>”+文件路径;
系统(sysCall.c_str());
ifstream所有文件(filepath.c_str());
而(!allfiles.eof())
{
getline(所有文件,临时文件);
文件推送(temp);
}
}
//将下一个文件名中的数据放入文件向量,然后从向量中删除文件名
void FileAnalysis::readNext()
{

cout这可以无限优化,但我的直接反应是使用一个容器而不是vector。记住,vector的存储是在内存中串行分配的,这意味着如果没有足够的当前空间容纳新元素,添加额外的元素会导致整个vector的重新分配

尝试为常量插入优化的容器,例如队列或列表

或者,如果需要vector,您可以尝试预先分配预期的内存占用,以避免连续重新分配。请参阅
vector.reserve()
:。请注意,保留容量是以元素而不是字节表示的

int numberOfItems = 1000;
int numberOfFiles = 360;

size_type totalExpectedSize = (numberOfItems) * (numberOfFiles);
myVector.reserve( totalExpectedSize );
----------编辑下面的代码帖子----------

我最关心的是
analyze()
中的以下逻辑:


还有其他的优化和评论要做,但现在我仅限于尝试解决您眼前的问题。您能否发布
makeGraph()
函数,以及所有相关容器(点、最小点、最大点、allmin、allmax)的定义?

这可以无休止地优化,但我的直接反应是使用除vector之外的容器。请记住,vector的存储是在内存中串行分配的,这意味着如果没有足够的当前空间容纳新元素,则添加其他元素会导致整个vector的重新分配

尝试为常量插入优化的容器,例如队列或列表

或者,如果需要vector,您可以尝试预先分配预期的内存占用,以避免连续重新分配。请参阅
vector.reserve()
:。请注意,保留容量是以元素而不是字节表示的

int numberOfItems = 1000;
int numberOfFiles = 360;

size_type totalExpectedSize = (numberOfItems) * (numberOfFiles);
myVector.reserve( totalExpectedSize );
----------编辑下面的代码帖子----------

我最关心的是
analyze()
中的以下逻辑:

还有其他的优化和评论需要做,但目前我仅限于尝试解决您眼前的问题。您能否发布
makeGraph()
函数,以及所有相关容器的定义(点、最小点、最大点、allmin、allmax)?

尝试以下几点:

  • 运行
    top
    查看程序正在使用的内存量
  • valgrind
    下运行一个较小的问题示例(例如,从一个文件中读取10个浮点值),并检查内存泄漏
  • 使用
    reserve()
  • 有几件事可以尝试:

  • 运行
    top
    查看程序正在使用的内存量
  • valgrind
    下运行一个较小的问题示例(例如,从一个文件中读取10个浮点值),并检查内存泄漏
  • 使用
    reserve()

  • 看看你的代码和迭代次数。如果你有这么多没有睡眠或基于事件编程的迭代,你的进程可能会消耗大量CPU

    为vector预先分配元素的数量,这样vector就不需要重新分配..但这将是多余的


    由于大部分程序都消耗CPU,请将进程作为后台运行,并使用top命令查看程序的CPU和内存使用情况。

    查看代码和迭代次数。如果在没有睡眠或基于事件的编程的情况下有如此多的迭代,进程可能会消耗大量CPU

    为vector预先分配元素的数量,这样vector就不需要重新分配..但这将是多余的

    由于您的程序主要消耗CPU,因此请将您的进程作为后台运行,并使用top命令查看CPU和mem
    File 1: 2 1 2 1 2 1 2 1 2 1 2
    minpoints: [1 1 1 1 1]
    allmin:    [1 1 1 1 1]
    
    File 2: 3 2 3 2 3 2 3 2 3 2 3
    minpoints: [1 1 1 1 1 2 2 2 2 2]
    allmin:    [1 1 1 1 1 1 1 1 1 1 2 2 2 2 2]
    
    File 3: 4 3 4 3 4 3 4 3 4 3 4
    minpoints: [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
    allmin:    [1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]