From owner-freebsd-questions@FreeBSD.ORG Mon Dec 1 22:43:40 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 013141065675 for ; Mon, 1 Dec 2008 22:43:40 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 5CC888FC12 for ; Mon, 1 Dec 2008 22:43:38 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (adsl144-35.kln.forthnet.gr [195.74.243.35]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-5) with ESMTP id mB1MhSU1031739 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 2 Dec 2008 00:43:35 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id mB1MhSlP003802; Tue, 2 Dec 2008 00:43:28 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id mB1MhRvU003801; Tue, 2 Dec 2008 00:43:27 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: yuri@rawbw.com References: <49345710.9070403@rawbw.com> Date: Tue, 02 Dec 2008 00:43:26 +0200 In-Reply-To: <49345710.9070403@rawbw.com> (yuri@rawbw.com's message of "Mon, 01 Dec 2008 13:28:48 -0800") Message-ID: <874p1njz3l.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-MailScanner-ID: mB1MhSU1031739 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.834, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.56, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: Why process memory starts so high up in virtual space with FreeBSD malloc? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Dec 2008 22:43:40 -0000 On Mon, 01 Dec 2008 13:28:48 -0800, Yuri wrote: > I am compiling the following program: > > #include > main() { printf("0x%x\n", malloc(1)); } You should probably use printf("%p", ptr) to print pointers :) > in 32-bit 7.1-PRERELEASE and get 0x28201100 which is ~673MB of 4GB > address space or 16%. > > When I run the same program with the google malloc (from > devel/google-perftools) > I get much lower value 0x80aa0e8 which is ~135MB of 4GB address space or > ~3%. > > Why FreeBSD memory allocator wastes such a high percentage of the > memory space? The FreeBSD malloc(3) implementation can use either mmap() or sbrk() to obtain memory from the system. It does not 'waste a high percentage of memory' but it simply maps only high addresses (with an unmapped 'hole' in lower addresses). You can tune FreeBSD malloc(3) to prefer either sbrk() or mmap() by setting the 'D' and 'M' malloc options. The following program prints different initial allocation results when these two malloc() options are configured differently: % cat -n foo.c 1 #include 2 #include 3 4 int 5 main(void) 6 { 7 char *p; 8 9 p = malloc(1); 10 if (p == NULL) 11 return EXIT_FAILURE; 12 13 printf("%p\n", malloc(1)); 14 free(p); 15 return EXIT_SUCCESS; 16 } % For example: % env MALLOC_OPTIONS='Dm' ./foo 0x8101102 % env MALLOC_OPTIONS='dM' ./foo 0x28201102 % More details about the 'D', 'M' and other malloc() options should be available in the manpage for your release. - Giorgos