[plt-scheme] current-memory-use returning a negative number

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Mon Jul 30 10:58:38 EDT 2007

Hi all,

I've noticed some odd behaviour (negative return values) from current- 
memory-use running MzScheme v370.6 on Debian. It looks like an  
overflow at around (* 1024 1024 1024) bytes. Is this expected  
behaviour, or might there be a bug?

I've included code and debugging output below.

Many thanks,

-- Dave

I am stress testing a web application: hammering the server to make  
sure I've configured lru.ss appropriately. The application is  
configured to start dumping continuations when memory use exceeds (*  
1024 1024 1024) bytes:

   ;; continuation-manager : LRU-manager
   ;; An continuation is allowed to live a maximum of 6 hours
   ;; (30 minutes x 12), and a minimum of 1 minute (5 seconds
   ;; x 12).  Lifetime is controlled by memory usage.  Usage
   ;; above memory-threshold will result is more rapid death.
   (define continuation-manager
     (let ([memory-threshold (* 1024 1024 1024)]) ; in bytes
        ;; Called when an instance has expired.
        ;; The condition below is checked every 5 seconds
        ;; One 'life point' is deducted every 30 minutes
        (* 30 60)
        ;; If this condition is true a 'life point' is deducted from  
the continuation
        (lambda ()
          (let* ([memory-use (current-memory-use)]
                 [collect?   (>= memory-use memory-threshold)])
            (log-warning "MEMORY USE"
        ;; The number of 'life points' an continuation starts with
        #:initial-count 12
        ;; Logging done whenever an continuation is collected
        #:inform-p (lambda args (log-warning "COLLECTED" args)))))

Everything works as expected until memory use gets to about the 1  
gigabyte mark. At that point, current-memory-use starts returning  
negative values:

   ; Start the app (1 line of output every 5 seconds) ...

   W,"MEMORY USE",33112328,1073741824,#f
   W,"MEMORY USE",34848800,1073741824,#f
   W,"MEMORY USE",34852516,1073741824,#f

   ; ... skip forward 5 mins or so, with current-memory-use  
increasing ...

   W,"MEMORY USE",1072749848,1073741824,#f
   W,"MEMORY USE",1057830404,1073741824,#f
   W,"MEMORY USE",1063427456,1073741824,#f
   W,"MEMORY USE",-1062537120,1073741824,#f
   W,"MEMORY USE",-1068958460,1073741824,#f
   W,"MEMORY USE",-1058235684,1073741824,#f

   ; ... current-memory-use remains negative and starts approaching  
zero, until ...

   W,"MEMORY USE",-502924500,1073741824,#f
   W,"MEMORY USE",-513441012,1073741824,#f
   W,"MEMORY USE",-507992488,1073741824,#f
   The system has run out of memory!

I've temporarily fixed my code by changing the expression for  
"collect?" to:

   (or (>= memory-use memory-threshold)
       (< memory-use 0))

To clarify these results, the web application has been running for a  
long time without problems: it normally manages its memory just fine.  
We're only seeing this behaviour now because we're testing to failure.

Posted on the users mailing list.