From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#45200: [PATCH] Force Glibc to free the memory freed Date: Wed, 03 Feb 2021 17:07:04 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33952"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: fweimer@redhat.com, carlos@redhat.com, hi-angel@yandex.ru, 45200@debbugs.gnu.org To: DJ Delorie Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Feb 03 23:37:10 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7Qlp-0008iU-J3 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 03 Feb 2021 23:37:09 +0100 Original-Received: from localhost ([::1]:34202 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l7Qlo-0003Ut-L6 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 03 Feb 2021 17:37:08 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55064) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7QJg-0001aH-C2 for bug-gnu-emacs@gnu.org; Wed, 03 Feb 2021 17:08:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:55752) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l7QJe-0004Lw-BM for bug-gnu-emacs@gnu.org; Wed, 03 Feb 2021 17:08:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l7QJe-0005ss-3s for bug-gnu-emacs@gnu.org; Wed, 03 Feb 2021 17:08:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 03 Feb 2021 22:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45200 X-GNU-PR-Package: emacs Original-Received: via spool by 45200-submit@debbugs.gnu.org id=B45200.161239003522567 (code B ref 45200); Wed, 03 Feb 2021 22:08:02 +0000 Original-Received: (at 45200) by debbugs.gnu.org; 3 Feb 2021 22:07:15 +0000 Original-Received: from localhost ([127.0.0.1]:39065 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l7QIs-0005ru-RP for submit@debbugs.gnu.org; Wed, 03 Feb 2021 17:07:15 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:37696) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l7QIq-0005rh-NH for 45200@debbugs.gnu.org; Wed, 03 Feb 2021 17:07:13 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 7443380D8E; Wed, 3 Feb 2021 17:07:07 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id B27598050D; Wed, 3 Feb 2021 17:07:05 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1612390025; bh=5vsSPTJktnPs0QpmeT1J7GjOhcDat+1BLPM2j4T7RJ0=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=Gl4o/sg8i6E096/NobNVQopVr+bTC6ubepBO2780VZ70JxJxw0MxMqgf37oA67zEO 0l1vtBVaXAfnHWhE7/jvhhcA0Gao/pAaXJbQ3MrbWDvhogz2w9PQYNW7tWkDWbajj/ 9CGaElXcWCNoyLNQkWGHLo5O5Z5HOlEjY9Z5N5dRgAD/5Zp2uqls1BX51zo5oJOftG Kt3jrGkSCySElU983Kh+oogeGEc4eWugiwDL7xGVQnNnP7stvgCOfVDQmt7F0ZqDe7 iirW0E3yoY0fXjbv9R7s1ewHsWlmOsYGKZAr/IShcT5OJ0tFVmyJIG7NoukgK1LWeB iHE15dT9G0Apw== Original-Received: from alfajor (76-10-182-85.dsl.teksavvy.com [76.10.182.85]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 65354120218; Wed, 3 Feb 2021 17:07:05 -0500 (EST) In-Reply-To: (DJ Delorie's message of "Wed, 03 Feb 2021 15:42:11 -0500") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:199251 Archived-At: >> I understand that as well. But I'm wondering why glibc is willing to >> keep *indefinitely* an unused 200MB of memory, which is more than double >> the mount of memory in use for the rest of the application's life. > To be blunt, 200Mb is peanuts compared to some applications, and it's I'm not worried about the absolute value, but about the proportion. I think in memory management code, having an overall overhead of 50% is generally considered acceptable (i.e. actual memory allocated is twice the memory used by the application), whether that comes from internal&external fragmentation or a stop© GC, ... But in our specific use case, there seems to be no limit to the overhead: if the application uses a heap of size N at some point in time it will never grown back down, so the overhead can end up being arbitrarily large. > *nothing* compared to an enterprise application. Keeping 200M around to > quickly satisfy memory requests of various sizes (not all cached chunks > are the same size) is IMHO reasonable. If the average allocation/deallocation rate justifies it, I fully agree. But if the variation of allocated space stays well below that for a long time, then those 200MB are truly wasted. >> I mean I understand that you can't predict the future, but I expected >> that "at some point" glibc should decide that those 200MB have been left >> unused for long enough that they deserve to be returned to the OS. > Where will we store that lifetime information? I haven't thought very much about it, so I'm sure it's easy to shoot holes through it, but I imagined something like: - one `static unsigned long hoard_size` keeps the approximate amount of space that is free but not returned to the OS. Not sure where/when to keep it up to date cheaply, admittedly. - one `static unsigned long smallest_recent_hoard_size`. This is updated whenever we allocate memory from the OS. - one `static unsigned long age_of_smallest_recent_hoard_size`. This is incremented every time we allocate memory from the OS (and reset whenever the value of smallest_recent_hoard_size is modified). Then you'd call `malloc_trim` based on a magic formula combining `age_of_smallest_recent_hoard_size` and the ratio of `smallest_recent_hoard_size / total_heap_size` (and you'd trim only what's necessary to release O(`smallest_recent_hoard_size`) memory). > Yet another word of memory used, Since 200MB is peanuts, I figure that extra 24B should be acceptable ;-) > yet another syscall to check the time? I didn't mean time as in an OS-notion of clock, no. > I agree that we could do better at detecting long-unused chunks, but > it's expensive (in terms of both development and runtime) to do so, and > typically at the expense of some other desired metric. No doubt. > I would ask the Emacs devs why they wait until gc to free() memory > instead of keeping track of uses more accurately and free()ing it > right away. It's a similar type of compromise. Delaying for some time is one thing. Delaying forever is another. >> The doc of `malloc_trim` suggests it's occasionally called by `free` and >> `mallopt` suggests via `M_TRIM_THRESHOLD` that there's a limit to how >> much extra spare memory glibc keeps around, so this suggests that indeed >> memory is trimmed "every once in a while". > Only when the available memory is "at the top of the heap". Ah, I see, that makes sense. I do remember such behavior in other/older libc libraries. > We used to have code that munmap()'d large "holes" in the cache, That's what I seem to remember, indeed. And our memory management code does play with `mallopt` in the hopes to encourage it to allocate using `mmap` in the hopes that it then deallocates via `munmap`. > but the resulting performance was horrible. Hmm... so that explains why we're seeing those problems again. Stefan