记忆占用增加

时间:2016-05-03 00:17:12

标签: c++ memory-management out-of-memory

我陷入困境;我的c++代码不断消耗更多内存(大约70G),直到整个过程被杀死。

我正在调用来自C++的{​​{1}}代码,该代码实现了Python算法。

Longest common subsequence length代码如下所示:

C++

我的#define MAX(a,b) (((a)>(b))?(a):(b)) #include <stdio.h> int LCSLength(long unsigned X[], long unsigned Y[], int m, int n) { int** L = new int*[m+1]; for(int i = 0; i < m+1; ++i) L[i] = new int[n+1]; printf("i am hre\n"); int i, j; for(i=0; i<=m; i++) { printf("i am hre1\n"); for(j=0; j<=n; j++) { if(i==0 || j==0) L[i][j] = 0; else if(X[i-1]==Y[j-1]) L[i][j] = L[i-1][j-1]+1; else L[i][j] = MAX(L[i-1][j],L[i][j-1]); } } int tt = L[m][n]; printf("i am hre2\n"); for (i = 0; i < m+1; i++) delete [] L[i]; delete [] L; return tt; } 代码是这样的:

Python

恕我直言,在from ctypes import cdll import ctypes lib = cdll.LoadLibrary('./liblcs.so') la = 36840 lb = 833841 a = (ctypes.c_ulong * la)() b = (ctypes.c_ulong * lb)() for i in range(la): a[i] = 1 for i in range(lb): b[i] = 1 print "test" lib._Z9LCSLengthPmS_ii(a, b, la, lb) 代码中,在C++操作之后可以在堆上分配大量内存, {{1>内部没有更多的额外内存消耗}}

然而,令我惊讶的是,我观察到在new期间使用的内存不断增加。 (我在loop上使用loop,并在进程被杀之前保持打印top

在这一点上我真的很困惑,因为我猜在内存分配之后,Linux内部只有一些算术运算,为什么代码会占用更多内存?

我清楚了吗?谁能在这个问题上给我一些帮助?谢谢!

2 个答案:

答案 0 :(得分:2)

你消耗太多记忆。系统在分配时不死的原因是因为Linux允许您分配比使用

更多的内存
http://serverfault.com/questions/141988/avoid-linux-out-of-memory-application-teardown

我在测试机器上做了同样的事情。我能够通过新的使用并开始循环,只有当系统决定我吃了太多的可用内存时才会杀了我。

这就是我得到的。 dmesg中的一条可爱的OOM消息。

[287602.898843] Out of memory: Kill process 7476 (a.out) score 792 or sacrifice child
[287602.899900] Killed process 7476 (a.out) total-vm:2885212kB, anon-rss:907032kB, file-rss:0kB, shmem-rss:0kB

在Linux上,您会在内核日志中看到类似的内容,或者看到dmesg的输出......

[287585.306678] Out of memory: Kill process 7469 (a.out) score 787 or sacrifice child
[287585.307759] Killed process 7469 (a.out) total-vm:2885208kB, anon-rss:906912kB, file-rss:4kB, shmem-rss:0kB
[287602.754624] a.out invoked oom-killer: gfp_mask=0x24201ca, order=0, oom_score_adj=0
[287602.755843] a.out cpuset=/ mems_allowed=0
[287602.756482] CPU: 0 PID: 7476 Comm: a.out Not tainted 4.5.0-x86_64-linode65 #2
[287602.757592] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.8.2-0-g33fbe13 by qemu-project.org 04/01/2014
[287602.759461]  0000000000000000 ffff88003d845780 ffffffff815abd27 0000000000000000
[287602.760689]  0000000000000282 ffff88003a377c58 ffffffff811d0e82 ffff8800397f8270
[287602.761915]  0000000000f7d192 000105902804d798 ffffffff81046a71 ffff88003d845780
[287602.763192] Call Trace:
[287602.763532]  [<ffffffff815abd27>] ? dump_stack+0x63/0x84
[287602.774614]  [<ffffffff811d0e82>] ? dump_header+0x59/0x1ed
[287602.775454]  [<ffffffff81046a71>] ? kvm_clock_read+0x1b/0x1d
[287602.776322]  [<ffffffff8112b046>] ? ktime_get+0x49/0x91
[287602.777127]  [<ffffffff81156c83>] ? delayacct_end+0x3b/0x60
[287602.777970]  [<ffffffff81187c11>] ? oom_kill_process+0xc0/0x367
[287602.778866]  [<ffffffff811882c5>] ? out_of_memory+0x3bf/0x406
[287602.779755]  [<ffffffff8118c646>] ? __alloc_pages_nodemask+0x8fc/0xa6b
[287602.780756]  [<ffffffff811c095d>] ? alloc_pages_current+0xbc/0xe0
[287602.781686]  [<ffffffff81186c1d>] ? filemap_fault+0x2d3/0x48b
[287602.782561]  [<ffffffff8128adea>] ? ext4_filemap_fault+0x37/0x51
[287602.783511]  [<ffffffff811a9d56>] ? __do_fault+0x68/0xb1
[287602.784310]  [<ffffffff811adcaa>] ? handle_mm_fault+0x6a4/0xd1b
[287602.785216]  [<ffffffff810496cd>] ? __do_page_fault+0x33d/0x398
[287602.786124]  [<ffffffff819c6ab8>] ? async_page_fault+0x28/0x30

答案 1 :(得分:1)

看看你在做什么:

#include <iostream>

int main(){

  int m = 36840;
  int n = 833841;
  unsigned long total = 0;

  total += (sizeof(int) * (m+1));

  for(int i = 0; i < m+1; ++i){
    total += (sizeof(int) * (n+1));
  }

  std::cout << total << '\n';

}

你只是在消耗太多的记忆 如果int的大小为4个字节,则分配122 GB。