[racket] Out of memory

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Apr 4 11:07:05 EDT 2011

On Apr 4, 2011, at 10:10 AM, Pierpaolo Bernardi wrote:

> On Mon, Apr 4, 2011 at 16:04, Pierpaolo Bernardi <olopierpa at gmail.com> wrote:
>> The function looks fine to me. So, what is the problem?
> I mean, fine as it shouldn't run out of memory.
> I am aware that the function is not testing what I wanted to test, but
> that's not the point :)

Here is a bit of experimentation. I separated the program into two pieces: 
 -- the creation of the vector 
 -- processing the vector 

I noticed that the first stage always finishes. Good. 
So I replaced the second stage with writing the vector to a file. Bad. 
  It fails. Even the built-in 'write' function just fails and writes 
  the vector only partially. 

I am out of time, but I suspect we can recreate this problem for 
any vector that consumes as much space as you asked for (10,000,000 
vector slots), which is probably close to half the 128Mb (each slot 
make take 4 bytes). 

What I don't understand is that it succeeds on rare occasions, 
because I don't see ANY allocation for CONTROL space and so if
it succeeds once, why not always? 

#lang racket

(define *v (vector))

(define (test-1 n exp)
  (let ((v (make-vector n)))
    (for ((i (in-range n)))
      (vector-set! v i (expt (random) exp)))
    (set! *v v))

(define (test-2 n)
  (define v *v)
  (let sum ((i 0) (all 0.0) (pos 0.0) (neg 0.0))
    (displayln `(,all ,pos ,neg))
    (if (= i n)
        (values all (+ pos neg))
        (let ((item (vector-ref v i)))
          (if (positive? item)
              (sum (+ i 1) (+ all item) (+ pos item) neg)
              (sum (+ i 1) (+ all item) pos (+ neg item)))))))

(test-1 10000000 -1)
(with-output-to-file "bug-test.rkt"
  (lambda () (write *v))
  #:exists 'replace)

; (test-2 10000000)

Posted on the users mailing list.