Overview
| Artifact ID: | 9ba84f713faa12071d57be52d123edd353d7a63e |
|---|---|
| Ticket: | 218378ffffffffffffffffffffffffffffffffff
memory leak in regsub |
| User & Date: | nobody on 2000-10-26 00:56:52 |
Changes
- assignee changed to: "nobody"
- cmimetype changed to: "text/plain"
- comment changed to:
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 - foundin changed to: "obsolete: 8.3"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- status changed to: "Open"
- submitter changed to: "nobody"
- subsystem changed to: "69. Other"
- title changed to: "memory leak in regsub"
- type changed to: "Bug"