This is the mail archive of the cygwin@cygwin.com 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]

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


Hi,

I hoped that someone would be willing to help me with resolving a simple cygwin-related malloc problem under Win NT 4.0. I have searched the web for an answer and haven't seen any related post. I apologize if the answer is obvious to posters on this list - I'm not very experienced with cygwin.


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.

Any hints? Listing is below. (I'd appreciate it if you'd cc me any reply). Thanks a lot!

Milos



/*
Finds largest power of 2 chunk of memory that can be allocated.
Milos Popovic, Nov 13, 2000
*/

#define EVER ;;
#define N 1024*1024*16/sizeof(mytype) // Start with 16MB vector

#include <stdio.h>
#include <stdlib.h>

typedef char mytype;

int main(void)
{
size_t n = N;
mytype *x; // = (char *) malloc(n * sizeof(mytype));

for(EVER) {
x = (mytype *) malloc(n * sizeof(mytype));

if (x == NULL) {
printf("Can't assign more memory!\n");
exit(1);
}

printf("Allocated %d bytes.\n", n * sizeof(mytype));
n *= 2; // Double amount of memory to allocate next time
printf("Hit enter."); getchar();
free(x);
// x = (char *) realloc(x, n * sizeof(mytype));
printf("Hit enter."); getchar();
}

printf("Done.\n");
return 0;
}


--
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/


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