Re: 'disposable' dirty pages [was: Out Of Memory in v. 2.1]

Richard B. Johnson (root@chaos.analogic.com)
Thu, 8 Oct 1998 08:49:54 -0400 (EDT)


On Thu, 8 Oct 1998, Cameron Simpson wrote:

> On 7 Oct 1998, in message <Pine.GSO.3.96.981007120041.28599A-100000@ultra5>
> Ely Wilson <plexus@ionet.net> wrote:
> | Hmm, I'm wondering. I haev this application, it allocates 10 blocks each
> | 1 megs in size. Then I free() 5 of those blocks, but the program is still
> | eating 10 megs of memory.
> | Is this right?
>
> Well, it's "normal". UNIX processes generally only grow.
>
> "Right"? Well, if you don't touch those pages then they'll get swapped
> out in the normal course of affairs anyway, so there's no actual
> overhead there if you have enough swap, and you have the win that later
> malloc()s don't need to call the OS (via brk()) to get more memory -
> it can reuse those 5 free megs.
>
> There are costs, of course:
>
> - it consumes swap - which isn't an issue if you have enough, but
> can be an issue
>
> - it makes fork()s expensive - modern fork()s do copy-on-write, so
> the 5 megs of data doesn't actually need copying, but the pages
> themselves still need to be marked
>
> So it's not as bad as you fear, but it's not totally free, either.
> --
> Cameron Simpson, DoD#743 cs@zip.com.au http://www.zip.com.au/~cs/
>

glibc seems to give back memory. Here I allocate some memory, I don't
touch any pages, but the memory usage goes up, presumably due to the
malloc() overhead when it writes information after the new break address.

When the buffers are freed. the memory usage goes down. This runs all
night without any problems or any 'creeping' memory usage.

#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#define PTRS 0x1000
#define SIZE 0x10000

main()
{
char *p[PTRS];
size_t i;
if(fork()) return 0;
(void)close(2);
(void)close(1);
(void)close(0);
for(;;)
{
for(i=0; i< PTRS; i++)
if((p[i] = (char *) malloc(SIZE * sizeof(char))) == NULL)
exit(1);

for(i=0; i< PTRS; i++)
free(p[i]);
}
}

Cheers,
Dick Johnson
***** FILE SYSTEM WAS MODIFIED *****
Penguin : Linux version 2.1.123 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/