Memory leaks in Tcl8.3.4 
Author Message
 Memory leaks in Tcl8.3.4

Hello All,

I have an application which embeds a tcl interpreter. I have noticed
that this has a fairly substantial memory leak, which I have traced,
using dmalloc, to tclAlloc.c:672 (which on my linux system just calls
malloc).

Is there a way that I can figure out why this isn't free-ing? I'd say
that this is a pretty substantial problem (~200K/iteration) and needs
fixing pretty soon. I call Tcl_Finalize() just before deleting the
interpreter, which I believe should tidy things up, and removing this
actually makes the memory leak far worse.

This is fairly critical, as I will want this application to keep on
running indefinately.

Any information or advice on debugging this would be helpful!

Cheers,

Graeme



Sat, 31 Jul 2004 02:15:15 GMT  
 Memory leaks in Tcl8.3.4
        ...

Quote:
> I have an application which embeds a tcl interpreter. I have noticed
> that this has a fairly substantial memory leak, which I have traced,
> using dmalloc, to tclAlloc.c:672 (which on my linux system just calls
> malloc).

You'll have to trace farther back to the call that caused that alloc,
like whereever an object was created.  As you note that Tcl is
embedded in your app, I would bet that there is an object being
created that isn't being freed.  8.3.4 runs through the entire Tcl
test suite without any mem leaks according to Purify, so I would
focus on whatever code you added first.  That doesn't mean that Tcl
is mem-leak free, but it has had a good comb run over 8.3.4.

--
  Jeff Hobbs                     The Tcl Guy
  Senior Developer               http://www.ActiveState.com/
        Tcl Support and Productivity Solutions
     http://www.ActiveState.com/Products/ASPN_Tcl/



Sat, 31 Jul 2004 03:38:58 GMT  
 Memory leaks in Tcl8.3.4

Quote:

>Hello All,

>I have an application which embeds a tcl interpreter. I have noticed
>that this has a fairly substantial memory leak, which I have traced,
>using dmalloc, to tclAlloc.c:672 (which on my linux system just calls
>malloc).

>Is there a way that I can figure out why this isn't free-ing? I'd say
>that this is a pretty substantial problem (~200K/iteration) and needs
>fixing pretty soon. I call Tcl_Finalize() just before deleting the
>interpreter, which I believe should tidy things up, and removing this
>actually makes the memory leak far worse.

>This is fairly critical, as I will want this application to keep on
>running indefinately.

>Any information or advice on debugging this would be helpful!

>Cheers,

>Graeme

Hello,

        What I usually do, is compile Tcl with the TCL_MEM_DEBUG flag turned on and
with symbols.  Then just before exiting your program you can do a
Tcl_DumpActiveMemory("/tmp/memdump");

        This will give you a very good idea of what has been allocated but not
freed.

Todd

--
Todd M. Helfter
Database Analyst/Programmer
Purdue University



Sat, 31 Jul 2004 05:41:12 GMT  
 Memory leaks in Tcl8.3.4
I have an application which embeds a tcl interpreter and use function
Tcl_GlobalEval(interp, user_cmd) to execute the user's tcl command. If
run in interactive mode (under tcl shell), it works fine. But if I run
in batch mode by redirecting the tcl script file as user's input, tcl
interpreter complains some tcl commands. For example, in file a.tcl,
I have:

        set n [ls; ls -alt]

When I run myAppl in interactive mode, I got

Quote:
> % set n [ls; ls -alt]
> a.tcl
> -rw-rw-r--   1 wzuo     ammocore      25 Feb 11 15:13 a.tcl

If I run myAppl < a.tcl, I got

Quote:
> % set n [ ls; ls -lat]
> invalid command name "ls"
> Exiting ...

I use debug to trace the problem and found that Tcl_GlobalEval behaves
differently in these two situations even with exactly same input. Does
anybody know what happens here? Thank you very much.

wzuo



Sat, 31 Jul 2004 09:22:44 GMT  
 Memory leaks in Tcl8.3.4

Quote:

>I have an application which embeds a tcl interpreter and use function
>Tcl_GlobalEval(interp, user_cmd) to execute the user's tcl command. If
>run in interactive mode (under tcl shell), it works fine. But if I run
>in batch mode by redirecting the tcl script file as user's input, tcl
>interpreter complains some tcl commands. For example, in file a.tcl,
>I have:

>    set n [ls; ls -alt]

>When I run myAppl in interactive mode, I got
>> % set n [ls; ls -alt]
>> a.tcl
>> -rw-rw-r--   1 wzuo     ammocore      25 Feb 11 15:13 a.tcl

>If I run myAppl < a.tcl, I got
>> % set n [ ls; ls -lat]
>> invalid command name "ls"
>> Exiting ...

>I use debug to trace the problem and found that Tcl_GlobalEval behaves
>differently in these two situations even with exactly same input. Does
>anybody know what happens here? Thank you very much.

>wzuo

