mmap() vs sbrk() -- Question ?? 
Author Message
 mmap() vs sbrk() -- Question ??

Hi,

I was using sbrk() so that I can give the
memory I was using back to the OS, when I
was done with it.  However, I was told that
sbrk() and malloc() should not be used together,
so I then switched over to mmap().  But mmap()
doesnt appear to give the memory back to the
OS when I munmap() it...does that sound correct?

If so, how can I give memory back to the OS, when
Im done with it?  Do I have to use sbrk() ???

My problem is that I need large amounts of memory,
for only a little time....

Thanks in advance

-Bill

--

William J Koscho



http://www.*-*-*.com/ ~koscho



Fri, 07 Jan 2000 03:00:00 GMT  
 mmap() vs sbrk() -- Question ??



Quote:
>I was using sbrk() so that I can give the
>memory I was using back to the OS, when I
>was done with it.  However, I was told that
>sbrk() and malloc() should not be used together,
>so I then switched over to mmap().  But mmap()
>doesnt appear to give the memory back to the
>OS when I munmap() it...does that sound correct?

Well... whether memory is "given back to the OS" in
some sense depends on the implementation, even for
sbrk() (which is a UNIXism, not available in every C
implementation).

Modern UNIX implementations of malloc() ought to be
able to survive having the program break *extended*
but not *contracted*.  However, if you can avoid
relying on this you are right to do so.

I vaguely recall that a mmap()ed segment is removed
from page space only after termination of the last
process that held a handle to it.  Again, this is a
UNIXism and (so far as I know) there is no guarantee
about such implementation details.

Quote:
>If so, how can I give memory back to the OS, when
>Im done with it?  Do I have to use sbrk() ???

*Perhaps* sbrk() will genuinely remove the tail from
the page pool.  If you do this, avoid anything (like
stdio) that may invoke malloc().

Quote:
>My problem is that I need large amounts of memory,
>for only a little time....

If your paging space is large enough, don't worry,
unused "memory" will soon migrate to disk rather than
RAM.  I don't think there is any good *general* way
to do what you're asking to do, although there may be
adequate ways to solve a *specific* problem.  (E.g.
fork a separate process.)  Good luck!

If the news reader allowed me to, I'd redirect
followups to a UNIX programming newsgroup, which would
be more useful than comp.lang.c for this topic.



Sat, 08 Jan 2000 03:00:00 GMT  
 mmap() vs sbrk() -- Question ??



Quote:
>Hi,

>I was using sbrk() so that I can give the
>memory I was using back to the OS, when I

mmap() vs. sbrk() is not a dilemma for comp.lang.c, but for
comp.unix.programmer.

Quote:
>so I then switched over to mmap().  But mmap()
>doesnt appear to give the memory back to the
>OS when I munmap() it...does that sound correct?

That does not sound correct at all. When you unmap pages, they are returned to
the OS.


Sat, 08 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. file i/o vs. mmap question

2. Question about sbrk()

3. brk(),sbrk() question

4. mmap question

5. mmap question

6. Problems with 2GB RAM on Linux and libc's sbrk

7. Problems with 2GB RAM on Linux and libc's sbrk

8. malloc and sbrk...what's the difference?

9. sbrk() in MS C 5.1 on MS-DOS

10. Looking for sbrk in qc2.5

11. sbrk() and malloc()

12. mmap() system call

 

 
Powered by phpBB® Forum Software