caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* memory leak in C snippet?
@ 2004-12-28 19:32 Lodewijk Voge
  2004-12-31  9:35 ` [Caml-list] " Matthieu Brucher
  0 siblings, 1 reply; 5+ messages in thread
From: Lodewijk Voge @ 2004-12-28 19:32 UTC (permalink / raw)
  To: caml-list

hello,

I'm writing a daemon in ocaml with some glue code in C. it all works 
fine, except memory seems to leak to a point the daemon can last
only about a week on the smallest systems. after a lot of removing code 
I come to this snippet that seems to leak:

external bar: unit -> Unix.inet_addr * unit = "bar"

let _ =
         let s = String.create 392 in
         while true do
                 ignore(bar ());
         done

with

CAMLprim value bar(value unit) {
         CAMLparam1(unit);
         CAMLlocal2(res, addr);
         int i;

         addr = alloc_string(4);
         *(int *)(String_val(addr)) = htonl(0xac100000);
         res = alloc_tuple(2);
         Store_field(res, 0, addr);
         Store_field(res, 1, Val_unit);
         CAMLreturn(res);
}

which gives when run:

~/test$ while /usr/bin/true; do ps -o rss -p 15696 | tail -1; sleep 2; 
done
    784
    784
    788
    792
    796
    800
    804
    808
    812
^C

an ever growing RSS. strangely, having bar() return just the alloced 
string instead of a tuple, this doesn't occur. even more strangely, if 
I delete the let s = String.make 392 line, it doesn't occur either. 
systems tested are MacOS X, FreeBSD (the target system) and Linux. as 
the RSS approaches the segment size that'll expand too, and this 
gobbles up all available virtual memory in a week on the smallest 
target systems.

so, my question is: can anyone see an obvious error in the C snippet? 
some violation of the rules in the manual I missed?

thanks,
Lodewijk


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] memory leak in C snippet?
  2004-12-28 19:32 memory leak in C snippet? Lodewijk Voge
@ 2004-12-31  9:35 ` Matthieu Brucher
  2004-12-31 11:14   ` Lodewijk Voge
  2004-12-31 15:10   ` Jon Harrop
  0 siblings, 2 replies; 5+ messages in thread
From: Matthieu Brucher @ 2004-12-31  9:35 UTC (permalink / raw)
  Cc: caml-list

Hello

I'm not very into Ocaml at the moment, so perhaps what I will say isn't 
relevant, but you seem to allocate 2 memory blocs in your function 
(alloc_string and alloc_tuple), but where do you free this memory ? Does 
OCaml take charge of this or do you still have to do it yourself as it 
is in a C function ?
Happy new year.

Matthieu

Lodewijk Voge wrote:

> hello,
>
> I'm writing a daemon in ocaml with some glue code in C. it all works 
> fine, except memory seems to leak to a point the daemon can last
> only about a week on the smallest systems. after a lot of removing 
> code I come to this snippet that seems to leak:
>
> external bar: unit -> Unix.inet_addr * unit = "bar"
>
> let _ =
>         let s = String.create 392 in
>         while true do
>                 ignore(bar ());
>         done
>
> with
>
> CAMLprim value bar(value unit) {
>         CAMLparam1(unit);
>         CAMLlocal2(res, addr);
>         int i;
>
>         addr = alloc_string(4);
>         *(int *)(String_val(addr)) = htonl(0xac100000);
>         res = alloc_tuple(2);
>         Store_field(res, 0, addr);
>         Store_field(res, 1, Val_unit);
>         CAMLreturn(res);
> }
>
> which gives when run:
>
> ~/test$ while /usr/bin/true; do ps -o rss -p 15696 | tail -1; sleep 2; 
> done
>    784
>    784
>    788
>    792
>    796
>    800
>    804
>    808
>    812
> ^C
>
> an ever growing RSS. strangely, having bar() return just the alloced 
> string instead of a tuple, this doesn't occur. even more strangely, if 
> I delete the let s = String.make 392 line, it doesn't occur either. 
> systems tested are MacOS X, FreeBSD (the target system) and Linux. as 
> the RSS approaches the segment size that'll expand too, and this 
> gobbles up all available virtual memory in a week on the smallest 
> target systems.
>
> so, my question is: can anyone see an obvious error in the C snippet? 
> some violation of the rules in the manual I missed?
>
> thanks,
> Lodewijk



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.298 / Virus Database: 265.6.7 - Release Date: 30/12/2004



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] memory leak in C snippet?
  2004-12-31  9:35 ` [Caml-list] " Matthieu Brucher
@ 2004-12-31 11:14   ` Lodewijk Voge
  2004-12-31 15:10   ` Jon Harrop
  1 sibling, 0 replies; 5+ messages in thread
From: Lodewijk Voge @ 2004-12-31 11:14 UTC (permalink / raw)
  To: caml-list

On 31-dec-04, at 10:35, Matthieu Brucher wrote:

> I'm not very into Ocaml at the moment, so perhaps what I will say 
> isn't relevant, but you seem to allocate 2 memory blocs in your 
> function (alloc_string and alloc_tuple), but where do you free this 
> memory ? Does OCaml take charge of this or do you still have to do it 
> yourself as it is in a C function ?

no, ocaml is supposed to do this. if it didn't, the daemon wouldn't 
last an hour, let alone a week :)

I think I'll file this as a bug and see what the gurus have to say 
about it. I'm staring myself silly at the code snippet and the manual 
and it's not helping.

Lodewijk


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] memory leak in C snippet?
  2004-12-31  9:35 ` [Caml-list] " Matthieu Brucher
  2004-12-31 11:14   ` Lodewijk Voge
@ 2004-12-31 15:10   ` Jon Harrop
  2004-12-31 15:48     ` Lodewijk Voge
  1 sibling, 1 reply; 5+ messages in thread
From: Jon Harrop @ 2004-12-31 15:10 UTC (permalink / raw)
  To: caml-list

On Friday 31 December 2004 09:35, Matthieu Brucher wrote:
> > CAMLprim value bar(value unit) {

What is the ocaml type of this function?

Cheers,
Jon.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] memory leak in C snippet?
  2004-12-31 15:10   ` Jon Harrop
@ 2004-12-31 15:48     ` Lodewijk Voge
  0 siblings, 0 replies; 5+ messages in thread
From: Lodewijk Voge @ 2004-12-31 15:48 UTC (permalink / raw)
  To: caml-list

On 31-dec-04, at 16:10, Jon Harrop wrote:

>>> CAMLprim value bar(value unit) {
> What is the ocaml type of this function?

external bar: unit -> Unix.inet_addr * unit = "bar"

Lodewijk


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-12-31 15:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-28 19:32 memory leak in C snippet? Lodewijk Voge
2004-12-31  9:35 ` [Caml-list] " Matthieu Brucher
2004-12-31 11:14   ` Lodewijk Voge
2004-12-31 15:10   ` Jon Harrop
2004-12-31 15:48     ` Lodewijk Voge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).