释放内存的问题 [英] problem in free up the memory

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

问题描述

#include <conio.h>
#include<malloc.h>
int main(int argc, char* argv[])
{
    void *p;
    getch();
    p=malloc(sizeof(int)*14000);
        printf("memory is allocated");
    getch();
    free(p);
        getch();
            printf("memory has been cleard");
                getch();
    return 0;
}


我用那个代码..
我创建了指向指针的内存.
我检查了虚拟内存使用率是否很高(800k)
释放内存后,它仍然仅位于该位置(仍为800k)
我清除内存..那么为什么它仍然处于高位...


i use the that code..
i create a memory to the pointer.
and i check the virtual memory usage it goes high(800k)
After i free up the memory it still in that position only(still in 800k)
i clear the memory.. then why its still in high...

推荐答案

您正确地释放了内存.您是如何检查堆的?系统错误报告内存使用情况.另外,您检查了两次内存,但是您应该完成三遍:分配之前,分配之后和释放之后.您可能会看到相同的800K.您只分配了大约50K.不用担心.

您可以找到并使用一些内存泄漏检测工具,它们的工作原理完全不同:它们嵌入在您的代码中,并拦截与内存消耗有关的所有调用并计算每个字节.如果您想找到并使用这样的工具(我建议这样做),请从这里开始:
http://en.wikipedia.org/wiki/Memory_debugger [
You deallocated memory correctly. How did you check up heap? The system reports memory usage inaccurately. Also, you checked memory twice, but you should have done it three times: before allocation, after allocation and after deallocation. Chances are, you see the same 800K. You allocated only about 50K. Nothing to worry about.

You can find and use some memory leak detection tools, which work by a completely different principle: they are embedded in your code and intercept all calls related to memory consumption and calculate every byte. If you want to find and use such tool (which I would recommend), start here:
http://en.wikipedia.org/wiki/Memory_debugger[^].

—SA


任务管理器的粒度不是很好.尝试增大分配的内存大小,您最终应该可以看到区别.

尝试一下以查看不同之处:
The granularity of the task manager is not very good... try making the size of the allocated memory bigger and you should eventually be able to see the difference.

Try this to see a difference:
p=malloc(sizeof(int)*500000);


通常,c内存子系统从os中分配并释放更大块的内存.它使用自己的免费列表管理等功能,从那些较大的块中进行分配……看一下: http://en. wikipedia.org/wiki/Malloc [ ^ ]

最好的问候
Espen Harlinn
usually the c memory subsystem allocates and releases memory in larger chunks from the os. It the performs allocations from those larger chunks using its own free list management etc ... take a look at this: http://en.wikipedia.org/wiki/Malloc[^]

Best regards
Espen Harlinn


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

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