Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Ask for a more efficient way to deallocate memory (full version)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-12-09 (21:39)
From: Fabrice.Pardo@l...
Subject: Ask for a more efficient way to deallocate memory (full version)
Hello, and sorry for my previous unfinished message.

As the function Unix.opendir returns a value,
we can expect that Unix.closedir will be automatically
called when the dir_handle value is no more referenced.
That's not the case, and calling opendir too many times as in

for i = 1 to 2000 do
  let d = Unix.opendir "/tmp" in ()

Exception: Unix.Unix_error (Unix.EMFILE, "opendir", "/tmp").

Replacing Unix.opendir by safe_opendir:

let safe_opendir path =
  let _ = Gc.major ()
  and d = Unix.opendir path
  in Gc.finalise Unix.closedir d

seems a solution, but the efficiency is very low due to high
cost of garbage collector calling function.

My question is how to write a code as efficient
as it would be possible using a reference-counted language.