Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <3DD32536.DEBB4158@yahoo.com> Date: Wed, 13 Nov 2002 23:23:18 -0500 From: CBFalconer Reply-To: cbfalconer AT worldnet DOT att DOT net Organization: Ched Research X-Accept-Language: en MIME-Version: 1.0 To: Milos Popovic , cygwin AT cygwin DOT com Subject: Re: help: cygwin on WinNT - allocates 3x memory asked for References: <5 DOT 0 DOT 2 DOT 1 DOT 2 DOT 20021113160404 DOT 02790b80 AT hesiod> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit *** 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 (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net) Available for consulting/temporary embedded and systems. USE worldnet address! -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/