ls isn't a tcl command, but in interactive-mode [unknown] looks out into the
system.  be explicit and call exec, or better yet, use [glob].

set n [exec ls\; ls -alt]
--

[species: human; planet: earth,milkyway,alpha sector]
Please be aware of the 7.5 year ping times when placing a call from alpha centari



Sat, 31 Jul 2004 09:28:41 GMT  
 Memory leaks in Tcl8.3.4

Quote:

> I have an application which embeds a tcl interpreter. I have noticed
> that this has a fairly substantial memory leak, which I have traced,
> using dmalloc, to tclAlloc.c:672 (which on my linux system just calls
> malloc).

 Hmm, I'm afraid that tracing a memory leak to malloc() is not very
helpful. You should do the tracing in reverse, finding out which top-
level Tcl function allocates the memory that is not released.

Quote:

> Is there a way that I can figure out why this isn't free-ing?
> Any information or advice on debugging this would be helpful!

 Find the smallest amount of code (Tcl or C) that reproduces this
problem. Hint: put that code in a tight loop, so that you can notice
very quickly whether memory consumption grows or doesn't.

 Next, double-check that the error is not on your end.

 Then, post that code as a bug report to sourceforge.

        Frank

--

Experience is something you never have until just after you need it.
- Alfred E. Neuman



Sat, 31 Jul 2004 16:47:57 GMT  
 Memory leaks in Tcl8.3.4

Quote:
>         What I usually do, is compile Tcl with the TCL_MEM_DEBUG flag turned on and
> with symbols.  Then just before exiting your program you can do a
> Tcl_DumpActiveMemory("/tmp/memdump");

>         This will give you a very good idea of what has been allocated but not
> freed.

A powerful solution, if with disturbing consequences. I find that there are 1034
memory allocations remaining, most fairly small (<= 1024 bytes) I'd include the list
here, but I think that it'll be too long. Anyway, most of
the allocations are called from things in ../generic such as tclIO.c, regc_nfa.c
(loads of these) tclRegexp.c regcomp.c, regc_color.c, tclHash.c and a bunch of others.
It seems that around 90% of the remaining allocations are linked with the regular
expression stuff.

Cheers,

Graeme



Sat, 31 Jul 2004 18:48:18 GMT  
 Memory leaks in Tcl8.3.4

Quote:

> >         What I usually do, is compile Tcl with the TCL_MEM_DEBUG flag turned on and
> > with symbols.  Then just before exiting your program you can do a
> > Tcl_DumpActiveMemory("/tmp/memdump");

> >         This will give you a very good idea of what has been allocated but not
> > freed.

> A powerful solution, if with disturbing consequences. I find that there are 1034
> memory allocations remaining, most fairly small (<= 1024 bytes) I'd include the list
> here, but I think that it'll be too long. Anyway, most of
> the allocations are called from things in ../generic such as tclIO.c, regc_nfa.c
> (loads of these) tclRegexp.c regcomp.c, regc_color.c, tclHash.c and a bunch of others.
> It seems that around 90% of the remaining allocations are linked with the regular
> expression stuff.

> Cheers,

> Graeme

Doh! Please ignore this message - I hadn't put Tcl_Finalize back in yet. There are just a
couple of tiny leaks left, in tclThread.c (32 bytes a piece)

Still have a memory problem though...

Graeme



Sat, 31 Jul 2004 19:04:09 GMT  
 Memory leaks in Tcl8.3.4

Quote:

>  Hmm, I'm afraid that tracing a memory leak to malloc() is not very
> helpful. You should do the tracing in reverse, finding out which top-
> level Tcl function allocates the memory that is not released.

That's why people use Purify with MIU analysis turned on.

Donal.
--

-- Always running as a superuser is not a fault, it's an OS preference.



Sat, 31 Jul 2004 19:36:03 GMT  
 Memory leaks in Tcl8.3.4

Quote:

> It seems that around 90% of the remaining allocations are linked with the
> regular expression stuff.

The regexp engine definitely does not leak; if you write a tight loop that does
a million different [regexp] evaluations, the amount of memory required to run
remains constant throughout the run.

  for {set i 0} {$i<1000000} {incr i} {
     regexp ^$i$ sldfglksdg
  }

You can't get a much stronger proof than that!

Donal.
--

-- Always running as a superuser is not a fault, it's an OS preference.



Sat, 31 Jul 2004 19:39:02 GMT  
 Memory leaks in Tcl8.3.4

Quote:


> > I have an application which embeds a tcl interpreter. I have noticed
> > that this has a fairly substantial memory leak, which I have traced,
> > using dmalloc, to tclAlloc.c:672 (which on my linux system just calls
> > malloc).

>  Hmm, I'm afraid that tracing a memory leak to malloc() is not very
> helpful. You should do the tracing in reverse, finding out which top-
> level Tcl function allocates the memory that is not released.

> > Is there a way that I can figure out why this isn't free-ing?
> > Any information or advice on debugging this would be helpful!

