Created
December 18, 2019 08:58
-
-
Save Metaxal/190099a09021baa484781102574d3ea0 to your computer and use it in GitHub Desktop.
The limits of with-limits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require racket/sandbox) | |
;; Make sure you have at least 4×max-memory available | |
;; otherwise your computer might freeze. | |
(define max-memory 1024) | |
(define a-tree #f) | |
;; with-limits should limit the memory use, but it fails in this case. | |
;; See below for an explanation. | |
(with-limits | |
#f max-memory | |
(let loop ([tree '()] [idx 0]) | |
; Memory check | |
(define mem (current-memory-use)) | |
(when (> mem | |
(* 4 1024 1024 max-memory)) | |
(error "Reached 4×max-memory. with-limits check failed." mem)) | |
; Eat up memory quickly | |
(set! a-tree tree) | |
#;(set! a-tree (list (length tree))) ; alternative to the line above | |
(loop (list (build-list (sqr idx) (λ(i)(+ idx i))) | |
(make-list (sqr idx) tree)) | |
(+ idx 1)))) | |
(displayln "with-limits broke the loop.") | |
(displayln (length a-tree)) | |
#| Explanation by Matthew Flatt: | |
"The tree program above just cannot work like you want. | |
Accounting is based on reachability (not on, say, allocation time), | |
and only those values reachable specifically from the sandbox will | |
be charged to the sandbox." | |
That is, the tree build inside the loop is reachable outside the sandbox | |
as it is stored in `a-tree`; thus it is not counted by `with-limits`. | |
Replacing the line | |
(set! a-tree tree) | |
with | |
(set! a-tree (list (length tree))) | |
ensures that `tree` is counted by `with-limits`, but it may still use | |
more than max-memory memory space. | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment