【问题标题】:Program is Killed During Testing程序在测试期间被杀死
【发布时间】:2011-07-27 22:16:03
【问题描述】:

当我运行我的程序时,我会收到消息Killed,其中包含有关脚本的一些信息。在对这个问题进行了一些研究之后,我发现我并没有删除我的动态分配的变量(愚蠢的我!)。但是,现在,我觉得我已经解决了这个问题,但是当我使用 Linux 时,我仍然在终端中收到 Killed 消息。

    //does the of the manipulation of the load factor.
    for (int tableSize = fileLength; tableSize < fileLength * 2; tableSize = tableSize + 500)
    {   

        //creates hash tables to be reused for each of the trials.
        for(int fileNum = 0; fileNum < NUMTIMES; fileNum++)
        {   

            Array_HashTable* linear_div_hash = new Array_HashTable(tableSize);
            LinkedList_HashTable *chain_div_hash = new LinkedList_HashTable(tableSize);

            Array_HashTable *doubleHash = new Array_HashTable(tableSize);        
            LinkedList_HashTable *mult_hash = new LinkedList_HashTable(tableSize);
            //Does the hashing for each of the files created.
            for (int index = 0; index < fileLength; index++)        
            {
                linear_div_hash -> Linear_ProbeDH(read[fileNum][index]);
                chain_div_hash ->  Division_Hash(read[fileNum][index]);
                doubleHash -> Double_Hash(read[fileNum][index]);
                mult_hash -> Mulitplication_Hash(read[fileNum][index]);
            }//ends the index for loop.

            optimalOutput("VariableSizeLinearCollisionData", fileLength, tableSize, linear_div_hash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeDoubleCollisionData", fileLength, tableSize, doubleHash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeDivisionChainingCollisionData", fileLength, tableSize, chain_div_hash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeMultiplicationChainingCollisionData", fileLength, tableSize, mult_hash -> getCollisions(),fileAppendage);    

            linear_div_hash -> EndArray_HashTable(); 
            chain_div_hash-> EndLinkedList_HashTable();
            doubleHash -> EndArray_HashTable();
            mult_hash-> EndLinkedList_HashTable();

            delete  linear_div_hash; 
            delete  chain_div_hash ;
            delete  doubleHash ;
            delete  mult_hash ;
        }//ends the fileNum for loop
    }//ends the parent for loop with the size as the variable.

基本上代码是这样工作的,第一个 for 循环控制哈希表的大小。第二个循环控制将使用哪个文件的数据进行散列。并为此实例化一个哈希表对象。最后一个循环调用散列函数。然后使用输出函数将统计信息输出到文件中。然后我使用与析构函数类似的函数从我的类中删除动态变量。我不能使用析构函数来执行此操作,因为它给了我错误。然后我删除对象。

我能做什么?

【问题讨论】:

  • 我不知道我应该这样做。我刚刚回去为他们所有人做了这件事。感谢您的提醒。
  • 没问题!很高兴你很好地利用了 S.O.欢迎!
  • @tpar:鉴于您已经包含了一个希望人们查看的代码块,您可能应该用适用的语言标记问题。它看起来像 c++,所以我会为你坚持下去。如果我弄错了,请更改它。
  • @tpar44:有什么阻止您将linear_div_hash 和朋友声明为堆栈变量(非指针)吗?由于您没有在for 循环的范围之外使用它们,因此让它们成为堆栈变量并让它们超出范围为您调用析构函数会更容易。
  • @tpar44 你知道程序在什么时候被杀死了吗?

标签: c++ linux memory out-of-memory kill


【解决方案1】:

如果你在 linux 上运行,你可以使用 valgrind 来处理这个

valgrind myprogram

它会缓慢但会报告许多内存问题。如果还是找不到,可以使用 massif 对其进行堆分析

valgrind --tool=massif myprogram
ms_print <profile_output_file>

这将及时生成内存使用图以及在几个快照时刻的最大内存分配(包括分配位置的精确堆栈跟踪)。

哦,使用gcc -g 构建调试信息

【讨论】:

    【解决方案2】:

    在展示的代码中,您对两种类型的对象分别调用 newdelete 四次。这看起来不错如果Array_HashTableLinkedList_HashTable 的析构函数正确地释放了它们的对象分配的所有内存。

    如果您仍然从这段代码中泄漏内存,那么这些对象将是我的第一个嫌疑人。

    【讨论】:

    • 如果只有一个指针 int *array 是一个数组,你将如何正确释放析构函数中的内存?我只是想确保我做对了
    • @tpar:该类的程序员(你?)必须跟踪该类使用new(或new[],因为您谈论的是数组)分配的任何内存,然后调用delete(或delete[])在析构函数中的那个内存上。
    • 好吧,既然这就是我所做的,那是我唯一能做的来获得记忆吗?会不会是我的内存不足,无法在程序中使用?
    • @tpar:你能估计一下你的程序在正常运行期间会分配多少内存吗?这与可用内存相比如何?如果预期分配与可用内存一样大或更大,您可能会用完。如果它远小于您可能仍在某处泄漏内存。
    • 我在代码中发现了泄漏...我忘了删除一些动态变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多