wellington / go-libsass

Go wrapper for libsass, the only Sass 3.5 compiler for Go
http://godoc.org/github.com/wellington/go-libsass
Apache License 2.0
206 stars 28 forks source link

Possible memory leak? #76

Open theothertomelliott opened 5 years ago

theothertomelliott commented 5 years ago

I've been using go-libsass to compile sass content for static sites in a long-running process, and believe I'm seeing a memory leak in the library.

I put together a small program to exercise this:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "runtime"
    "strings"
    "time"

    humanize "github.com/dustin/go-humanize"
    libsass "github.com/wellington/go-libsass"
)

func main() {
    var (
        sample = `
$font-stack:    Helvetica, sans-serif;
$primary-color: #333;
body {
    font: 100% $font-stack;
    color: $primary-color;
}
        `
        maxAlloc uint64
        start    = time.Now()
    )
    for i := 0; true; i++ {
        comp, err := libsass.New(ioutil.Discard, strings.NewReader(sample))
        if err != nil {
            log.Fatal(err)
        }

        if err := comp.Run(); err != nil {
            log.Fatal(err)
        }
        var m runtime.MemStats
        runtime.ReadMemStats(&m)
        if m.Alloc > maxAlloc*2 {
            maxAlloc = m.Alloc
            fmt.Printf(
                "%v (%d): Alloc = %v MiB, NumGC = %v\n",
                time.Since(start),
                i,
                humanize.Bytes(maxAlloc),
                m.NumGC,
            )
        }
    }
}

Running using go-libsass v0.9.2 I see the following output after running for 10 minutes:

1.839078ms (0): Alloc = 106 kB MiB, NumGC = 0
21.184192ms (26): Alloc = 215 kB MiB, NumGC = 0
55.547495ms (79): Alloc = 432 kB MiB, NumGC = 0
109.932748ms (186): Alloc = 868 kB MiB, NumGC = 0
206.257662ms (400): Alloc = 1.7 MB MiB, NumGC = 0
713.857603ms (1563): Alloc = 3.5 MB MiB, NumGC = 1
4.377593582s (9946): Alloc = 7.0 MB MiB, NumGC = 16
8.849047488s (19345): Alloc = 14 MB MiB, NumGC = 24
22.658077622s (40564): Alloc = 28 MB MiB, NumGC = 33
46.552797916s (78313): Alloc = 56 MB MiB, NumGC = 41
1m38.739205546s (165444): Alloc = 111 MB MiB, NumGC = 50
2m59.728412134s (318347): Alloc = 223 MB MiB, NumGC = 58
6m5.303138905s (660328): Alloc = 446 MB MiB, NumGC = 67

Is there a step I'm missing to release resources? Or is there something that needs adding to the Run() function?