Racketには評価時のリソースの利用を制限するサンドボックス機能があります。
使用可能なメモリを制限して式を評価するプログラムを書いてみます。
#lang racket
(require racket/sandbox)
(define e (parameterize ((sandbox-memory-limit 1)) ;; 1MByte
(make-evaluator 'racket/base)))
;; 1KByteくらい (1MByte制限)
(printf "~s\n" (vector-length (e '(make-vector 1000))))
(flush-output)
;; 1MByteくらい (制限なし)
(printf "~s\n" (vector-length (make-vector 1000000)))
(flush-output)
;; 1MByteくらい (1MByte制限)
(printf "~s\n" (vector-length (e '(make-vector 1000000))))
(flush-output)
> racket test.rkt 1000 1000000 out of memory context...: /usr/racket/collects/racket/sandbox.rkt:355:0: call-with-limits /usr/racket/collects/racket/sandbox.rkt:403:0: call-with-custodian-shutdown /usr/racket/collects/racket/private/more-scheme.rkt:146:2: call-with-break-parameterization /usr/racket/collects/racket/sandbox.rkt:760:5: loop
0 件のコメント:
コメントを投稿