Zelphir Kaltstahl
Zelphir Kaltstahl

Reputation: 6189

Specify memory limit in Racket

In DrRacket I can set a memory limit using the GUI. However, I find the editor not sooo good and want to use another editor of my choice. But how do I specify the memory limit then?

I can think of two possibilities, but couldn't find anything about either of these:

Upvotes: 5

Views: 2390

Answers (2)

Dmitry
Dmitry

Reputation: 844

See custodians.

For example:

#lang racket

;; Set limit
(custodian-limit-memory
   (current-custodian) (* 2 1024 1024))

(define x (make-bytes (* 4 1024 1024)))

Result of this code is 'out of memory'.

Upvotes: 11

Ben Greenman
Ben Greenman

Reputation: 2002

At the beginning of the code, you can set a memory limit for the module. (docs)

#lang racket/base
(define MAX-BYTES 1000)
(custodian-limit-memory (current-custodian) MAX-BYTES)

....

I don't know a straightforward command-line solution, but you can call custodian-limit-memory in your racketrc file to set a limit for the REPL.

See also racket/sandbox, especially call-with-limits.

Upvotes: 7

Related Questions