News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

VirtualAlloc / VirtualFree

Started by ic2, May 26, 2007, 04:41:18 AM

Previous topic - Next topic

ic2

Why do Alloc functions add 4k to a running program and you already freed it during runtime.  And for every call to it thereafter, while running, it add 4k more to the memory of the program each and every time you call it.

I'm mainly specking of VirtualAlloc and VirtualFree.  .....Not sure about  HeapAlloc.  I'll be check that out in a few minutes. .....

I check it with TASK MANAGER on XP as i go viewing Mem Usage and Peak Memory.   It makes no since for an app to grow in memory.  And it seem that calling VirtualFree , it only release  something  but the  memory foot print is still there lingering in or around your app and the worse part is not being able to reused it again.  It only add more memory usage each time you call it again.

Why is this?  And is there any other ALLOC function that can  be use that will totally free the memory after usage?        Or is it that i'm doing something wrong.

Thanks in advance any insight about this



MichaelW

Using this test app, for GlobalAlloc and HeapAlloc, for small allocations (1KB) the memory usage does not decrease as the memory is freed. For larger allocations (64KB) the memory usage does decrease as the memory is freed, coming close to the original usage when all of the allocations have been freed. For VirtualAlloc, whether the allocation is large or small, the memory usage does decrease as the memory is freed, returning to exactly the original usage when all of the allocations have been freed. I used the management console system monitor to track the memory usage.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    REPEAT_COUNT  equ 16
    ALLOC_SIZE    equ 1024*64

    REPEAT REPEAT_COUNT
      print "GlobalAlloc",13,10
      call wait_key
      push alloc(ALLOC_SIZE)
    ENDM
    REPEAT REPEAT_COUNT
      print "GlobalFree",13,10
      call wait_key
      pop eax
      free eax
    ENDM

    REPEAT REPEAT_COUNT
      print "HeapAlloc",13,10
      call wait_key
      push halloc(ALLOC_SIZE)
    ENDM
    REPEAT REPEAT_COUNT
      print "HeapFree",13,10
      call wait_key
      pop eax
      free eax
    ENDM

    REPEAT REPEAT_COUNT
      print "VirtualAlloc",13,10
      call wait_key
      invoke VirtualAlloc,NULL,ALLOC_SIZE,MEM_COMMIT,PAGE_READWRITE
      push eax
    ENDM
    REPEAT REPEAT_COUNT
      print "VirtualFree",13,10
      call wait_key
      pop eax
      invoke VirtualFree,eax,0,MEM_RELEASE
    ENDM

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


eschew obfuscation

ic2

As far as  VIRTUAL ALLOC,  what you're saying is, if the app only use, lets say 8k of Virtual Memory and i free it... it's normal for an  app to keep the added size of Virtual Memory in memory, doing the life of the program.... and no matter how many times i call it doing the life of the app, as long as i call VirtualFree properly each and every time, that size should never increase and should show that fact in TASKMAN.

If i got this right, I'm definitely doing something wrong.

Now i know for a fact i got to fix it instead of continuing to wonder about it.


Thanks MichaelW

MichaelW

Running the code as posted, under Windows 2000, monitoring the Mem Usage and VM Size columns for the specific process on the Processes tab in Windows Task Manager:

At start                         684   168
After 16 calls to GlobalAlloc   1720  1260
After 16 calls to GlobalFree     696   172
After 16 calls to HeapAlloc      824  1260
After 16 calls to HeapFree       696   172
After 16 calls to VirtualAlloc   696  1196
After 16 calls to VirtualFree    696   172

eschew obfuscation