Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-02-09
03:46 • Ticket [218378ffff] memory leak in regsub status still Closed with 4 other c... artifact: eb11e7f3bf user: rheller
2001-04-20
01:59 • Ticket [218378ffff]: 3 changes artifact: bc1810f8a9 user: dgp
2000-10-26
08:21 • Closed ticket [218378ffff]. artifact: 0660548d60 user: welch
00:56 • New ticket [218378ffff]. artifact: 9ba84f713f user: nobody

memory leak in regsub

Closed / Fixed218378ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 25 years ago
Descriptionplain2000-10-26 00:56:52
OriginalBugID: 4333 Bug
Version: 8.3
SubmitDate: '2000-03-07'
LastModified: '2000-03-21'
Severity: SER
Status: Closed
Submitter: techsupp
ChangedBy: ericm
OS: All Unix
FixedDate: '2000-10-25'
ClosedDate: '2000-03-21'


Name:
Dave Dykstra

ReproducibleScript:
I kept paring down my code to the following test script.  While running tclsh, in another
window do a shell loop running ps -l on the process every second or two to watch the process
size expansion.  It does not happen with tclsh8.0.  I have observed the growth on solaris, hp, and
amdahl's UTS.  The "after"s are in there to allow time for ps to note the process size.

-----------------------
after 3000
puts "initializing"
lappend subs {^lib\/toolman\/.*}
lappend subs {^adm\/rug\/local\/.*}
lappend subs {^\.[^\/]*_time$}
lappend subs {^\.nsb_[^\/]*$}

for {set n 0} {$n < 13} {incr n} {
    foreach sub $subs {
            lappend subs "^x/[string range $sub 1 end]"
    }
}

puts "done with setting up subs"
after 3000
puts "calling regsub on 'nonmatch' with [llength $subs] substitutions"
set path "nonmatch"
foreach sub $subs {
        regsub -all -- $sub $path "" path
}
puts "path is still $path"
after 3000
puts "done"

ObservedBehavior:
Process size grows.



Memory management in Tcl is a bit of a tricky subject.  One important bit (important for this discussion anyway) is that all literals are stored in a global literal table.  This enables better performance at the cost of more memory usage.

Another important bit has to do specifically with regular expressions.  When a regular expression is created, it has to be "compiled" from the string representation of it.  This can be compute-intensive, so in order to further enhance performance, the compiled representation of a regular expression is cached, so if the regexp is used again, Tcl can avoid recompiling it.

I believe that what you are seeing here is the growth of the global literal table, and the creation of many compiled regular expression objects.  Here's a breakdown of what is happening:

1.  Each lappend subs creates an entry in the global literal table.  This
entry is a string object.  This causes some memory allocation.

2.  Each time you call regsub with one of those literals, the object in the global literal table that is associated with it gets changed to a regexp object.  This causes some additional memory allocation.

If you were to later go back and force the objects associated with those literals to be converted to some non-regular expression object (for example, by doing [llength] on it), the compiled regular expression bits would be freed.  Be aware that if you try this you may not actually see the memory footprint of your application decrease; although Tcl is freeing the memory, the OS may not actually reclaim it until it needs to.  This was the case on my Linux machine.

In short, this is the expected behavior of the interpreter.  The process is growing because the global literal table is growing.  You can try to avoid this by avoiding literal expressions and instead using computed values.

- eric

-- 03/21/2000 ericm
welch Closed ticket [218378ffff]. [History diff]2000-10-26 08:21:16
dgp Ticket [218378ffff]: 3 changes [History diff]2001-04-20 01:59:01
rhellerplain2002-02-09 03:46:13
Logged In: YES 
user_id=259412

I have discovered that the http V2.0 package seems to suffer
from this form of memory leak -- it is using *computed*
patterns for regexp.  I have a script that is using the http
package as part of an "endless" loop -- I have implemented a
daemon type of program that fetches an image via HTTP from a
web server (an embedded web server in a network camera)
periodically to disk space on a file server.

This deamon is showing a memory leak.

Is there patch for the http V2.0 that will cause the
compiled regular expressions to be freed?

% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(machine)   = alpha
tcl_platform(os)        = OSF1
tcl_platform(osVersion) = V4.0
tcl_platform(platform)  = unix
tcl_platform(user)      = heller
% set tcl_patchLevel
8.2.0
Log in (anonymous is fine) to add a comment.