Memory Allocation/Deallocation issues??
-
I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan
-
I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan
pavanbabut wrote:
Is there something I am doing wrong??
As long as you are freeing all memory that you allocate, then no :)
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."
-
I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan
You can make calls to GetProcessWorkingSetSize and GlobalMemoryStatus to see if the numbers are changing.
-
You can make calls to GetProcessWorkingSetSize and GlobalMemoryStatus to see if the numbers are changing.
Whenever I check it, it is freeing only half of the allocated size. But it frees the whole allocated memory when I deallocate it at the time of terminating the application, not while 'm running the application. -Pavan.
-
I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan
How are you checking this? There's no accurate way that I know of. Try this if you'd like:
MEMORYSTATUS OrigMemoryStat;
::GlobalMemoryStatus(&PrevMemoryStat);TRACE(_T("********************************\n"));
for (int i = 0; i < 100; ++i)
{
HGLOBAL hGlobal = ::GlobalAlloc(GHND, 2000);
::GlobalFree(hGlobal);MEMORYSTATUS MemoryStat;
::GlobalMemoryStatus(&MemoryStat);
TRACE(_T("********************************\n"));
// TRACE(_T("dwTotalPhys %lu Bytes\n"),MemoryStat.dwTotalPhys);
// TRACE(_T("dwAvailPhys %lu Bytes\n"),MemoryStat.dwAvailPhys);
// TRACE(_T("dwTotalVirtual %lu Bytes\n"),MemoryStat.dwTotalVirtual);
// TRACE(_T("dwAvailVirtual %lu Bytes\n"),MemoryStat.dwAvailVirtual);
// TRACE(_T("dwMemoryLoad %lu%%\n"),MemoryStat.dwMemoryLoad);
TRACE(_T("** Delta dwAvailPhys %li Bytes\n"), (long)MemoryStat.dwAvailPhys - (long)OrigMemoryStat.dwAvailPhys);
TRACE(_T("** Delta dwAvailVirtual %li Bytes\n"), (long)MemoryStat.dwAvailVirtual - (long)OrigMemoryStat.dwAvailVirtual);
}TRACE(_T("********************************\n"));
Trust the OS :~ :suss:
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."
-
How are you checking this? There's no accurate way that I know of. Try this if you'd like:
MEMORYSTATUS OrigMemoryStat;
::GlobalMemoryStatus(&PrevMemoryStat);TRACE(_T("********************************\n"));
for (int i = 0; i < 100; ++i)
{
HGLOBAL hGlobal = ::GlobalAlloc(GHND, 2000);
::GlobalFree(hGlobal);MEMORYSTATUS MemoryStat;
::GlobalMemoryStatus(&MemoryStat);
TRACE(_T("********************************\n"));
// TRACE(_T("dwTotalPhys %lu Bytes\n"),MemoryStat.dwTotalPhys);
// TRACE(_T("dwAvailPhys %lu Bytes\n"),MemoryStat.dwAvailPhys);
// TRACE(_T("dwTotalVirtual %lu Bytes\n"),MemoryStat.dwTotalVirtual);
// TRACE(_T("dwAvailVirtual %lu Bytes\n"),MemoryStat.dwAvailVirtual);
// TRACE(_T("dwMemoryLoad %lu%%\n"),MemoryStat.dwMemoryLoad);
TRACE(_T("** Delta dwAvailPhys %li Bytes\n"), (long)MemoryStat.dwAvailPhys - (long)OrigMemoryStat.dwAvailPhys);
TRACE(_T("** Delta dwAvailVirtual %li Bytes\n"), (long)MemoryStat.dwAvailVirtual - (long)OrigMemoryStat.dwAvailVirtual);
}TRACE(_T("********************************\n"));
Trust the OS :~ :suss:
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."
Ok, I figured out the problem. Once I allocate the memory, I am saving data into the variable by calling another function, in this process I am allocating more memory to another variable to hold a bitmap data, which is not being deallocated later and its piling up. -Pavan.
-
I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan
have a look at this : http://www.flounder.com/howbig.htm [^]
Maximilien Lincourt Your Head A Splode - Strong Bad
-
have a look at this : http://www.flounder.com/howbig.htm [^]
Maximilien Lincourt Your Head A Splode - Strong Bad
Nice article... thanks..:)