Will there be a shaded GCed binding?

Suppose I have this in a long ml file:

let l = [1;2;3]

let l = [1;2;3;4]

let _ = ...

Will the first l = [1;2;3]be gCed ever?


What to do if the code looks like this:

let l = [1;2;3]

let l = [1;2;3;4]

let l = [1;2;3]

let _ = ...

There are three l. 1st is obscured by 2nd, and then 2nd is obscured by 3rd.

Are the following situations possible since the GC schedule is not defined?

  • when reaching the third lGC did not collect the 1st [1;2;3], therefore the same memory was reused or re-indicated

  • Immediately after the second lGC assembled the 1st [1;2;3], then the third lwill create a new memory for[1;2;3]

+4
source share
2 answers

, - l , , GCed.


ocaml; list .

"" ( , ).

, , , , ; , , .

:

  • -
  • , , / , . , , - / .

@PascalCuoq :

, . OCaml (?) - GCed, .

, , "l", .

+3

OCaml toplevel, l l, ( ) . , , , , .

$ rlwrap ocaml
        OCaml version 4.00.1

# let l = [ 1 ] ;;
val l : int list = [1]
# let w = Weak.create 1 ;;
val w : '_a Weak.t = <abstr>
# Weak.set w 0 (Some l) ;;
- : unit = ()
# Gc.full_major () ;;
- : unit = ()
# Weak.check w 0 ;;
- : bool = true
# 

true , l .

# let l = [ 2 ] ;;
val l : int list = [2]
# Weak.check w 0 ;;
- : bool = true
# Gc.full_major () ;;
- : unit = ()
# Weak.check w 0 ;;
- : bool = true
# 

, "" ( , GC).

l:

$ cat t.ml
let l = [ 1 ] ;;
let w = Weak.create 1 ;;
Weak.set w 0 (Some l) ;;
Gc.full_major () ;;
Printf.printf "%B\n" (Weak.check w 0) ;;
let l = [ 2 ] ;;
Printf.printf "%B\n" (Weak.check w 0) ;;
Gc.full_major () ;;
Printf.printf "%B\n" (Weak.check w 0) ;;
$ ocamlc t.ml
$ ./a.out 
true
true
true
$ ocamlopt t.ml
$ ./a.out 
true
true
true

GC "", , , :

let g () = Gc.full_major ()

let f () = let l = [ 1 ] in (* do something with l; *) g(); 1

, g ( f), l ( ) . , - . GC, , , f .

+4

Source: https://habr.com/ru/post/1533410/


All Articles