Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Freeing memory doesn't show the released memory in taskmgr

11 views
Skip to first unread message

Andrew Stark

unread,
Apr 29, 2013, 10:36:03 AM4/29/13
to
Hello Group,

i'm using malloc() to allocate some memory. My (ugly) testcode look's
like this:

char* mymy[6];
for (int i = 0; i < 5; ++i) {
mymem = GetProcessMemUsage(NULL);
cout << "ALLOC: RAM USED: " << ByteConv(mymem,
ByteConv_KiloBytes) << endl;
mymy[i] = (char*)malloc(65535);
Sleep(3000);
}

for (int i = 0; i < 5; ++i) {
mymem = GetProcessMemUsage(NULL);
cout << "DEALLOC: RAM USED: " << ByteConv(mymem,
ByteConv_KiloBytes) << endl;
free(mymy[i]);
Sleep(3000);
}

The first loop allocates some memory and GetProcessMemUsage() will get
an increased value of the used memory (which is correct)

In the next loop each memory-block will get free'd but this time
GetProcessMemUsage() doesn't show a decreased value of the used
memory.

How can i free such memory so that GetProcessMemUsage() or apps like
taskmgr can show the right value? Is this possible or do i have to
write my own memory-handler to get the right (real) used memory?

Thanks!
Best regards
Andrew

long long GetProcessMemUsage(HANDLE hprocess)
{
PROCESS_MEMORY_COUNTERS_EX pmc;
if (hprocess == NULL) hprocess = GetCurrentProcess();
GetProcessMemoryInfo(hprocess, (PROCESS_MEMORY_COUNTERS*)&pmc,
sizeof(pmc));
//SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
//SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
return pmc.PrivateUsage;
}

Deanna Earley

unread,
Apr 29, 2013, 10:50:51 AM4/29/13
to
On 29/04/2013 15:36, Andrew Stark wrote:
> Hello Group,
>
> i'm using malloc() to allocate some memory. My (ugly) testcode look's
> like this:
>
<SNIP>
>
> The first loop allocates some memory and GetProcessMemUsage() will get
> an increased value of the used memory (which is correct)
>
> In the next loop each memory-block will get free'd but this time
> GetProcessMemUsage() doesn't show a decreased value of the used
> memory.
>
> How can i free such memory so that GetProcessMemUsage() or apps like
> taskmgr can show the right value? Is this possible or do i have to
> write my own memory-handler to get the right (real) used memory?

Memory is "cached" and kept around depending on the allocator in use.
You do not have any direct control over exactly when it will be freed
back to the operations system, especially for such small amounts as 64KB.

--
Deanna Earley (dee.e...@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored. Please reply to the
group.)

Geoff

unread,
Apr 29, 2013, 1:49:51 PM4/29/13
to
On Mon, 29 Apr 2013 16:36:03 +0200, Andrew Stark <ast...@gmx.at>
wrote:

>
>How can i free such memory so that GetProcessMemUsage() or apps like
>taskmgr can show the right value? Is this possible or do i have to
>write my own memory-handler to get the right (real) used memory?

The point of using the OS memory management is to allow the system to
determine how much memory to allocate to your process based on it's
demands. If you want to know how much memory you have allocated and
freed you should probably keep a global counter and add/subtract the
malloc'd/freed bytes as you run.

The critical metric is the process working set, this is what the OS
memory allocator is concerned with. It's going to increase your
working set on demand but it's going to be lazy about decreasing your
working set in anticipation of more demand. It's more efficient to be
lazy about freed memory than to be constantly trying to keep everyone
lean.

You can persuade the OS to lean out your process working set with the
EmptyWorkingSet API function.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682606(v=vs.85).aspx

JJ

unread,
Apr 30, 2013, 12:08:52 AM4/30/13
to
On Mon, 29 Apr 2013 16:36:03 +0200, Andrew Stark wrote:
> The first loop allocates some memory and GetProcessMemUsage() will get
> an increased value of the used memory (which is correct)
>
> In the next loop each memory-block will get free'd but this time
> GetProcessMemUsage() doesn't show a decreased value of the used
> memory.
>
> How can i free such memory so that GetProcessMemUsage() or apps like
> taskmgr can show the right value? Is this possible or do i have to
> write my own memory-handler to get the right (real) used memory?

GetProcessMemUsage's PrivateUsage and Task Manager only show the virtual
memory. It doesn't show the Heap blocks (including CRT memory manager's
sub-blocks within a heap block) that are within virtual memory blocks. When
a heap block is freed, depending on the block location and size, the virtual
memory block that contains it may not shrink immediately or may not shrink
at all.
0 new messages