Re: How to release heap memory that is marked as 'free'



Wrong. What SetProcessWorkingSetSize does is screw up your process by forcing all the
pages out. It doesn't free anything; it just gives the illusion of freeing something, and
consequently it is actually a Really Bad Idea. I think it was Mark Russinovich who coined
the term "fraudware" for programs that do this.

Believing any of those numbers leads to erroneous solutions for non-problems. If you slow
down, it means you have badly fragmented memory, and "returning" memory to the OS is not
going to solve things. You need to rethink the entire problem that is causing the
fragmentation, not apply superficial non-solutions to the problem.

SetProcessWorkingSetSize is working as advertised; the problem is that it isn't even
vaguely related to solving your problem, and its use is inappropriate.

I've spent a nontrivial part of my career writing and optimizing storage allocators. You
need to understand why all this memory is being allocated. If you have a gigabyte image,
you are doing a lot of allocations. Whie a heapwalker is very informative, it cannot be
used in conjunction with any tools such as process viewer, manipuating the working set, or
any similar tools; it completely invalidates their information. So you need to understand
exactly where all that storage is going. It sounds to me like chronic fragmentation
syndrome. And that takes a lot of work to fix. The fix will not ever involve
SetProcessWorkingSetSize.
joe
On Thu, 13 Apr 2006 10:26:29 -0400, "Michael Evenson" <mevenson@xxxxxxxxxxxx> wrote:

Tom,

I have found that using the SetProcessWorkingSetSize function does
immediately show the memory being returned in task manager. I set a
breakpoint just before the call and the memory usage for the process is over
4MB. Immediately after the call, Task Manager shows the memory usage to be
around 130K which is what I would expect. When I then look at the heap dump,
I still have a very large amount of 'FREED' memory that belongs to my
process's allocated memory pool. Then when I go to allocate a very small
amount of memory (say for a local CString), the memory usage in Task Manager
shows application has the original 4MB of memory allocated. This 4MB value
eventually grows to over 1GB during program execution and it slows to a
crawl. By the way, I should mention that all of my calls to 'new' to
allocate memory that I have control over inside my application are done at
program startup and NOT within the loop where the memory seems to be getting
allocated and released.

I am thinking that the SetProcessWorkingSetSize function is not working
as advertised. Is this possible. I am checking the return code from
SetProcessWorkingSetSize and it is non-zero indicating that it was
successful.

Mike


"Tom Serface" <tserface@xxxxxxx> wrote in message
news:OF6$KOwXGHA.3868@xxxxxxxxxxxxxxxxxxxxxxx
Hi Michael,

I've seen the amount of memory for a process stay at a higher amount, but
once the memory is freed (especially globally allocated memory) Windows
seems to consume it again for other programs as needed. Are you seeing
other program "not" getting memory after you free it? Do the same
programs work OK if you exit your program first? Could it be that you've
allocated memory "around" the large block so the OS can't get it because
it's discontiguous? This article was interesting:

http://www.rsinc.com/services/techtip.asp?ttid=3346

Tom

"Michael Evenson" <mevenson@xxxxxxxxxxxx> wrote in message
news:5uGdnT3CS9jSz6PZRVn-vA@xxxxxxxxxxxxxxx

Actually the memory does still belong to the process, but has been freed.
Apparently Windows does not reclaim the freed memory from the heap until
it is needed. This is all well and good except that , if Windows cannot
find a large enough contiguous block of memory upon a request for
allocation, it prefers to use virtual memory over doing a garbage
collection to find more memory. The freed memory becomes part of my
process's 'working set'. Eventually, my process consumes so much memory
in it's working set (which is marked as free since there is no leak) that
all other process running on the machine have to get their memory from
the virtual memory pool.




Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
.



Relevant Pages

  • Re: CStrings and memory
    ... Then you allocate a few more strings ... Now you need to allocate another string, but there is no more memory. ... In addition to strings, there are other things that get allocated: ... have perhaps confused "address space" with "working set". ...
    (microsoft.public.vc.mfc)
  • Tru64 issues with Infinite limits
    ... An automated test in my nightly build was failing due to Out of Memory ... Cannot allocate block 146 ...
    (comp.unix.tru64)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Re: Sharing memory between kernelspace and userspace
    ... deallocate, on a totally dynamic basis, userspace ... Let userspace allocate shared memory visible to multiple ... and pass that into the kernel for it to write to. ...
    (Linux-Kernel)