内存释放不起作用。 [英] Memory deallocation does not work.

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

问题描述



我有一个非常简单的代码块的问题,应该可以工作

但显然没有。

这个大块的代码只是一个POC,旨在找到一个更大的b
项目中的错误,其中似乎也出现了同样的问题。

这里的交易:当我运行这块时代码,我希望由测试分配的所有内存

要释放的对象,但我观察到的是第二次睡眠后的
(在向量的所有添加之后),进程占用的内存为$ 4G $ b,这是正常,但是在最后一次睡眠后,这个过程占用的内存高达590Meg

,当我预计它完全被释放时。

这段代码究竟出了什么问题?

提前很多

代码:


# include< stdio.h>

#include< stdlib.h>

#include< unistd.h>

#include< ; vector>

#include< iterator>

使用命名空间std;

class Test {

public:

unsigned char * data;


测试(){


}

void Go(int taille)

{


data =(unsigned char *)malloc(taille);

printf(" ; malloc:%p \ n",data);

}


~测试()

{

printf(" delete%p\ n,数据);

免费(数据);

}


};


vector< Test * v;

int i = 0;


int main()

{


sleep(10);


for(i = 0 ; i< 10000; i ++)

{

测试* t =新测试();

t-> Go(75000);

v.push_back(t);

}


睡觉(10);


vector< Test *> :: iterator it;

for(it = v.begin();它!= v.end();它++)

{


测试* t = *它;

删除(t );

}

睡觉(10);


}

解决方案

ch ******* *********@gmail.com 在新闻中写道:1171302593.431343.309790

@ q2g2000cwa.googlegroups.com:




我有一个非常简单的代码块的问题,应该可以工作

,但显然没有。



否......它并不明显不起作用。


这个代码块只是一个POC,旨在找到一个更大的项目中出现同样问题的错误。

这里的交易:我跑的时候这段代码,我希望通过测试分配所有内存

要释放的对象,但我观察到的是第二次睡眠后的
(在向量的所有添加之后),进程占用的内存为$ 4G $ b,这是正常,但是在最后一次睡眠后,这个过程占用的内存高达590Meg

,当我预计它完全被释放时。

这段代码究竟出了什么问题?

提前很多



假设你有的是你从

程序中删除(或免费)内存,它必须交还给操作系统。这是'b
$ b特定于实现的行为,关于是否和/或何时实际完成了



< blockquote> ch **************** @ gmail.com 写道:




我有一个非常简单的代码块应该有效的问题

但显然没有。

这个代码块只是一个POC,旨在找到一个更大的b / b
项目中的错误问题似乎发生了。

这里的交易:当我运行这段代码时,我希望通过测试分配所有内存

。要释放的对象,但我观察到的是第二次睡眠后的
(在向量的所有添加之后),进程占用的内存为$ 4G $ b,这是正常,但是在最后一次睡眠后,这个过程占用的内存高达590Meg

,当我预计它完全被释放时。

这段代码究竟出了什么问题??

提前很多



除了使用malloc /免费而不是新[] /删除[]?


据我所知,没有。你的期望是什么?免费

并删除将内存返回到免费商店。返回操作系统的内存是否为
完全取决于系统。


因此,在访问内存的意义上,内存正在被释放
通过指针
没有未定义,因为内存返回到

免费商店,它只是没有返回到操作系统。


如果您需要这样的行为,您将需要处理OS

细节。我建议在专门针对您的平台的新闻组中询问。

您可以在
http://www.parashift.com/c++-faq-lit...t.html#faq -5.9


12fév,19:11,red floyd< no.s ... @ here.dudewrote:


christophe.chaz ... @ gmail.com写道:




我有一个非常简单的代码块的问题应该可以工作

但显然不会。

这个代码块只是一个POC,旨在找到更大的bug

项目中似乎也出现了同样的问题。

这里的交易:当我运行这段代码时,我希望所有内存都是

由测试分配。要释放的对象,但我观察到的是第二次睡眠后的
(在向量的所有添加之后),进程占用的内存为$ 4G $ b,这是正常但是在最后一次睡眠后,这个过程占用的内存高达590Meg

,当我预计它完全被释放时。


这段代码究竟出了什么问题?

提前很多



除了使用malloc / free而不是new [] / delete []之外?



是的,我知道,但我们需要不时重新分配,因此我们选择malloc / free是

的原因

你知道用C ++处理可变大小缓冲区的更好方法吗?

对象?


据我所知,没什么。你的期望是什么?免费

并删除将内存返回到免费商店。返回操作系统的内存是否为
完全取决于系统。


因此,在访问内存的意义上,内存正在被释放
通过指针
没有未定义,因为内存返回到

免费商店,它只是没有返回到操作系统。


如果您需要这样的行为,您将需要处理OS

细节。我建议在专门针对您平台的新闻组中提问。

您可以在http://www.parashift.com/c++-faq-lite/how-to-post找到此类新闻组的部分列表。 html#faq-5.9



答案很多:这就是我想听到的:内存

被释放但尚未返回操作系统。

最终会在一段时间后返回吗?那个

就是说如果最后一次睡眠时间足够长,那么使用过的虚拟内存会减少(是的,我忘了提及这一切是

打算在Linux下运行)


无论如何,谢谢你和安德烈。


Christophe


Hi,
I have a problem with a really simple chunk of code which should work
but does obviously does not.
This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.

What exactly is wrong with this code ??
Thanx a lot in advance
Code :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include <iterator>
using namespace std;
class Test{
public :
unsigned char *data ;

Test(){

}
void Go(int taille)
{

data = (unsigned char*) malloc(taille);
printf("malloc : %p\n",data);
}

~Test()
{
printf("delete %p\n",data);
free(data);
}

};

vector<Test *v;
int i = 0 ;

int main()
{

sleep(10);

for(i = 0;i<10000;i++)
{
Test *t = new Test();
t->Go(75000);
v.push_back(t);
}

sleep(10);

vector<Test *>::iterator it;
for(it=v.begin() ; it!=v.end();it++)
{

Test *t = *it;
delete(t);
}
sleep(10);

}

解决方案

ch****************@gmail.com wrote in news:1171302593.431343.309790
@q2g2000cwa.googlegroups.com:

Hi,
I have a problem with a really simple chunk of code which should work
but does obviously does not.

No... it''s not obvious that it does not "work".

This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.

What exactly is wrong with this code ??
Thanx a lot in advance

The assumption you have is that when you delete (or free) memory from your
program that it is necessarily handed back to the OS. That''s
implementation-specific behaviour as to whether if and/or when that is
actually done.


ch****************@gmail.com wrote:

Hi,
I have a problem with a really simple chunk of code which should work
but does obviously does not.
This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.

What exactly is wrong with this code ??
Thanx a lot in advance

Other than the use of malloc/free instead of new[]/delete[]?

As far as I can tell, nothing. What is wrong is your expectation. free
and delete return the memory to the free store. Whether said memory is
returned to the operating system is completely system dependent.

So the memory is being deallocated in the sense that accessing memory
through the pointers is no undefined, as the memory is returned to the
free store, it''s just not being returned to the OS.

If you need such behavior to occur, you will need to deal with OS
specifics. I suggest asking in a newsgroup dedicated to your platform.
You can find a partial list of such newsgroups at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9


On 12 fév, 19:11, red floyd <no.s...@here.dudewrote:

christophe.chaz...@gmail.com wrote:

Hi,
I have a problem with a really simple chunk of code which should work
but does obviously does not.
This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.

What exactly is wrong with this code ??
Thanx a lot in advance


Other than the use of malloc/free instead of new[]/delete[]?

Yeah I know but we need to reallocate from time to time, so that''s the
reason why we chose malloc/free
Do you know a better way of dealing with variable size buffer in C++
objects ?

As far as I can tell, nothing. What is wrong is your expectation. free
and delete return the memory to the free store. Whether said memory is
returned to the operating system is completely system dependent.

So the memory is being deallocated in the sense that accessing memory
through the pointers is no undefined, as the memory is returned to the
free store, it''s just not being returned to the OS.

If you need such behavior to occur, you will need to deal with OS
specifics. I suggest asking in a newsgroup dedicated to your platform.
You can find a partial list of such newsgroups athttp://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Thanx a lot for the answer : that''s what I wanted to hear : the memory
is freed but not yet returned to the OS.
Would it eventually be returned after a certain amount of time ? That
is to say if the last sleep is long enough, would the used virtual
memory decrease in a top (Yeah I forgot to mention all this is
intended to run under Linux)

Anyway Thank you to you and Andre.

Christophe


这篇关于内存释放不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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