public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Patrick McGehearty <patrick.mcgehearty@oracle.com>
To: Christian Hoff <christian_hoff@gmx.net>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Carlos O'Donell <carlos@redhat.com>,
	Konstantin Kharlamov <hi-angel@yandex.ru>,
	libc-help@sourceware.org
Subject: Re: Excessive memory consumption when using malloc()
Date: Mon, 29 Nov 2021 11:06:54 -0600	[thread overview]
Message-ID: <98938444-917a-5027-4d43-061e958b21a3@oracle.com> (raw)
In-Reply-To: <3f88fb7c-672a-da09-89d7-240e9991369d@gmx.net>

I wonder if a useful heuristic would be for under some
circumstances for the malloc library to call malloc_trim()
every so often, based on passage of time in addition to
the current calls to malloc_trim() based on user calls to free().

If free memory is cached but unused for a 'substantial'
time while the program is doing 'other' things, it gets
freed for other use by the OS. Setting the default to
a time is large enough to avoid excess syscall overhead
while being below typical human perception would be right
for most contexts.

This option could be under tunable control (at least
until we understand better heuristics) with two
variables:
1) A variable that says yes/no we will call malloc_trim()
based on time. (Initial default: NO)
2) A variable that specifies the time frequency.
(Initial default: 1/10 second or reasonable approximation)

This approach avoids the need to have malloc_trim()
or calls to other malloc internals appear in user
applications.

I have not made any investigation of implementation
details. If anyone wants to take this idea and run
with it, they are welcome to do so.

- Patrick McGehearty


On 11/26/2021 12:10 PM, Christian Hoff via Libc-help wrote:
> Hello Adhemerval,
>
> On 11/25/21 9:56 PM, Adhemerval Zanella wrote:
>> What I think we might improve is to maybe add an heuristic to call
>> malloc_trim once a certain level of fragmentation in the main_arena 
>> is found.
>> The question is which metric and threshold to use.  The trimming does 
>> have
>> a cost, however I think it worth to decrease fragmentation and memory 
>> utilization.
>
> Yes, I think this is a very good idea. It is difficult for us to tell
> our customers that our application consumes so much memory even while it
> is not running any computations. Of course, we can call malloc_trim() as
> a workaround, but even this is difficult as this is a Java application,
> so calling system functions is not so straightforward. Any glibc
> improvements in this area would be highly welcome.
>
>
> Best regards,
>
>    Christian
>


  reply	other threads:[~2021-11-29 17:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-25 17:20 Christian Hoff
2021-11-25 17:46 ` Konstantin Kharlamov
2021-11-25 18:12   ` Konstantin Kharlamov
2021-11-25 18:21     ` Carlos O'Donell
2021-11-25 20:56       ` Adhemerval Zanella
2021-11-26 18:10         ` Christian Hoff
2021-11-29 17:06           ` Patrick McGehearty [this message]
2021-11-25 18:20 ` Carlos O'Donell
2021-11-26 17:58   ` Christian Hoff
2021-11-29 19:44     ` Christian Hoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98938444-917a-5027-4d43-061e958b21a3@oracle.com \
    --to=patrick.mcgehearty@oracle.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=christian_hoff@gmx.net \
    --cc=hi-angel@yandex.ru \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).