Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 5 Jul 2021 22:31:26 +0300
From:      Vitaliy Gusev <gusev.vitaliy@gmail.com>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        freebsd-hackers@freebsd.org, Mark Johnston <markj@freebsd.org>
Subject:   Re: madvise(MADV_FREE) doesn't work in some cases?
Message-ID:  <59F26FB5-3A88-42C5-816F-B877977CBA6B@gmail.com>
In-Reply-To: <57BCE463-6200-4F83-A321-2F0444E7F063@gmail.com>
References:  <0A95973D-254A-4574-8DC7-9F515F60B873@gmail.com> <YOBLn/XHpmEBfAdw@kib.kiev.ua> <57BCE463-6200-4F83-A321-2F0444E7F063@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_9276BB31-643D-47F6-84E2-082A874DDA5D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Further investigation shown that if use MADV_DONTNEED in simple mmap =
test program and run it twice (second after the first finishes =
madvise()), then memory is not freed at all and first instance is =
killed.

If change MADV_DONTNEED flag with  MADV_FREE, then memory is freed.


Code is:

#include <sys/mman.h>
#include <err.h>
#include <stdint.h>
#include <sys/cdefs.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
	size_t len =3D (size_t)(argc > 1 ? atoi(argv[1]) : 1024) * 1024 =
* 1024;
	uint8_t *ptr, *end;
	uint8_t *p;
	int pagesz =3D (1<<12);

	ptr =3D (uint8_t *)mmap(NULL, len, PROT_WRITE | PROT_READ, =
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (ptr =3D=3D MAP_FAILED)
		err(1, "cannot mmap");

	end =3D ptr + len;

	p =3D ptr;
	while (p < end) {
		*(uint64_t *)p =3D 1;
		p +=3D pagesz;
	}

	sleep(1);
	printf("madvise\n");

	p =3D ptr;
	while (p < end) {
		int error;

		error =3D madvise(p, pagesz, MADV_DONTNEED);
		if (error) {
			err(1, "cannot madvise");
		}
		p +=3D pagesz;
	}

	printf("press Enter to exit\n");
	getchar();
}


--Apple-Mail=_9276BB31-643D-47F6-84E2-082A874DDA5D--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?59F26FB5-3A88-42C5-816F-B877977CBA6B>