gpt4 book ai didi

c++ - 内存占用增加

转载 作者:行者123 更新时间:2023-11-30 02:31:56 28 4
gpt4 key购买 nike

我被困在有线的情况下;我的 c++ 代码不断消耗更多内存(达到大约 70G),直到整个进程被终止。

我正在从 Python 调用一个 C++ 代码,它实现了 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++ 代码中,在 new 操作之后可以在堆上分配大量内存,不会有更多的额外内存循环内的消费。

然而,令我惊讶的是,我观察到在 loop 期间使用的内存不断增加。 (我在 Linux 上使用 top,它会在进程终止之前打印 i am her1)

此时我真的很困惑,因为我猜内存分配后,循环内部只有一些算术运算,为什么代码占用更多内存?

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

最佳答案

你消耗的内存太多了。系统不会死于分配的原因是因为 Linux 允许您分配比您可以使用的更多的内存

http://serverfault.com/questions/141988/avoid-linux-out-of-memory-application-teardown

我只是在测试机上做了同样的事情。我能够绕过 new 的使用并开始循环,只有当系统确定我消耗了太多可用 RAM 时它才杀了我。

这就是我得到的。 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

关于c++ - 内存占用增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993623/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com