Skip to content

Commit 7263540

Browse files
dmagebradfitz
authored andcommitted
regexp: Regexp shouldn't keep references to inputs
If you try to find something in a slice of bytes using a Regexp object, the byte array will not be released by GC until you use the Regexp object on another slice of bytes. It happens because the Regexp object keep references to the input data in its cache. Change-Id: I873107f15c1900aa53ccae5d29dbc885b9562808 Reviewed-on: https://go-review.googlesource.com/96715 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 37a038a commit 7263540

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/regexp/regexp.go

+5
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ func (re *Regexp) get() *machine {
226226
// grow to the maximum number of simultaneous matches
227227
// run using re. (The cache empties when re gets garbage collected.)
228228
func (re *Regexp) put(z *machine) {
229+
// Remove references to input data that we no longer need.
230+
z.inputBytes.str = nil
231+
z.inputString.str = ""
232+
z.inputReader.r = nil
233+
229234
re.mu.Lock()
230235
re.machine = append(re.machine, z)
231236
re.mu.Unlock()

0 commit comments

Comments
 (0)