Skip to content

Instantly share code, notes, and snippets.

@bobbysmith007
Created May 30, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbysmith007/8dd2da4483d32ab0d02d334f8b81f1bc to your computer and use it in GitHub Desktop.
Save bobbysmith007/8dd2da4483d32ab0d02d334f8b81f1bc to your computer and use it in GitHub Desktop.
If the Heap size exceeds a certain limit, garbage collect till you are below it again
(defun current-heap-size-megs (&key (spaces '(:static :dynamic :read-only))
&aux (total-mem 0) (total-cnt 0))
(iter (for s in spaces)
(iter (for (mem cnt type) in (sb-vm::type-breakdown s))
(incf total-mem mem)
(incf total-cnt cnt)))
(values (ceiling (/ total-mem (expt 2 20)))
total-cnt))
(defun gc-till-heap-available ( &key
(heap-size-max 500)
(sleep-time .01) (limit 5)
log?
&aux (cnt 0) megs)
(iter
(setf megs (current-heap-size-megs))
(while (> megs heap-size-max))
;; while debugging I am going to turn this off, but
;; this seems to be the reason
;; (function-cache:clear-cache-all-function-caches)
(incf cnt)
(when (= limit 3)
(adwolf-log.error "Had to clear all caches to free space so we can continue")
(function-cache:clear-cache-all-function-caches))
(unless (plusp (decf limit))
(error "Couldnt collect enough garbage to continue full-validation loop"))
(sb-ext:gc :full t)
(sleep sleep-time))
(when log?
(adwolf-log.info "GC'ed ~A times resulting in a ~DM heap " cnt megs))
(values cnt megs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment