Subject: bug#49656: more data on #43389

From: Eli Zaretskii <[email-masked]>
Date: Wed, 21 Jul 2021 15:21:40 +0300

> Date: Wed, 21 Jul 2021 07:16:23 +0530 (IST)
> Cc: [email-masked]
> From: Madhu <[email-masked]>
> 
> > It could be if, for example, you are using packages or your own code
> > which plays with GC thresholds.  E.g., I'm told that lsp-mode does
> > that; are you per chance using it?
> 
> No I haven't gotten around to trying that i'm sure none of my packages
> mess with gc-threshold.  I will double check that.

Thanks, please do.  It's important to know that.

> > How much memory and swap do you have there?  Can you enlarge the total
> > VM size (by enlarging swap) so that you could run Emacs longer when it
> > gets to such large sizes?
> 
> The problem is not that emacs won't run.  I am unable to use the
> memory in the machine for other purposes and am obliged to kill emacs.

If you enlarge the swap space, your system will still be workable even
when Emacs has such a large footprint.  That's the purpose of my
suggestion.  It is important to have a usable system when these
situations happen because if we come up with some experiments to
conduct in such cases, you need a usable system to be able to do that.

> > Btw, 1.5GB is not too large for several days worth of work.
> 
> I also have emacs running for several weeks when the problem doesn't
> happen with RES never above say 500M for the same workloads and usage
> patterns.

Then one thing to try to figure out is what was the difference between
these two classes of sessions -- what and how did you do differently
in one that you didn't in the other?  That could point us in the right
direction.

> >  Was the value of gcs-done increasing with time, or did it stay put
> > (which would be an evidence that Emacs doesn't run GC at all)?  If
> > GC was running, how frequently did it run?  (You could answer the
> > last question either by looking at the rate of growth in gcs-done,
> > or by setting garbage-collection-messages non-nil, which will cause
> > Emacs announce ever GC in the echo area.)
> 
> There is nothing exceptional with GC. GC usually completes quickly
> because it doesnt access much memory.

I'd like quantitative measures, please: what is the rate of GC cycles
when the memory footprint is measurd in GBs, and how much time does
each GC cycle take?  Also, does the memory footprint becomes smaller
after a GC cycle, and by how much?

> >> But doesn't the malloc info allocation and the gc reports indicate a
> >> clear discrepancy?
> >
> > Not to me, it doesn't.  It means the malloc arena holds to a lot of
> > memory that it cannot release to the OS, but it is known that this can
> > happen for certain patterns of memory allocation and deallocation.
> > You can find explanations of why this happens on the net.
> 
> But that is the point - I've been asking from the first time I posted
> on this - WHAT is emacs allocating in these exceptional cases.

It's impractical to try to answer these questions.  If you put a
breakpoint on 'malloc' and 'free' and then run Emacs, you will see
that it calls these functions extremely frequently, with widely
different block sizes.  We don't have infrastructure that would record
each allocation and deallocation with enough info to be able to
analyze that, and even if we did, finding the callers which are
responsible for the memory that's not returned to the OS is a very
large and hard job.  We tried that previously, with the help of the
glibc developers using their debugging features -- it didn't really
help us with the diagnostics.

> I understand my usage patterns over the years of constant emacs use
> and the "random" memory bloat in some sessions makes no sense and it
> only suggests a memory leak in emacs code.

It is impossible to look for memory leaks in Emacs without some clues
about where those leaks could be.  If you can provide the information
I asked above, we might be able to come up with such clues.

Alternatively, you could try running Emacs under Valgrind (see
instructions in etc/DEBUG), although this will probably catch memory
leaks only on the C level, not on the Lisp level.

> >> Does it not indicate a leak which the GC is not aware of?
> >
> > No.  Emacs allocates a lot of memory for purposes other than Lisp
> > objects, and that memory is not visible to GC nor to memory-report.
> 
> This is too vague.

But that's all I can say, given the information you provided till now.

> This 2GB active memory is not legitimate and the point is to figure
> out where and why emacs is holding on to this memory.
> 
> This is where I need assistance.

This is me assisting you.  I'm asking you to help us analyze this
problem by providing more information.  I don't think we will be able
to make progress here without that additional info.

> SO what could that be, which it is NOT allocating under identical
> usage patterns?

I don't know, sorry.

> I'm sure others should be hitting the problem too

We could, of course, wait for others to report similar problems, and
then hope the information they provide would point us to the right
direction.  IME, though, this is not a very efficient method, because
in many cases the reasons for the memory growth are not the same.