Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Memory leakage

Closed / Fixed2949302fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 17 years ago
Descriptionplain2010-02-10 20:31:18
Run the code and observe memory:

for {set index 0} {$index < 2000} {incr index} {catch {expr a}}

Tested on XP and openSuse.

thx

for cross-reference: http://bugs.activestate.com/show_bug.cgi?id=86122
dgpplain2010-02-11 11:16:44
Using this script, I see no leak on the
tip of the core-8-4-branch:

    proc getbytes {} {
        set lines [split [memory info] "\n"]
        lindex $lines 3 3
    }
    set end [getbytes]
    for {set i 0} {$i < 5} {incr i} {
        catch {expr a}

        set tmp $end
        set end [getbytes]
    }
    puts "Leaked [expr {$end - $tmp}] bytes!"

Since this does not show any leak, what does?
dkfplain2010-02-11 16:23:38
I reran the same script interactively (except for 2 million iterations) while watching the process in another window. Even a leak of a single byte would have shown, but the memory assigned to the process stuck fast to well below 1MB. When I do something that *does* allocate memory (e.g. building a list) it is quite clear in 'top'...
tombertplain2010-02-11 16:33:39
I do not have the memory command installed, I'am watching with windows task manager:
every time I call this script the memory goes: 10236k, 10556k, 10860k, 1168k, ...
I'am using the ActiveState distribution 8.4.19.2 and running the script in the tkcon.

Then I've tried the same thing with the tclsh (again from ActiveState), the memory goes: 4496k, 4800k, 5108k, ....

Did you use task manager as well or only this built-in memory tool?
tombertplain2010-02-11 16:44:22
Figured it out: I tested it on 8.4.15 on an openSuse distribution (no ActiveState) and the leakage does not happen.
When I'am using the ActiveState distribution (on the same machine) it leaks.

So ... probably I post again in the ActiveState bugs from where I initially come from ...

http://bugs.activestate.com/show_bug.cgi?id=86122

thx
for help!
hobbsplain2010-02-12 01:01:55
allow_comments - 1

It appears this has to do with the TIP#280 backport to 8.4 used by ActiveTcl.
andreas_kupriesplain2010-02-12 01:37:32
Yes. From the investigation ...

Confirmed leak for CVS head of Tcl as well, on linux.
Ran the attached trial script against a plain tclsh, and a tclsh with
-DTCL_TIP280 (*).
Only the latter is leaking, see the results below

(*) Both compiled with --enable-symbols=all to activate the integrated [memory]
command and debugging.

Run results, plain shell first, the #280-enabled shell.

/home/andreask/workbench/MemoryLeakExpr/install/tcl84p/bin/tclsh8.4
8.4.19
   0:   current bytes allocated        76836
   1:   current bytes allocated        76912
   2:   current bytes allocated        76912
   3:   current bytes allocated        76912
   4:   current bytes allocated        76912
   5:   current bytes allocated        76912
   6:   current bytes allocated        76912
   7:   current bytes allocated        76912
   8:   current bytes allocated        76912
   9:   current bytes allocated        76912

/home/andreask/workbench/MemoryLeakExpr/install/tcl84/bin/tclsh8.4
8.4.19
   0:   current bytes allocated        80966
   1:   current bytes allocated       313042
   2:   current bytes allocated       545042
   3:   current bytes allocated       777042
   4:   current bytes allocated      1009042
   5:   current bytes allocated      1241042
   6:   current bytes allocated      1473042
   7:   current bytes allocated      1705042
   8:   current bytes allocated      1937042
   9:   current bytes allocated      2169042

Now focusing on the #280 code ...

... Later ...

I seem to have identified the leak.

extCmdMapPtr in the CompileEnv structure and the adjunct structures it points
to.

* Allocated in TclInitCompileEnv (tclCompile.c:827).

* Handed over to the interp->lineBCPtr HashTable in TclInitByteCodeObj
  (tclCompile.c:772), called by TclSetByteCodeFromAny (tclCompile.c:447).
Because of this
  change of ownership it is not freed as part of TclFreeCompileEnv(), but when
the 
  bytecode structure is released, i.e. TclCleanupByteCode
(tclCompile.c:711-743).

* This hand-over is however conditional on the compilation result being TCL_OK,
and that
  is not the case here. The structure is therefore never handed over to a
bytecode
  structure, and not released at that point either. LEAK.

Tcl 8.5+ do not have this leak because for them bytecode compilation cannot
fail. I.e. is always ok, and thus the handover is always done. This is because
in 8.5+ any type of compilation problem, i.e. like bad syntax in an expression
is converted into a __runtime__ error (invokation of [error] command), instead
of failing the compilation itself.
andreas_kupriesplain2010-02-12 01:37:33
allow_comments - 0
andreas_kupriesplain2010-02-12 02:52:33
allow_comments - 1

Fixes committed to cvs 8.4 branch head.
Log in (anonymous is fine) to add a comment.