SML108@psuvm.psu.edu (Scott Le Grand) (04/22/91)
Hi, I wish to allocate 10 times as much memory as I really need to initialize some data. I use malloc to do this, and then use free to dispose of the 90% I am not going to use during execution (I am sampling the top 10% of a randomly generated distribution). When I use free on it, I find I am still using the huge amount of memory I started with, even after supposedly disposing of it. What is going on here? Scott
sweetmr@SCT60A.SUNYCT.EDU (michael sweet) (04/22/91)
> Hi, I wish to allocate 10 times as much memory as I really need to initialize > ... > using the huge amount of memory I started with, even after supposedly > disposing of it. What is going on here? Well, the *big* problem is that most implementations of malloc() use the sbrk() function to get more memory. Unfortunately, there is no 'unsbrk()' function to return the memory to the system. On some systems, memory is allocated onto the end of an existing block, making selective freeing of memory impossible. The only way around this is to roll you own malloc() function (see K&R 2nd edition) which keeps track of the highest-used address for a block of memory. When that block is completely free, you can call sbrk() with a negative number (the size of the block negated) to return the memory to the system. Note however that even this will not garentee that the memory is returned to the system, tho you can be confident that large blocks *will* be... -Mike Sweet ------------------------------------------------------------------------------ "The only TASC (315) 724-1100 (voice) truth is that 555 French Road (315) 724-2031 (fax) there are no New Hartford, NY 13413 Internet: sweetmr@sct60a.sunyct.edu truths, only beliefs." Delphi: DODGECOLT ------------------------------------------------------------------------------