>  Find the smallest amount of code (Tcl or C) that reproduces this
> problem. Hint: put that code in a tight loop, so that you can notice
> very quickly whether memory consumption grows or doesn't.

>  Next, double-check that the error is not on your end.

>  Then, post that code as a bug report to sourceforge.

>         Frank

> --

> Experience is something you never have until just after you need it.
> - Alfred E. Neuman

Okay, I have done this, and I observe the same fairly substantial memory
leak - from this I'd guess that it is pilot error, and would appreciate
some guidance. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <tcl.h>

int main() {
  Tcl_Interp *interp;
  int i;
  char task[] = "for {set i 0} {$i < 1000} {incr i} {set a($i) $i}"

  for(i = 0; i < 100000; i++) {
    interp = Tcl_CreateInterp();
    Tcl_Init(interp);
    Tcl_Eval(interp, task);
    Tcl_Finalize();
    Tcl_DeleteInterp(interp);
  }

  return 0;

Quote:
}

and compiled with

cc -o example example.c -ltcl

This uses memory pretty rapidly - what have I missed?

Cheers,

Graeme



Sat, 31 Jul 2004 23:00:45 GMT  
 Memory leaks in Tcl8.3.4

Quote:


>> >         What I usually do, is compile Tcl with the TCL_MEM_DEBUG flag turned on and
>> > with symbols.  Then just before exiting your program you can do a
>> > Tcl_DumpActiveMemory("/tmp/memdump");

>> >         This will give you a very good idea of what has been allocated but not
>> > freed.

>> A powerful solution, if with disturbing consequences. I find that there are 1034
>> memory allocations remaining, most fairly small (<= 1024 bytes) I'd include the list
>> here, but I think that it'll be too long. Anyway, most of
>> the allocations are called from things in ../generic such as tclIO.c, regc_nfa.c
>> (loads of these) tclRegexp.c regcomp.c, regc_color.c, tclHash.c and a bunch of others.
>> It seems that around 90% of the remaining allocations are linked with the regular
>> expression stuff.

>> Cheers,

>> Graeme

>Doh! Please ignore this message - I hadn't put Tcl_Finalize back in yet. There are just a
>couple of tiny leaks left, in tclThread.c (32 bytes a piece)

>Still have a memory problem though...

>Graeme

It seems likely to me that perhaps your application is leaking elsewhere, not in
the TCL code.

If you are doing your own malloc/free's, you might consider using TCL's ckalloc,
ckrealloc, and ckfree (at least temporarily) as that is what triggers the
Tcl_DumpActiveMemory.

I once found out that my extension was leaking memory a meg per second because
I had done (psuedocode)

Tcl_ObjSetVar2(interp, arrayvar, Tcl_NewStringObj(....), .....)

and replacing that with

tmp_obj = Tcl_NewStringObj();
Tcl_IncrRefCount(tmp_obj);
Tcl_ObjSetVar2(interp, arrayvar, tmp_obj, ....);
Tcl_DecrRefCount(tmp_obj);

prevented any leaks at all.

--
Todd M. Helfter
Database Analyst/Programmer
Purdue University



Sat, 31 Jul 2004 22:00:49 GMT  
 Memory leaks in Tcl8.3.4

Quote:

> ...
> #include <stdio.h>
> #include <stdlib.h>
> #include <tcl.h>

> int main() {
>   Tcl_Interp *interp;
>   int i;
>   char task[] = "for {set i 0} {$i < 1000} {incr i} {set a($i) $i}"

>   for(i = 0; i < 100000; i++) {
>     interp = Tcl_CreateInterp();
>     Tcl_Init(interp);
>     Tcl_Eval(interp, task);
>     Tcl_Finalize();
>     Tcl_DeleteInterp(interp);
>   }

>   return 0;
> }

> and compiled with

> cc -o example example.c -ltcl

> This uses memory pretty rapidly - what have I missed?

I think you are leaking (part of) the interp structures. Why are you
calling Tcl_Finalize() before Tcl_DeleteInterp(interp)? Does inverting
the order of those two statements plug the leak?

Actually, the natural way would be to call
   Tcl_Finalize();
   return 0;
outside the loop - or replace both these statements with Tcl_Exit(0).

Miguel



Sat, 31 Jul 2004 22:58:03 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Memory leak in tcl8.3.4 library (c program)

2. Potential memory leak in Tcl8.4b1 using thread allocator

3. Second potential memory leak in Tcl8.4b1 using thread allocator

4. Memory leaks in Tcl8.1.1

5. Memory leak tcl8.4.1 sol8

6. tcl8.0p2 memory leaks with arrays

7. Memory leak problems with tcl8.0b2 on NT...

8. memory leak and leak-fixing 'patterns'

9. python startup memory size and memory leak

10. Uninitialized memory errors and memory leaks in Tk

11. memory usage (how to debug a memory leak?)

12. Temp var leak in compiled Tcl8.0p2???

 

 
Powered by phpBB® Forum Software