






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of memory management and garbage collection in ocaml, focusing on the implementation of stacks using modules and closures. It also discusses the relationship between objects and closures, encoding objects with functions, and memory classes. The document also touches upon memory management in other programming languages such as c, ruby, and java.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







CMSC 330 2
CMSC 330 3
class Stack { class Node { Integer val; Node next; Node(Integer v, Node n) { val = v; next = n; } }; private Node theStack; void push(Integer v) { theStack = new Node(v, theStack); } Integer pop() { if (theStack == null) throw new NoSuchElementException(); Integer temp = theStack.val; theStack = theStack.next; return temp; } } CMSC 330 4
module type STACK = sig type 'a stack val new_stack : unit -> 'a stack val push : 'a stack -> 'a -> unit val pop : 'a stack -> 'a end module Stack : STACK = struct type 'a stack = 'a list ref let new_stack () = ref [] let push s x = s := (x::!s) let pop s = match !s with [] -> failwith "Empty stack" | (h::t) -> s := t; h end
CMSC 330 5
let new_stack () = let this = ref [] in let push x = this := (x::!this) and pop () = match !this with [] -> failwith "Empty stack" | (h::t) -> this := t; h in (push, pop)
**# let s = new_stack ();; val s : ('_a -> unit) * (unit -> '_a) = (, )
**- : unit = ()
CMSC 330 6
CMSC 330 7
CMSC 330 8
class C { int x = 0; void set_x(int y) { x = y; } int get_x() { return x; } }
let make () = let x = ref 0 in ( (fun y -> x := y), (fun () -> !x) )
x = 0
C c = new C(); c.set_x(3); int y = c.get_x();
x = ref 0
fun y -> x := y fun () -> !x let (set, get) = make ();; set 3;; let y = get ();;
CMSC 330 9
class C { f1 ... fn; m1 ... mn; }
let make () = let f1 = ... ... and fn = ... in ( fun ... , ( body of m1 ) ... fun ..., ( body of mn ) )
Tuple containing closures
CMSC 330 10
let rec map f = function [] -> [] | (h::t) -> (f h)::(map f t)
CMSC 330 11
public interface Function { Integer eval(Integer arg); }
CMSC 330 12
class MultTwo implements Function { Integer eval(Integer arg) { return new Integer(arg * 2); } }
CMSC 330 20
CMSC 330 21
CMSC 330 22
CMSC 330 23
CMSC 330 24
CMSC 330 25
CMSC 330 26
**{ int x = (int ) malloc(sizeof(int)); }
**{ int x = ...malloc(); free(x); x = 5; / oops! / }
*{ int x = ...malloc(); free(x); free(x); } ¾ This may corrupt the memory management data structures
CMSC 330 27
CMSC 330 28
CMSC 330 29
CMSC 330 30
CMSC 330 37
CMSC 330 38
stack 1
CMSC 330 39
stack 1
CMSC 330 40
stack 1
CMSC 330 41
stack 1
CMSC 330 42
stack 1 2
CMSC 330 43
stack 1 2
CMSC 330 44
stack
CMSC 330 45
CMSC 330 46
CMSC 330 47
stack
CMSC 330 48
stack
CMSC 330 55
CMSC 330 56
CMSC 330 57
stack
CMSC 330 58
stack
①
CMSC 330 59
stack
①
CMSC 330 60
stack
①
CMSC 330 61
CMSC 330 62
Object lifetime increases ⇒
More objects live
“Young objects die quickly; old objects keep living”
CMSC 330 63
CMSC 330 64
CMSC 330 65
CMSC 330 66