This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: help: cygwin on WinNT - allocates 3x memory asked for

*** Posted and mailed ***
Milos Popovic wrote:
... snip ...
> PROBLEM: a C program written to allocate x amount of memory actually
> uses about 3 times that much memory, when compiled with gcc (3.2-2)
> under cygwin (1.3.15-2). This does not occur when compiling with a
> native windows compiler (I used lcc) where allocating 50MB increases
> the system memory used by roughly the 50MB.
> The program below tries to allocate memory starting with 16MB and
> doubling the allocation until it fails. The getchar statements are
> there to interrupt the program and wait for a keypress so that I can
> monitor the amount of memory used by the system in the Windows NT
> Task Manager before and after each allocation.
> The result is that a 512MB allocation actually increases the system
> memory usage by about 1500MB. This ratio of 3 is the same regardless
> of the size of allocation. When compiled with a native compiler
> (non-cygwin), a 512MB allocation increases the used ram by about
> 512MB. I also tried using realloc instead of malloc/free for
> successive allocations - no difference. C++ version with new/delete
> on g++ - also no difference.  The factor of 3 is also the same
> whether you're allocating a vector of chars, doubles, etc.

... snip code ...

You are probably looking at the underlying system allocation of
virtual memory.  When memory can be allocated with an allocate on
write policy, it makes sense to reserve a ratio of virtual
allocation to real allocation (which is not actual memory) of that
sort.  I don't know just how Cygwin allocates, but it probably
involves a sbrk (unix like) call, which in turn calls some windows
mechanism.  One of those mechanisms is anticipating future
requirements on the basis of actual allocation so far.  With
appropriate policies this can be done at no cost, although the
system has been known to bite when the memory is actually written,
and thus gobbles disk space for the virtual image.  Strictly
speaking the policy is not ISO C conforming.

BTW note that your policy of doubling the size prevents ever
reusing previously allocated and freed memory.  This in itself
forces the system memory assigned to always have a useless
fragment slightly smaller than the last allocation. 1/2 + 1/4 +
1/8 + ... is always smaller than 1.

You would probably have widely different results if you wrote all
over each allocated block when assigned.

I hope this makes some sense to you.  At any rate, I wouldn't
worry about it.

Chuck F ( (
   Available for consulting/temporary embedded and systems.
   <>  USE worldnet address!

Unsubscribe info:
Bug reporting:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]