[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory checking functions



At 14:32 11/25/92 +0000, matti_karjalainen@hut.fi wrote:
>David S. Bright and Lawrence Au discussed about memory checking functions.
>Thanks for the (%freebytes).
>
>Now my question: 
>
>Is there any way to automatically check the free Lisp heap size just after
>GC. I am using an object database where my program may continue to load
>objects until it is out of Lisp heap. I would like to define a function to
>be called from Lisp after GC so that my program may purge some objects if
>Lisp heap is too low.

MCL has two undocumented hooks for the GC.

ccl::*pre-gc-hook*
  If set to a function, will be called with no arguments soon
  after there is only 8K of memory left to be consed until the GC
  runs.

ccl::*post-gc-hook*
  If set to a function, will be called with no arguments soon
  after the GC runs.

"soon after" means that the hooks will be called the next time
MCL's periodic task dispatcher is called. This is once per tick
(1/60 second) unless that's not possible because your code is running
WITHOUT-INTERRUPTS.

Note that since these hooks are undocumented, they may disappear in a
future version of MCL.

Example:

; This threshold needs to be large enough that your code cannot
; cons this much in one tick.
(defparameter *min-freebytes* 100000)

(defvar *data* nil)

(defun maybe-purge-some-objects ()
  (let ((memory-needed (- *min-freebytes* (ccl::%freebytes))))
    (when (> memory-needed 0)
      (purge-some-objects memory-needed))))

(defun purge-some-objects (bytes-needed)
  (declare (ignore bytes-needed))
  (setq *data* nil))

(setq ccl::*post-gc-hook* #'maybe-purge-some-objects)

(defun cons-like-mad ()
  (loop
    (push (1+ (or (car *data*) 0)) *data*)))

; So that we can see the *post-gc-hook* in action.
(trace purge-some-objects)

; This does not get an out of memory error because purge-some-objects
; occasionally sets *data* to nil.
(cons-like-mad)

; Clean up
(setq *data* nil)