Memory leaks 如何确保内存泄漏得到处理?

Memory leaks 如何确保内存泄漏得到处理?,memory-leaks,Memory Leaks,操作系统何时释放内存?当我使用性能计数器时,我看不到它被删除。请参阅下面的代码。分配前的内存使用量与取消分配后的内存使用量之差应为0,但不是0 基本上,我有一个托管在dllhost中的COM dll,它会泄漏内存(在32位MS-OS上超过2GB) #包括“stdafx.h” #包括 #包括 #包括 #包括 //#定义\u CRTDBG\u映射\u ALLOC #包括 #包括 使用名称空间std; /* #ifdef_调试 #ifndef DBG_新 #定义DBG_NEW NEW(_NORMAL_

操作系统何时释放内存?当我使用性能计数器时,我看不到它被删除。请参阅下面的代码。分配前的内存使用量与取消分配后的内存使用量之差应为0,但不是0

基本上,我有一个托管在dllhost中的COM dll,它会泄漏内存(在32位MS-OS上超过2GB)

#包括“stdafx.h”
#包括
#包括
#包括
#包括
//#定义\u CRTDBG\u映射\u ALLOC
#包括
#包括
使用名称空间std;
/*
#ifdef_调试
#ifndef DBG_新
#定义DBG_NEW NEW(_NORMAL_BLOCK、_FILE、_LINE)
#定义新DBG_new
#恩迪夫
#endif/\u调试
*/
模板
类ctypedheaptrmap:公共映射
{
公众:
//建筑
ctypedheaptrmap()
{
};   
//析构函数
~ctypedheaptrmap()
{
删除内容();
};  
void DeleteContents()
{
迭代器输入;
/*清空列表并删除内存*/
ItEntry=begin();
while(ItEntry!=end())
{
T pT=输入->秒;
删除[]pT;
pT=NULL;
ItEntry++;
}
map::clear();
};  
};
typedef ctypedheaptrmap验证图;
int _tmain(int argc,_TCHAR*argv[]
{
进程_内存_计数器_expmcx={};
pmcx.cb=sizeof(pmcx);
GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast(&pmcx),pmcx.cb);
//假设上面的GetProcessMemoryInfo调用分配了一些内存。所以再次获取内存状态
pmcx.cb=sizeof(pmcx);
GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast(&pmcx),pmcx.cb);
printf(“内存使用量(分配前)=%ld\n”,pmcx.WorkingSetSize);
{
验证地图pStr;
char*ptr1=新字符[10000];
插入(验证映射::值类型(1,ptr1));
char*ptr2=新字符[10000];
插入(验证映射::值类型(2,ptr2));
char*ptr3=新字符[10000];
插入(验证映射::值类型(3,ptr3));
char*ptr4=新字符[10000];
pStr.insert(验证映射::值类型(4,ptr4));
char*ptr5=新字符[10000];
插入(验证映射::值类型(5,ptr5));
char*ptr6=新字符[10000];
插入(验证映射::值类型(6,ptr6));
char*ptr7=新字符[10000];
插入(验证映射::值类型(7,ptr7));
char*ptr8=新字符[10000];
插入(验证映射::值类型(8,ptr8));
char*ptr9=新字符[10000];
插入(验证映射::值类型(9,ptr9));
}
pmcx.cb=sizeof(pmcx);
GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast(&pmcx),pmcx.cb);
printf(“内存使用量(取消分配后)=%ld\n”,pmcx.WorkingSetSize);
睡觉(60000);//睡一分钟
返回0;
}

在使用内存的程序终止后,即在主程序中返回0后,系统将释放内存。当调用
GetProcessMemoryInfo
函数时,内存没有被释放,因此当它应该为零时,内存使用量会有很大的差异。实际上,在程序结束后,系统正在释放内存(它总是这样做)


但是,你不应该觉得你是清白的,你说泄漏的是2GB内存,这是很多,我高度怀疑你的程序需要这么多内存才能运行。您确实应该考虑在代码中查找位置,以释放未使用的变量的内存。

简单:不要创建泄漏验证。映射pSTR超出范围,因此调用析构函数。此时操作系统不释放内存吗?验证映射pStr超出范围,因此调用析构函数。操作系统当时不释放内存吗?在2 GB内存问题上,我有一个C++代码,它根据某种优化来计算一些东西。优化是在递归函数中进行的。该函数根据需要在堆上分配内存。析构函数应该释放它。这个特定的dll托管在dllhost中,代码是多线程的。所以,如果操作系统只是在程序退出后才释放内存,那对我就没有好处了。你真的不应该依赖操作系统释放内存。C和C++是低级别的,非常强大,原因是,你应该注意分配和分配你自己,它只保证程序退出后内存会被释放。
            #include "stdafx.h"
        #include <stdlib.h>
        #include <crtdbg.h>
        #include <list>
        #include <map>



        //#define _CRTDBG_MAP_ALLOC
        #include <stdlib.h>
        #include <crtdbg.h>

        using namespace std;
        /*
        #ifdef _DEBUG
           #ifndef DBG_NEW
              #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
              #define new DBG_NEW
           #endif
        #endif  // _DEBUG
        */
        template <class K, class T, class Pr = less<K>, class A = allocator<T> > 
        class CTypedHeapPtrMap : public map<K, T, Pr, A > 
        {
        public:   
            // Construction
            CTypedHeapPtrMap()
            {
            };   

            // Destructor
            ~CTypedHeapPtrMap() 
            {
                DeleteContents();
            };  

            void DeleteContents() 
            {
                iterator ItEntry;

                /* Empty the list and delete memory */
                ItEntry = begin();   
                while (ItEntry != end())
                {

                    T pT = ItEntry->second;
                    delete[] pT;
                    pT = NULL;
                    ItEntry++;
                }
                map<K,T,Pr,A>::clear();
            };  
        };

        typedef CTypedHeapPtrMap<long, char*>                   VALIDATION_MAP;

        int _tmain(int argc, _TCHAR* argv[])
        {
            PROCESS_MEMORY_COUNTERS_EX pmcx = {};

            pmcx.cb = sizeof(pmcx);
            GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmcx), pmcx.cb);

            //assumuing GetProcessMemoryInfo call above allocates some memory. So get the memory status again
            pmcx.cb = sizeof(pmcx);
            GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmcx), pmcx.cb);
            printf(" Memory usage (Before allocation) = %ld\n", pmcx.WorkingSetSize);

            {
                VALIDATION_MAP pStr;

                char *ptr1 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(1, ptr1));
                char *ptr2 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(2, ptr2));
                char *ptr3 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(3, ptr3));
                char *ptr4 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(4, ptr4));
                char *ptr5 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(5, ptr5));
                char *ptr6 = new char[10000];   
                pStr.insert(VALIDATION_MAP::value_type(6, ptr6));
                char *ptr7 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(7, ptr7));
                char *ptr8 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(8, ptr8));
                char *ptr9 = new char[10000];
                pStr.insert(VALIDATION_MAP::value_type(9, ptr9));
            }
            pmcx.cb = sizeof(pmcx);
            GetProcessMemoryInfo(GetCurrentProcess(),reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmcx), pmcx.cb);
            printf(" Memory usage (After de-allocation) = %ld\n", pmcx.WorkingSetSize);
            Sleep(60000);//sleep for a minute
            return 0;
        }