At Tue, 15 Dec 2020 17:00:42 -0800 (PST), Alex Harsanyi wrote:
> I am trying to use `(current-process-memory 'cumulative)` to determine the
> total memory used by an application, including the memory that was
> reclaimed by the garbage collector. I would expect the results from the
> call to be constantly increasing, and this is indeed the case at a "global"
> scale: [...]
>
> However, occasionally, there seems to be a drop in the reported memory use,
I'm able to replicate that with
#lang racket
(define (work n)
(length (let loop ([n n])
(if (zero? n)
'()
(cons (make-vector n) (loop (sub1 n)))))))
(let loop ([u (current-memory-use 'cumulative)])
(work (random 1000))
(let ([u2 (current-memory-use 'cumulative)])
(if (u2 . < . u)
(error "oops")
(loop u2))))
which reports an error fairly quickly for me.
The implementation of
(current-process-memory 'cumulative)
is
(+ (bytes-deallocated) (bytes-allocated))
It's possible for a GC to happen at the start of the call to
`bytes-allocated`, which I think would produced the blip you see (i.e.,
there could be bytes not yet attributed to `bytes-deallocated`, but
they get shifted there before `bytes-allocated` runs).
The solution is make that arithmetic atomic with respect to a GC by
disabling signals around the arithmetic. I've pushed that change, and
my test program doesn't error anymore, at least.
Matthew