From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Ask for a more efficient way to deallocate memory (full version)
Date: Sun, 9 Dec 2007 22:57:34 +0100 [thread overview]
Message-ID: <1197237454.475c64cec866b@webmail.in-berlin.de> (raw)
In-Reply-To: <39980.81.57.198.61.1197236388.squirrel@webmail.lpn.cnrs.fr>
Hello,
Zitat von Fabrice.Pardo@lpn.cnrs.fr:
> 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,
Yes, and that's a operating system issue, not OCaml's.
There btw. a long while ago was already a discussion on that.
opendir does only give back a handle, and that does not need much
memory. So it is not a problem for that you should use the Gc-module.
the error you got says: there are not more filedescriptors available to
you, and the system gives you an error because of that and OCaml then
throsws you an exception.
You have to open and to close by yourself.
There are many possiblities to do that work.
You can code it directly in a function.
So, you could open the directory, read the conetnst to alist,
then close the directory and give back the dircontens (list
of entries) as a return value.
The calling function then can collect the items.
Or give a list of dierctories to the function and
let it fo all opendir/readdir/closedir things,
collecting all dir-contents.
So, this all is possible without Gc-module, and using the
Gc-module shows me that you don't understand the rpoblem at hand: it is
not the garbage collector that must be used to throw away allocated
data, it is the Operating-System, which needs the files to be closed.
As it is not the Garbage Collector, it makes no sense to use
Gc-functions here.
Just open the dir, read the contents and then close the dir.
That's all.
> and calling opendir too many times as in
>
> for i = 1 to 2000 do
> let d = Unix.opendir "/tmp" in ()
> done;;
This makles no sense.
Why to open directories and not use them?
Do you go to the bakery, buy 2000 breads and then
put them in your garage, not eating one of them?
[...]
>
> 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.
So, don't use it.
>
> My question is how to write a code as efficient
> as it would be possible using a reference-counted language.
[...]
So, it seems you think OCaml is very inefficient and so you try
to make coding "as efficient as possible" before you write any line of
code. This makes no sense. OCaml is astouningly efficient,
and you should better start to write your code and only if you
come to a limit, where you really think you have to optimize,
then you should.
So, do you really want to open some-thousand directories?
Then you can write a function, that gets a list of directories as
argument and then in a loop
opens --- reads (and daves data) --- closes
No Gc-stuff needed.
Ciao,
Oliver
next prev parent reply other threads:[~2007-12-09 21:57 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-09 21:39 Fabrice.Pardo
2007-12-09 21:55 ` [Caml-list] " Olivier Andrieu
2007-12-10 11:25 ` Fabrice Pardo
2007-12-10 12:03 ` Michaël Le Barbier
2007-12-10 16:33 ` Oliver Bandel
2007-12-10 20:27 ` Richard Jones
2007-12-10 21:05 ` Oliver Bandel
2007-12-10 21:15 ` Gordon Henriksen
2007-12-10 22:13 ` Oliver Bandel
2007-12-10 22:59 ` Jon Harrop
2007-12-10 23:29 ` Jon Harrop
2007-12-11 2:03 ` Yitzhak Mandelbaum
2007-12-15 21:33 ` Oliver Bandel
2007-12-16 15:14 ` Jon Harrop
2007-12-10 23:24 ` Jon Harrop
2007-12-09 21:57 ` Oliver Bandel [this message]
2007-12-09 22:12 ` Jon Harrop
2007-12-09 22:34 ` Oliver Bandel
2007-12-09 22:16 ` Oliver Bandel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1197237454.475c64cec866b@webmail.in-berlin.de \
--to=oliver@first.in-berlin.de \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox