Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 ()
done;;

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

Thanks.