简体   繁体   English

太多的内存被分配,以至于当我的程序运行得足够深时,我被“杀死”

[英]So much memory being malloc'd that I get “Killed” when running my program deep enough

I have a program that goes n^2 layers deep of recursion and mallocs a bunch of memory to concatenate char*s together. 我有一个程序,可以进行n ^ 2层递归深度处理,并分配一堆内存以将char * s连接在一起。 With a large enough n, the process just gets killed by the server (since it is consuming too much memory). 如果n足够大,则进程只会被服务器杀死(因为它消耗了太多的内存)。 How can I release this memory and still have my data? 如何释放此内存并仍然有我的数据?

It's mallocs look like 它的malloc看起来像

test = (char *)malloc(sizeof(char) * 256);

I need this data straight until the end of the program. 我需要这些数据,直到程序结束。 How can I deal with this? 我该如何处理?

You can't. 你不能 Once you release the memory the data is gone. 释放内存后,数据就消失了。

What you may be able to do is to make better use of the memory you have available. 您可能能够做的是更好地利用可用的内存。 With the code you posted, I can't think of a way to help you manage the memory any better though 使用您发布的代码,我想不出一种方法来帮助您更好地管理内存

Without thinking about it deeper, why do you need all of the data in memory? 不用更深入地思考,为什么需要内存中的所有数据? Several things you need to do: 您需要做的几件事:

  1. Write the char's to file 将字符写入文件
  2. use a global "master" char array, and only malloc for that, reuse the memory for the next segment. 使用全局“主” char数组,仅使用malloc,将内存重用于下一个段。 If this is your present setup, see 1 如果这是您当前的设置,请参见1
  3. Use valgrind to see if you're leaking all that memory. 使用valgrind查看是否正在泄漏所有内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM