Is ParseWithClaims function make a memory leak ?
Author: spksoftCreated Jan 20, 2023Updated Oct 31, 2025
Is ParseWithClaims function make a memory leak ?
I use golang-jwt/jwt/[email protected] to parse jwt string to struct by ParseWithClaims. Our service have 1350 req/sec at peak time. So I monitoring the pods memory and visualize its by grafana see below

After that I dump the heap profiling from our services and visualize its by go tool pprof and selected the sample to inuse_space. Pls See below
Graph view

Source View
github.com/golang-jwt/jwt/v4.(*Parser).ParseWithClaims
/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/parser.go
Total: 217.01MB 217.01MB (flat, cum) 98.63%
80 . . ???
88 217.01MB 217.01MB ???
92 . . ???
106 . . ???
net/http.newBufioReader
/usr/local/go/src/net/http/server.go
Total: 1MB 1MB (flat, cum) 0.46%
838 . . ???
845 1MB 1MB ???
net/http.(*conn).serve
/usr/local/go/src/net/http/server.go
Total: 0 1MB (flat, cum) 0.46%
1869 . . ???
1873 . . ???
1874 . 1MB ???
1875 . . ???
1878 . . ???
net/http.(*conn).serve
/usr/local/go/src/net/http/server.go
Total: 0 218.51MB (flat, cum) 99.31%
1952 . 218.51MB ???
1957 . . ???
1962 . . ???
net/http.serverHandler.ServeHTTP
/usr/local/go/src/net/http/server.go
Total: 0 218.51MB (flat, cum) 99.31%
2887 . 218.51MB ???
bufio.NewReaderSize
/usr/local/go/src/bufio/bufio.go
Total: 1MB 1MB (flat, cum) 0.46%
56 . . ???
57 1MB 1MB ???
bufio.NewReader
/usr/local/go/src/bufio/bufio.go
Total: 1MB 1MB (flat, cum) 0.46%
63 1MB 1MB ???
regexp/syntax.Compile
/usr/local/go/src/regexp/syntax/compile.go
Total: 0 517.33kB (flat, cum) 0.23%
73 . 517.33kB ???
regexp/syntax.(*compiler).compile
/usr/local/go/src/regexp/syntax/compile.go
Total: 0 517.33kB (flat, cum) 0.23%
109 . 517.33kB ???
regexp/syntax.(*compiler).compile
/usr/local/go/src/regexp/syntax/compile.go
Total: 0 1.01MB (flat, cum) 0.46%
132 . 517.33kB ???
146 . 517.33kB ???
regexp/syntax.(*compiler).inst
/usr/local/go/src/regexp/syntax/compile.go
Total: 517.33kB 517.33kB (flat, cum) 0.23%
163 517.33kB 517.33kB ???
regexp/syntax.(*compiler).rune
/usr/local/go/src/regexp/syntax/compile.go
Total: 517.33kB 517.33kB (flat, cum) 0.23%
257 517.33kB 517.33kB ???
github.com/golang-jwt/jwt/v4.(*SigningMethodHMAC).Sign
/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/hmac.go
Total: 512.38kB 512.38kB (flat, cum) 0.23%
88 . . ???
89 512.38kB 512.38kB ???
91 . . ???
bytes.(*Buffer).grow
/usr/local/go/src/bytes/buffer.go
Total: 0 512.25kB (flat, cum) 0.23%
142 . 512.25kB ???
bytes.(*Buffer).ReadFrom
/usr/local/go/src/bytes/buffer.go
Total: 0 512.25kB (flat, cum) 0.23%
202 . 512.25kB ???
bytes.makeSlice
/usr/local/go/src/bytes/buffer.go
Total: 512.25kB 512.25kB (flat, cum) 0.23%
229 512.25kB 512.25kB ???
reflect.mapiterinit
/usr/local/go/src/runtime/map.go
Total: 512.05kB 512.05kB (flat, cum) 0.23%
1339 512.05kB 512.05kB ???
github.com/getsentry/sentry-go/gin.(*handler).handle
/go/pkg/mod/github.com/getsentry/[email protected]/gin/sentrygin.go
Total: 0 218.01MB (flat, cum) 99.09%
52 . . ???
54 . . ???
55 . . ???
57 . 218.01MB ???
<my_pkg>/logger/middlewares.NewRequestLogger.func1
/app/logger/middlewares/httplogger.go
Total: 0 218.51MB (flat, cum) 99.31%
22 . . ???
23 . 512.25kB ???
25 . . ???
31 . . ???
34 . . ???
36 . . ???
37 . . ???
38 . . ???
41 . . ???
42 . . ???
43 . . ???
46 . 218.01MB ???
50 . . ???
51 . . ???
59 . . ???
github.com/gin-gonic/gin.(*Engine).ServeHTTP
/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go
Total: 0 218.51MB (flat, cum) 99.31%
567 . . ???
572 . 218.51MB ???
574 . . ???
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest
/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go
Total: 0 218.51MB (flat, cum) 99.31%
616 . 218.51MB ???
617 . . ???
regexp.Compile
/usr/local/go/src/regexp/regexp.go
Total: 0 517.33kB (flat, cum) 0.23%
133 . 517.33kB ???
regexp.compile
/usr/local/go/src/regexp/regexp.go
Total: 0 517.33kB (flat, cum) 0.23%
178 . 517.33kB ???
189 . . ???
regexp.MustCompile
/usr/local/go/src/regexp/regexp.go
Total: 0 517.33kB (flat, cum) 0.23%
309 . 517.33kB ??? - Peak View
File: service
Type: inuse_space
Time: Jan 20, 2023 at 3:35pm (+07)
Showing nodes accounting for 220.02MB, 100% of 220.02MB total
----------------------------------------------------------+-------------
flat flat% sum% cum cum% calls calls% + context
----------------------------------------------------------+-------------
217.01MB 100% | github.com/golang-jwt/jwt/v4.ParseWithClaims /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:108
217.01MB 98.63% 98.63% 217.01MB 98.63% | github.com/golang-jwt/jwt/v4.(*Parser).ParseWithClaims /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/parser.go:88
----------------------------------------------------------+-------------
1MB 100% | bufio.NewReader /usr/local/go/src/bufio/bufio.go:63 (inline)
1MB 0.46% 99.09% 1MB 0.46% | bufio.NewReaderSize /usr/local/go/src/bufio/bufio.go:57
----------------------------------------------------------+-------------
0.51MB 100% | regexp/syntax.(*compiler).rune /usr/local/go/src/regexp/syntax/compile.go:257 (inline)
0.51MB 0.23% 99.32% 0.51MB 0.23% | regexp/syntax.(*compiler).inst /usr/local/go/src/regexp/syntax/compile.go:163
----------------------------------------------------------+-------------
0.50MB 100% | github.com/golang-jwt/jwt/v4.(*Token).SignedString /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:64
0.50MB 0.23% 99.55% 0.50MB 0.23% | github.com/golang-jwt/jwt/v4.(*SigningMethodHMAC).Sign /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/hmac.go:89
----------------------------------------------------------+-------------
0.50MB 100% | bytes.(*Buffer).grow /usr/local/go/src/bytes/buffer.go:142
0.50MB 0.23% 99.77% 0.50MB 0.23% | bytes.makeSlice /usr/local/go/src/bytes/buffer.go:229
----------------------------------------------------------+-------------
0.50MB 100% | reflect.Value.MapKeys /usr/local/go/src/reflect/value.go:1215
0.50MB 0.23% 100% 0.50MB 0.23% | reflect.mapiterinit /usr/local/go/src/runtime/map.go:1339
----------------------------------------------------------+-------------
1MB 100% | net/http.newBufioReader /usr/local/go/src/net/http/server.go:845 (inline)
0 0% 100% 1MB 0.46% | bufio.NewReader /usr/local/go/src/bufio/bufio.go:63
1MB 100% | bufio.NewReaderSize /usr/local/go/src/bufio/bufio.go:57 (inline)
----------------------------------------------------------+-------------
0.50MB 100% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:23
0 0% 100% 0.50MB 0.23% | bytes.(*Buffer).ReadFrom /usr/local/go/src/bytes/buffer.go:202
0.50MB 100% | bytes.(*Buffer).grow /usr/local/go/src/bytes/buffer.go:142
----------------------------------------------------------+-------------
0.50MB 100% | bytes.(*Buffer).ReadFrom /usr/local/go/src/bytes/buffer.go:202
0 0% 100% 0.50MB 0.23% | bytes.(*Buffer).grow /usr/local/go/src/bytes/buffer.go:142
0.50MB 100% | bytes.makeSlice /usr/local/go/src/bytes/buffer.go:229
----------------------------------------------------------+-------------
0.50MB 100% | encoding/json.Marshal /usr/local/go/src/encoding/json/encode.go:161
0 0% 100% 0.50MB 0.23% | encoding/json.(*encodeState).marshal /usr/local/go/src/encoding/json/encode.go:332
0.50MB 100% | encoding/json.(*encodeState).reflectValue /usr/local/go/src/encoding/json/encode.go:360
----------------------------------------------------------+-------------
0.50MB 100% | encoding/json.(*encodeState).marshal /usr/local/go/src/encoding/json/encode.go:332
0 0% 100% 0.50MB 0.23% | encoding/json.(*encodeState).reflectValue /usr/local/go/src/encoding/json/encode.go:360
0.50MB 100% | encoding/json.mapEncoder.encode /usr/local/go/src/encoding/json/encode.go:797
----------------------------------------------------------+-------------
0.50MB 100% | github.com/golang-jwt/jwt/v4.(*Token).SigningString /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:78
0 0% 100% 0.50MB 0.23% | encoding/json.Marshal /usr/local/go/src/encoding/json/encode.go:161
0.50MB 100% | encoding/json.(*encodeState).marshal /usr/local/go/src/encoding/json/encode.go:332
----------------------------------------------------------+-------------
0.50MB 100% | encoding/json.(*encodeState).reflectValue /usr/local/go/src/encoding/json/encode.go:360
0 0% 100% 0.50MB 0.23% | encoding/json.mapEncoder.encode /usr/local/go/src/encoding/json/encode.go:797
0.50MB 100% | reflect.Value.MapKeys /usr/local/go/src/reflect/value.go:1215
----------------------------------------------------------+-------------
217.51MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 217.51MB 98.86% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:43
217.01MB 99.77% | <my_pkg>/app/proxy.proxy.<my_handler2> /app/app/proxy/handler.go:68
0.50MB 0.23% | <my_pkg>/app/proxy.proxy.<my_handler2> /app/app/proxy/handler.go:113
----------------------------------------------------------+-------------
0.50MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 0.50MB 0.23% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:46
0.50MB 100% | <my_pkg>/app/proxy.proxy.<my_handler> /app/app/proxy/handler.go:172
----------------------------------------------------------+-------------
0.50MB 100% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:46
0 0% 100% 0.50MB 0.23% | <my_pkg>/app/proxy.proxy.<my_handler> /app/app/proxy/handler.go:172
0.50MB 100% | github.com/golang-jwt/jwt/v4.(*Token).SignedString /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:61
----------------------------------------------------------+-------------
0.50MB 100% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:43
0 0% 100% 0.50MB 0.23% | <my_pkg>/app/proxy.proxy.<my_handler2> /app/app/proxy/handler.go:113
0.50MB 100% | github.com/golang-jwt/jwt/v4.(*Token).SignedString /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:64
----------------------------------------------------------+-------------
217.01MB 100% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:43
0 0% 100% 217.01MB 98.63% | <my_pkg>/app/proxy.proxy.<my_handler2> /app/app/proxy/handler.go:68
217.01MB 100% | github.com/golang-jwt/jwt/v4.ParseWithClaims /go/pkg/mod/github.com/golang-jwt/jwt/[email protected]/token.go:108
----------------------------------------------------------+-------------
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 218.01MB 99.09% | <my_pkg>/logger/middlewares.CaptureException /app/logger/middlewares/sentry.go:13
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (inline)
----------------------------------------------------------+-------------
0.50MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 0.50MB 0.23% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:23
0.50MB 100% | bytes.(*Buffer).ReadFrom /usr/local/go/src/bytes/buffer.go:202
----------------------------------------------------------+-------------
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 218.01MB 99.09% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:46
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (inline)
----------------------------------------------------------+-------------
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
0 0% 100% 218.01MB 99.09% | github.com/getsentry/sentry-go/gin.(*handler).handle /go/pkg/mod/github.com/getsentry/[email protected]/gin/sentrygin.go:57
218.01MB 100% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (inline)
----------------------------------------------------------+-------------
218.51MB 100% | github.com/gin-gonic/gin.(*Engine).handleHTTPRequest /go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:616 (inline)
218.01MB 99.77% | <my_pkg>/logger/middlewares.CaptureException /app/logger/middlewares/sentry.go:13 (inline)
218.01MB 99.77% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:46 (inline)
218.01MB 99.77% | github.com/getsentry/sentry-go/gin.(*handler).handle /go/pkg/mod/github.com/getsentry/[email protected]/gin/sentrygin.go:57 (inline)
0 0% 100% 218.51MB 99.31% | github.com/gin-gonic/gin.(*Context).Next /go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173
218.01MB 99.77% | <my_pkg>/logger/middlewares.CaptureException /app/logger/middlewares/sentry.go:13
218.01MB 99.77% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:46
218.01MB 99.77% | github.com/getsentry/sentry-go/gin.(*handler).handle /go/pkg/mod/github.com/getsentry/[email protected]/gin/sentrygin.go:57
217.51MB 99.54% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:43
0.50MB 0.23% | <my_pkg>/logger/middlewares.NewRequestLogger.func1 /app/logger/middlewares/httplogger.go:23
0.50MB 0.23% | <my_pkg>/app/proxy.proxy.HandleRequestAndRedirect /app/app/proxy/handler.go:46
----------------------------------------------------------+-------------
218.51MB 100% | net/http.serverHandler.ServeHTTP /usr/local/go/src/net/http/server.go:2887
0 0% 100% 218.51MB 99.31% | github.com/gin-gonic/gin.(*Engine).ServeHTTP /go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:572
218.51MB 100% | github.com/gin-gonic/gin.(*Engine).handleHTTPRequest /go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:616
----------------------------------------------------------+-------------
218.51MB 100% | github.comSource: golang-jwt/jwt