Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Sep 1997 06:12:36 +0200 (CEST)
From:      Mikael Karpberg <karpen@ocean.campus.luth.se>
To:        sef@Kithrup.COM (Sean Eric Fagan)
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Bug in malloc/free (was: Memory leak in getservbyXXX?)
Message-ID:  <199709220412.GAA08834@ocean.campus.luth.se>
In-Reply-To: <199709220258.TAA27605@kithrup.com> from Sean Eric Fagan at "Sep 21, 97 07:58:03 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
According to Sean Eric Fagan:
> >> 	char *cp = malloc(100);
> >> 	if (cp) {
> >> 		free(cp);
> >> 		cp = malloc(100);
> >> 	}
> >No, if you want the above code to always result in a non-NULL cp,
> >free() cannot ever return the memory back to the OS.
> [...] if the first malloc() succeeded, then, even if there is
> no more space available after that first malloc(), the free() and subsequent
> malloc() are required to work.

But if you do that allocation, and by doing so steals the last available
page in the system (swap and memory are exausted after your alloc succeeds)
and you free() it, system gets it back, and something else requests it, when
you don't have anywhere to take memory from for the second malloc(). All the
pages are taken, and you can't swap anything out. Malloc() would have to fail.
No?

However, if I understand everything right, phkmalloc doesn't shrink the
program's space, but simply tells the OS that the page's contents are not
important and can be disregarded. The program still has enough space
allocated on the swap to write the page to, but there is no need to write
it to that space, and read it back when the memory is accessed. Instead
the page is not reclaimed until the OS needs a free page, and if the program
wants to use the page later, it gets a zeroed page from the system. This
can be guaranteed since there is space on the swap for this page, so another
program can have an idle page be swapped out to there, and the program can
then get that page, after it has been zeroed.

Then it would indeed be guaranteed that the second malloc() would succeed.

Am I right? Maybe I'm completely off instead? :-)

  /Mikael



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199709220412.GAA08834>