std :: sort内存不足 [英] std::sort out of memory

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

问题描述

大家好,

当我使用std :: sort排序超过1000万个值时,遇到内存不足错误.

除了最明显的原因之外,这可能是其他原因.我该如何解决这种行为.

谢谢,
Vishnu

Hi all,

When I use std::sort for sorting more than 10 million values, I encountered out of memory error.

What could be the reason for this other than the most obvious reason. How can i get through this behavior.

Thanks,
Vishnu

推荐答案

我无法想象sort算法需要那么多额外的内存.

我唯一能想到的是由于某种原因,比较和复制值可能会导致内存泄漏.您的值实际上是类的实例吗?如果是这样,那么赋值或复制构造函数之一可能会泄漏内存吗?

附言:您使用哪个容器存储排序后的值?为避免重新分配,以所需的大小对其进行预分配可能会有所帮助.除非是课程列表.
I can''t imagine the sort algorithm requires that much additional memory.

The only other thing I can think of is that for some reason comparing and maybe copying the values causes memory leaks. Are your values actually instances of a class? And if so, is it possible that one of the assignment or copying constructors leaks memory?

P.S.: what container are you using to store the sorted values? it might be beneficial to preallocate it in the needed size to avoid reallocation. Unless it''s a list of course.


我的测试程序"
My ''test program''
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main()
{
  const int SIZE = 16000000;

  time_t starttime, endtime;
  vector <int> huge;

  starttime = time(NULL);
  for (size_t i=0; i<SIZE; i++)
  {
     huge.push_back(rand());
  }


  for (size_t i=0; i<100; i++)
    cout << huge[i] << endl;


  sort(huge.begin(), huge.end());

  cout << "----------------------------" << endl;
  for (size_t i=0; i<100; i++)
    cout << huge[i] << endl;
  endtime = time(NULL);
  cout << "elapsed seconds: " << (endtime - starttime) << endl;
}


在我的32位系统上以340秒的速度运行.
我想您应该发布相关代码以获得更好的帮助.


ran in 340 seconds on my 32-bit system.
I suppose you should post the relevant code to get better help.


除了最明显的原因外,没有其他原因.如何通过?使用较少的值.或具有更多内存和64位应用程序的64位系统. :-)

—SA
No reasons other than the most obvious reason. How to get through? Use less values. Or 64-bit system with more memory and 64-bit application. :-)

—SA


这篇关于std :: sort内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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