Specify memory limit in Racket
Asked Answered
T

2

5

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:

  • at the beginning of the code call some function to set the memory limit
  • when invoking racket (not DrRacket GUI tool, but the REPL on command line) give it some arguments to specify the limit
Tokharian answered 22/8, 2016 at 13:1 Comment(0)
A
7

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.

Ausgleich answered 22/8, 2016 at 19:7 Comment(1)
Both answers are great, however, I'll accept this one, because I like the define of max bytes. Very readable.Tokharian
D
11

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'.

Derouen answered 22/8, 2016 at 19:7 Comment(0)
A
7

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.

Ausgleich answered 22/8, 2016 at 19:7 Comment(1)
Both answers are great, however, I'll accept this one, because I like the define of max bytes. Very readable.Tokharian

© 2022 - 2024 — McMap. All rights reserved.