From owner-freebsd-current@FreeBSD.ORG Wed Jun 14 17:35:24 2006 Return-Path: X-Original-To: freebsd-current@freebsd.org Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7B1B216A479; Wed, 14 Jun 2006 17:35:24 +0000 (UTC) (envelope-from jasone@FreeBSD.org) Received: from lh.synack.net (lh.synack.net [204.152.188.37]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2860B43D6B; Wed, 14 Jun 2006 17:35:24 +0000 (GMT) (envelope-from jasone@FreeBSD.org) Received: by lh.synack.net (Postfix, from userid 100) id E81355E4926; Wed, 14 Jun 2006 10:35:23 -0700 (PDT) Received: from [192.168.150.128] (unknown [129.101.159.83]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by lh.synack.net (Postfix) with ESMTP id 533EC5E4906; Wed, 14 Jun 2006 10:35:22 -0700 (PDT) Message-ID: <449048C7.6090109@FreeBSD.org> Date: Wed, 14 Jun 2006 10:35:03 -0700 From: Jason Evans User-Agent: Mozilla Thunderbird 1.0.8-1.4.1 (X11/20060420) X-Accept-Language: en-us, en MIME-Version: 1.0 To: John Baldwin References: <448FC3AF.9060606@bulinfo.net> <200606141023.51185.jhb@freebsd.org> In-Reply-To: <200606141023.51185.jhb@freebsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Checker-Version: SpamAssassin 3.0.6 (2005-12-07) on lh.synack.net X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=failed version=3.0.6 Cc: freebsd-current@freebsd.org, jasone@freebsd.org, Krassimir Slavchev Subject: Re: memory leak in free() X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Jun 2006 17:35:24 -0000 John Baldwin wrote: > On Wednesday 14 June 2006 04:07, Krassimir Slavchev wrote: > >>Hello, >> >>This simple code demonstrates the problem: >> >>int main () >>{ >> char* buffer1; >> char* buffer2; >> int size = 2*1024*1024 + 1; >> >>for(;;) { >> buffer1 = (char *) malloc(size); >> buffer2 = (char *) malloc(size); >> >> free(buffer1); >> free(buffer2); >> } >>} >> >>The second free() does not free allocated memory if size >2Mb. >> >>On 6.1-STABLE all is OK. > > > This is probably an issue with jemalloc, I've cc'd jasone@ who wrote the > new malloc() in HEAD. > This is on a 32-bit system, right? If so, what's happening is that the brk-managed space (data segment) is being fragmented, such that the address space isn't returned to the OS. However, this is not really a memory leak, since madvise() is called in order to let the kernel know that the unused space need not be swapped out. I was reluctant to allow allocations > 1MB to be carved from brk because I knew this could happen, but people complained about it, so I added the feature. In practice, I think the current implementation makes the right tradeoff, but I have no strong feelings on the matter. If this is causing you particular problems for some application, a simple way to work around it is to decrease the data segment size for the application. That will cause most/all allocations to be carved from memory via mmap() instead. Incidentally, this isn't an issue on 64-bit systems, since only mmap() is used to request memory from the kernel. Jason