Skip to content


Browse files Browse the repository at this point in the history
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-by: Brad Fitzpatrick <>
Run-TryBot: Brad Fitzpatrick <>
TryBot-Result: Gobot Gobot <>
  • Loading branch information
dmage authored and bradfitz committed Feb 23, 2018
1 parent 37a038a commit 7263540
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/regexp/regexp.go
Expand Up @@ -226,6 +226,11 @@ func (re *Regexp) get() *machine {
// grow to the maximum number of simultaneous matches
// run using re. (The cache empties when re gets garbage collected.)
func (re *Regexp) put(z *machine) {
// Remove references to input data that we no longer need.
z.inputBytes.str = nil
z.inputString.str = ""
z.inputReader.r = nil
re.machine = append(re.machine, z)
Expand Down

0 comments on commit 7263540

Please sign in to comment.