jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.33k stars 782 forks source link

optimize: high memory footprints #301

Open echoiron opened 5 years ago

echoiron commented 5 years ago

improve : high memory footprints

Hello, our service uses your gopdf framework and has a high memory footprint. feedback

20190910171106

20190910171519

jung-kurt commented 5 years ago

This may be related to #110. Do you include high-resolution images in your documents?

echoiron commented 5 years ago

thanks reply. document is in plain text.

echoiron commented 5 years ago

memory not released

20190911092815

jung-kurt commented 5 years ago

Is your application a long-running service? If so, does the memory usage continue to get larger or does it plateau at some level?

echoiron commented 5 years ago

yes,our server is a long-running and memory usage will continue to grow

echoiron commented 5 years ago

pdf api code

echoiron commented 5 years ago

pprof monitor

echoiron commented 5 years ago

pprof traces

Type: inuse_space
Time: Sep 16, 2019 at 9:52am (CST)
-----------+-------------------------------------------------------
     bytes:  72kB
         0   bytes.makeSlice
             bytes.(*Buffer).grow
             bytes.(*Buffer).Write
             encoding/json.intEncoder
             encoding/json.arrayEncoder.encode
             encoding/json.sliceEncoder.encode
             encoding/json.structEncoder.encode
             encoding/json.ptrEncoder.encode
             encoding/json.(*encodeState).reflectValue
             encoding/json.(*encodeState).marshal
             encoding/json.Marshal
             github.com/jung-kurt/gofpdf.generateFontID
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
...
...
-----------+-------------------------------------------------------
     bytes:  2.26MB
         0   github.com/jung-kurt/gofpdf.(*utf8FontFile).generateSCCSDictionaries
             github.com/jung-kurt/gofpdf.(*utf8FontFile).parseTables
             github.com/jung-kurt/gofpdf.(*utf8FontFile).parseFile
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
     bytes:  1.20MB
         0   github.com/jung-kurt/gofpdf.(*utf8FontFile).generateSCCSDictionaries
             github.com/jung-kurt/gofpdf.(*utf8FontFile).parseTables
             github.com/jung-kurt/gofpdf.(*utf8FontFile).parseFile
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
...
-----------+-------------------------------------------------------
     bytes:  20.77MB
   20.77MB   bytes.makeSlice
             bytes.(*Buffer).grow
             bytes.(*Buffer).Grow
             io/ioutil.readAll
             io/ioutil.ReadFile
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
...
-----------+-------------------------------------------------------
     bytes:  11.16MB
   11.16MB   bytes.makeSlice
             bytes.(*Buffer).grow
             bytes.(*Buffer).Grow
             io/ioutil.readAll
             io/ioutil.ReadFile
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
...
-----------+-------------------------------------------------------
     bytes:  9.30MB
    9.30MB   bytes.makeSlice
             bytes.(*Buffer).grow
             bytes.(*Buffer).Grow
             io/ioutil.readAll
             io/ioutil.ReadFile
             github.com/jung-kurt/gofpdf.(*Fpdf).addFont
             github.com/jung-kurt/gofpdf.(*Fpdf).AddUTF8Font
             beegopdf/controllers.(*PdfGeneratorController).HandlerPdfGenerator
             reflect.Value.call
             reflect.Value.Call
             github.com/astaxie/beego.(*ControllerRegister).ServeHTTP
             net/http.serverHandler.ServeHTTP
             net/http.(*conn).serve
-----------+-------------------------------------------------------
...
-----------+-------------------------------------------------------
     bytes:  64B
         0   syscall.UTF16ToString
             internal/syscall/windows/registry.Key.ReadSubKeyNames
             mime.initMimeWindows
             mime.initMime
             sync.(*Once).Do
             mime.AddExtensionType
             github.com/astaxie/beego.registerMime
             github.com/astaxie/beego.initBeforeHTTPRun
             github.com/astaxie/beego.Run
             main.main
             runtime.main
-----------+-------------------------------------------------------
echoiron commented 5 years ago

hello author , could you add close methods of「AddUTF8Font」? although golang gc automatically reclaims memory, but time is slow.

jung-kurt commented 5 years ago

Thank you, @dajiugo, for tracking this problem down.

could you add close methods of「AddUTF8Font」?

Yes, I will do this shortly.

Antonboom commented 5 years ago

+1

I've opened a similar issue to gopdf: https://github.com/signintech/gopdf/issues/116 But later realized that the problem is not in leaks, but in the work of the application process.

Antonboom commented 5 years ago

Any news? 😢

jung-kurt commented 5 years ago

Any news?

Not yet -- hopefully soon.

Antonboom commented 5 years ago

https://github.com/jung-kurt/gofpdf/pull/321