{"id":347182,"date":"2023-03-23T21:01:18","date_gmt":"2023-03-23T21:01:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=347182"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=347182","title":{"rendered":"<span>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044e \u0443\u0442\u0435\u0447\u0435\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 Go<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0432 Go-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438: \u0447\u0435\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0443\u0442\u0435\u0447\u043a\u0438 \u0438 \u0441 \u0447\u0435\u0433\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h2>\u041f\u0440\u0438\u0447\u0438\u043d\u044b \u0443\u0442\u0435\u0447\u0435\u043a<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<p><strong>1) \u0423\u0442\u0435\u0447\u043a\u0430 \u0433\u043e\u0440\u0443\u0442\u0438\u043d<\/strong><\/p>\n<p>\u041f\u0440\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u0442\u0435\u043a\u0430\u044e\u0442 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0435\u0435 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0441\u0442\u0435\u043a \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439 \u0433\u043e\u0440\u0443\u0442\u0438\u043d:<\/p>\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\" )   func process(ch chan string) {   for v := range ch {      log.Println(v)   } }   func main() {   http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      ch := make(chan string)      \/\/ We forgot to close the channel. As the result process never finishes      \/\/ defer close(ch)        go process(ch)        for i := 0; i &lt; 5; i++ {         ch &lt;- fmt.Sprintf(\"text %d\", i)      }        writer.Write([]byte(\"for bar response\"))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) } <\/code><\/pre>\n<p><strong>2) \u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u043f\u0443, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0443\u0442\u0435\u043a\u0430\u0442\u044c. \u041e\u0434\u0438\u043d \u0440\u0430\u0437 \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0443\u0442\u0435\u0447\u043a\u0443 \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u043e gorilla context. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 http \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u043d\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u043f\u0443 \u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u043a\u043b\u044e\u0447 \u043c\u0430\u043f\u044b \u0431\u0435\u0437 \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u043a\u043e\u0434\u0435. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 Go 1.7, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 gorilla \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c http.Request.Context()<\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0435\u043d\u0430 \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0442\u0438\u043f\u043e\u0432 \u0443\u0442\u0435\u0447\u0435\u043a \u043a\u0430\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>3) sync.Pool, Cgo<\/strong><\/p>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u043d\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u043e sync.Pool \u0438 Cgo.<\/p>\n<p><strong>Sync.Pool<\/strong><br \/>\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <em>&#171;A Pool is a set of temporary objects that may be individually saved and retrieved.&#187;<\/em> \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f <a href=\"https:\/\/github.com\/golang\/go\/issues\/23199\" rel=\"noopener noreferrer nofollow\">issue<\/a>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u044b \u0432\u0435\u0447\u043d\u043e \u0432\u0438\u0441\u044f\u0442 \u0432 \u043f\u0443\u043b\u0435 \u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u0443\u0442\u0435\u0447\u043a\u0430.<\/p>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <strong>Cgo<\/strong>, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<h2>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0442\u0435\u0447\u0435\u043a<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0442\u0438\u043f\u0430\u043c\u0438 \u0443\u0442\u0435\u0447\u0435\u043a \u2013 \u043a\u0430\u043a \u0438\u0445 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c?<\/p>\n<p><strong>1) \u041f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0433\u043b\u0430\u0437\u0430\u043c\u0438<\/strong><\/p>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043a\u043e\u0434\u0443 \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u043e \u043c\u043d\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0443\u0442\u0435\u0447\u043a\u0438. \u0423 Go \u0435\u0441\u0442\u044c \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u2013 \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 http \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c response body. \u041f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2013 \u0437\u0430\u043a\u0440\u044b\u0442\u044c rows. \u041f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u2013 \u0444\u0430\u0439\u043b \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0442\u044c. \u042d\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u2013\u00a0 \u0434\u0430\u0436\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e \u044d\u0442\u0438 \u0448\u0430\u0433\u0438, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439, \u0433\u0434\u0435 \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u043b\u0438 http reponse body<\/em><\/p>\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      \/\/ we need not empty response from the server to receive a leak      resp, err := http.Get(\"http:\/\/localhost:9091\/foo\/baz\")      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE resp.Body!!!      \/\/ defer resp.Body.Close()        ret := fmt.Sprintf(\"external service returned %d\", resp.StatusCode)      writer.Write([]byte(ret))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439, \u0433\u0434\u0435 \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u043b\u0438 rows<\/em><\/p>\n<pre><code class=\"go\">package main   import (   \"database\/sql\"   \"encoding\/json\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\"     \"github.com\/google\/uuid\"   _ \"github.com\/lib\/pq\" )   type User struct {   ID   uuid.UUID `db:\"id\" json:\"id\"`   Name string    `db:\"name\" json:\"name\"` }   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     db, err := sql.Open(\"postgres\", \"postgres:\/\/127.0.0.1:5432\/test_db?sslmode=disable\")   if err != nil {      log.Fatal(err)   }   if err := db.Ping(); err != nil {      log.Fatal(err)   }     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      var (         users []User         user  User      )        query := \"select id, name from users limit 5\"      rows, err := db.Query(query)      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE ROWS!!!      \/\/ defer rows.Close()        \/\/ We need more than one row in the table to receive a leak      for rows.Next() {         if err := rows.Scan(&amp;user.ID, &amp;user.Name); err != nil {            writer.WriteHeader(http.StatusInternalServerError)            return         }         users = append(users, user)         break      }      if rows.Err() != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }        encoder := json.NewEncoder(writer)      if err := encoder.Encode(users); err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><strong>2) \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 (\u0441\u043c. \u041f\u0443\u043d\u043a\u0442 1) \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u043b\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0443\u0442\u0435\u0447\u043a\u0438, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 pprof. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <em>&#171;pprof is a tool for visualization and analysis of profiling data. pprof reads a collection of profiling samples in profile.proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).&#187;<\/em><\/p>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f pprof \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 import <em>&#171;net\/http\/pprof&#187;<\/em>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043e http \u043f\u043e\u0440\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d url <em>&#171;\/debug\/pprof\/&#187;<\/em>, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438 \u0438\u0441\u043a\u043e\u043c\u043e\u0433\u043e pprof.<\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f pprof:<\/p>\n<ul>\n<li>\n<p><strong>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c web \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 graph, \u0444\u043b\u0435\u0439\u043c-\u0433\u0440\u0430\u0444\u043e\u043c \u0438 \u0442.\u0434.<\/strong><br \/><em>go tool pprof -http=:8081 <\/em><a href=\"http:\/\/localhost:9090\/debug\/pprof\/goroutine\" rel=\"noopener noreferrer nofollow\"><em>http:\/\/localhost:9090\/debug\/pprof\/goroutine<\/em><\/a><\/p>\n<\/li>\n<li>\n<p><strong>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c pprof.<\/strong><br \/><em>go tool pprof <\/em><a href=\"http:\/\/localhost:9090\/debug\/pprof\/goroutine\" rel=\"noopener noreferrer nofollow\"><em>http:\/\/localhost:9090\/debug\/pprof\/goroutine<\/em><\/a><br \/>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 <em>web<\/em> \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0433\u0440\u0430\u0444\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434 \u0438 \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e url <em>&#171;\/debug\/pprof&#187;<\/em>, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a \u043f\u0440\u043e\u0434\u0443 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <em>&#171;go tool pprof  test pprof_goroutine&#187;<\/em> \u0438\u043b\u0438 <em>&#171;go tool pprof -http=:8081 test pprof_goroutine&#187;<\/em>. <em>test<\/em> \u2013 \u044d\u0442\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0438 <em>pprof_goroutine<\/em> \u2013 \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u0441\u043a\u0430\u0447\u0430\u043d\u043d\u044b\u0439 \u0441 \u043f\u0440\u043e\u0434\u0430 \u043f\u0440\u043e\u0444\u0438\u043b\u044c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0430\u043d\u0435\u0435 \u0441\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u0447\u0438\u043d \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b \u0443\u0442\u0435\u0447\u043a\u0443 \u0433\u043e\u0440\u0443\u0442\u0438\u043d. \u041f\u043e \u043c\u043e\u0435\u043c\u0443 \u043e\u043f\u044b\u0442\u0443, \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0435.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441 \u0443\u0442\u0435\u043a\u0430\u044e\u0449\u0438\u043c\u0438 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0432\u044b\u0448\u0435<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      \/\/ we need not empty response from the server to receive a leak      resp, err := http.Get(\"http:\/\/localhost:9091\/foo\/baz\")      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE resp.Body!!!      \/\/ defer resp.Body.Close()        ret := fmt.Sprintf(\"external service returned %d\", resp.StatusCode)      writer.Write([]byte(ret))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p><em>\u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0443<\/em><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/361\/f1b\/da3\/361f1bda306a6b569f40384c48cd0e7f.png\" width=\"2030\" height=\"1823\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/361\/f1b\/da3\/361f1bda306a6b569f40384c48cd0e7f.png\"\/><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0443\u0442\u0435\u0447\u043a\u0438 \u2013 \u0443\u0442\u0435\u0447\u043a\u0430 \u043a\u043e\u043d\u043d\u0435\u043a\u0442\u043e\u0432, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0440\u0430\u0441\u0442\u0443\u0449\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0445 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u043e\u0432 <em>ls -la \/proc\/{pid}\/fd.<\/em><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u044c heap. <br \/>\u0422\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043a\u043e\u0434 \u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u0438\u0448\u0435\u0442 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e.<\/p>\n<pre><code class=\"go\">package main   import (   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   var someMap = make(map[*http.Request][]int)   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      someMap[request] = make([]int, 10000)      \/\/delete(someMap, request)        writer.Write([]byte(\"foo bar response\"))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><em>\u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0445\u0438\u043f\u0430 \u043f\u043e\u0441\u043b\u0435 2000 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/em><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e5a\/fbe\/c7d\/e5afbec7d1ba947d725a248ef0e01367.png\" width=\"1636\" height=\"1552\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e5a\/fbe\/c7d\/e5afbec7d1ba947d725a248ef0e01367.png\"\/><\/figure>\n<p><strong>3) runtime.MemStats<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0443\u043d\u043a\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b, \u043d\u043e \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u043e\u0431\u0435\u0434\u0438\u0442\u044c \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c? \u0422\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>runtime.MemStats<\/em>. \u042d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0445\u0438\u043f\u0435 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0445\u0438\u043f\u0435, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u043e\u0432 \u0437\u0430\u043d\u044f\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0441\u043f\u0430\u043d\u0430\u043c\u0438 (\u0435\u0434\u0438\u043d\u0438\u0446\u044b \u0432 Go \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0442.\u0435. Go \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0443\u0447\u0438 \u043d\u0430 \u0441\u043f\u0430\u043d\u044b).<\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0432\u043e\u0441\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u2013 \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0434\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\/\u0443\u0447\u0430\u0441\u0442\u043a\u0430 \u043a\u043e\u0434\u0430, \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 <em>MemStats<\/em>.<\/p>\n<p><strong>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0417\u0434\u043e\u0440\u043e\u0432\u043e, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0438\u043b\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430, \u043d\u043e \u0442\u0430\u043a \u0431\u044b\u0432\u0430\u0435\u0442 \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0438 \u043f\u043e\u0438\u0441\u043a \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u043c. \u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433. \u0415\u0441\u043b\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u043d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u043d\u0434\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0442\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u0435\u0433\u043e, \u0447\u0435\u043c \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u0447\u0430\u0441\u044b \u043d\u0430 \u0442\u0440\u0435\u0439\u0441\u0438\u043d\u0433 \u0438 \u0434\u0435\u0431\u0430\u0433\u0433\u0438\u043d\u0433.<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<p><a href=\"https:\/\/go.dev\/blog\/pprof\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e pprof<\/a><br \/><a href=\"https:\/\/github.com\/google\/pprof\/blob\/main\/doc\/README.md#interpreting-the-callgraph\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0447\u0438\u0442\u0430\u0442\u044c \u0433\u0440\u0430\u0444<\/a><br \/><a href=\"https:\/\/www.youtube.com\/watch?v=xxDZuPEgbBU\" rel=\"noopener noreferrer nofollow\">Profiling &amp; Optimizing in Go \/ Brad Fitzpatrick<\/a><br \/><a href=\"https:\/\/go.dev\/blog\/pipelines\" rel=\"noopener noreferrer nofollow\">Go Concurrency Patterns: Pipelines and cancellation<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/724402\/\"> https:\/\/habr.com\/ru\/post\/724402\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0432 Go-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438: \u0447\u0435\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0443\u0442\u0435\u0447\u043a\u0438 \u0438 \u0441 \u0447\u0435\u0433\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h2>\u041f\u0440\u0438\u0447\u0438\u043d\u044b \u0443\u0442\u0435\u0447\u0435\u043a<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<p><strong>1) \u0423\u0442\u0435\u0447\u043a\u0430 \u0433\u043e\u0440\u0443\u0442\u0438\u043d<\/strong><\/p>\n<p>\u041f\u0440\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u0442\u0435\u043a\u0430\u044e\u0442 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0435\u0435 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0441\u0442\u0435\u043a \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439 \u0433\u043e\u0440\u0443\u0442\u0438\u043d:<\/p>\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\" )   func process(ch chan string) {   for v := range ch {      log.Println(v)   } }   func main() {   http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      ch := make(chan string)      \/\/ We forgot to close the channel. As the result process never finishes      \/\/ defer close(ch)        go process(ch)        for i := 0; i &lt; 5; i++ {         ch &lt;- fmt.Sprintf(\"text %d\", i)      }        writer.Write([]byte(\"for bar response\"))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) } <\/code><\/pre>\n<p><strong>2) \u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u043f\u0443, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0443\u0442\u0435\u043a\u0430\u0442\u044c. \u041e\u0434\u0438\u043d \u0440\u0430\u0437 \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0443\u0442\u0435\u0447\u043a\u0443 \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u043e gorilla context. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 http \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u043d\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u043f\u0443 \u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u043a\u043b\u044e\u0447 \u043c\u0430\u043f\u044b \u0431\u0435\u0437 \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u043a\u043e\u0434\u0435. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 Go 1.7, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 gorilla \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c http.Request.Context()<\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0435\u043d\u0430 \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0442\u0438\u043f\u043e\u0432 \u0443\u0442\u0435\u0447\u0435\u043a \u043a\u0430\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>3) sync.Pool, Cgo<\/strong><\/p>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u043d\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u043e sync.Pool \u0438 Cgo.<\/p>\n<p><strong>Sync.Pool<\/strong><br \/>\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <em>&#171;A Pool is a set of temporary objects that may be individually saved and retrieved.&#187;<\/em> \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f <a href=\"https:\/\/github.com\/golang\/go\/issues\/23199\" rel=\"noopener noreferrer nofollow\">issue<\/a>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u044b \u0432\u0435\u0447\u043d\u043e \u0432\u0438\u0441\u044f\u0442 \u0432 \u043f\u0443\u043b\u0435 \u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u0443\u0442\u0435\u0447\u043a\u0430.<\/p>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <strong>Cgo<\/strong>, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<h2>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0442\u0435\u0447\u0435\u043a<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0442\u0438\u043f\u0430\u043c\u0438 \u0443\u0442\u0435\u0447\u0435\u043a \u2013 \u043a\u0430\u043a \u0438\u0445 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c?<\/p>\n<p><strong>1) \u041f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u0433\u043b\u0430\u0437\u0430\u043c\u0438<\/strong><\/p>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043a\u043e\u0434\u0443 \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u043e \u043c\u043d\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0443\u0442\u0435\u0447\u043a\u0438. \u0423 Go \u0435\u0441\u0442\u044c \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u2013 \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 http \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c response body. \u041f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2013 \u0437\u0430\u043a\u0440\u044b\u0442\u044c rows. \u041f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u2013 \u0444\u0430\u0439\u043b \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0442\u044c. \u042d\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u2013\u00a0 \u0434\u0430\u0436\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e \u044d\u0442\u0438 \u0448\u0430\u0433\u0438, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439, \u0433\u0434\u0435 \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u043b\u0438 http reponse body<\/em><\/p>\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      \/\/ we need not empty response from the server to receive a leak      resp, err := http.Get(\"http:\/\/localhost:9091\/foo\/baz\")      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE resp.Body!!!      \/\/ defer resp.Body.Close()        ret := fmt.Sprintf(\"external service returned %d\", resp.StatusCode)      writer.Write([]byte(ret))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0443\u0442\u0435\u0447\u043a\u043e\u0439, \u0433\u0434\u0435 \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u043b\u0438 rows<\/em><\/p>\n<pre><code class=\"go\">package main   import (   \"database\/sql\"   \"encoding\/json\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\"     \"github.com\/google\/uuid\"   _ \"github.com\/lib\/pq\" )   type User struct {   ID   uuid.UUID `db:\"id\" json:\"id\"`   Name string    `db:\"name\" json:\"name\"` }   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     db, err := sql.Open(\"postgres\", \"postgres:\/\/127.0.0.1:5432\/test_db?sslmode=disable\")   if err != nil {      log.Fatal(err)   }   if err := db.Ping(); err != nil {      log.Fatal(err)   }     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      var (         users []User         user  User      )        query := \"select id, name from users limit 5\"      rows, err := db.Query(query)      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE ROWS!!!      \/\/ defer rows.Close()        \/\/ We need more than one row in the table to receive a leak      for rows.Next() {         if err := rows.Scan(&amp;user.ID, &amp;user.Name); err != nil {            writer.WriteHeader(http.StatusInternalServerError)            return         }         users = append(users, user)         break      }      if rows.Err() != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }        encoder := json.NewEncoder(writer)      if err := encoder.Encode(users); err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><strong>2) \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 (\u0441\u043c. \u041f\u0443\u043d\u043a\u0442 1) \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u043b\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0443\u0442\u0435\u0447\u043a\u0438, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 pprof. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <em>&#171;pprof is a tool for visualization and analysis of profiling data. pprof reads a collection of profiling samples in profile.proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).&#187;<\/em><\/p>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f pprof \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 import <em>&#171;net\/http\/pprof&#187;<\/em>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043e http \u043f\u043e\u0440\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d url <em>&#171;\/debug\/pprof\/&#187;<\/em>, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438 \u0438\u0441\u043a\u043e\u043c\u043e\u0433\u043e pprof.<\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f pprof:<\/p>\n<ul>\n<li>\n<p><strong>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c web \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 graph, \u0444\u043b\u0435\u0439\u043c-\u0433\u0440\u0430\u0444\u043e\u043c \u0438 \u0442.\u0434.<\/strong><br \/><em>go tool pprof -http=:8081 <\/em><a href=\"http:\/\/localhost:9090\/debug\/pprof\/goroutine\" rel=\"noopener noreferrer nofollow\"><em>http:\/\/localhost:9090\/debug\/pprof\/goroutine<\/em><\/a><\/p>\n<\/li>\n<li>\n<p><strong>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c pprof.<\/strong><br \/><em>go tool pprof <\/em><a href=\"http:\/\/localhost:9090\/debug\/pprof\/goroutine\" rel=\"noopener noreferrer nofollow\"><em>http:\/\/localhost:9090\/debug\/pprof\/goroutine<\/em><\/a><br \/>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 <em>web<\/em> \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0433\u0440\u0430\u0444\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434 \u0438 \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e url <em>&#171;\/debug\/pprof&#187;<\/em>, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a \u043f\u0440\u043e\u0434\u0443 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <em>&#171;go tool pprof  test pprof_goroutine&#187;<\/em> \u0438\u043b\u0438 <em>&#171;go tool pprof -http=:8081 test pprof_goroutine&#187;<\/em>. <em>test<\/em> \u2013 \u044d\u0442\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0438 <em>pprof_goroutine<\/em> \u2013 \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u0441\u043a\u0430\u0447\u0430\u043d\u043d\u044b\u0439 \u0441 \u043f\u0440\u043e\u0434\u0430 \u043f\u0440\u043e\u0444\u0438\u043b\u044c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0430\u043d\u0435\u0435 \u0441\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u0447\u0438\u043d \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b \u0443\u0442\u0435\u0447\u043a\u0443 \u0433\u043e\u0440\u0443\u0442\u0438\u043d. \u041f\u043e \u043c\u043e\u0435\u043c\u0443 \u043e\u043f\u044b\u0442\u0443, \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0435.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441 \u0443\u0442\u0435\u043a\u0430\u044e\u0449\u0438\u043c\u0438 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0432\u044b\u0448\u0435<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main   import (   \"fmt\"   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      \/\/ we need not empty response from the server to receive a leak      resp, err := http.Get(\"http:\/\/localhost:9091\/foo\/baz\")      if err != nil {         writer.WriteHeader(http.StatusInternalServerError)         return      }      \/\/ !!!WE FORGOT TO CLOSE resp.Body!!!      \/\/ defer resp.Body.Close()        ret := fmt.Sprintf(\"external service returned %d\", resp.StatusCode)      writer.Write([]byte(ret))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p><em>\u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0443<\/em><\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0443\u0442\u0435\u0447\u043a\u0438 \u2013 \u0443\u0442\u0435\u0447\u043a\u0430 \u043a\u043e\u043d\u043d\u0435\u043a\u0442\u043e\u0432, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0440\u0430\u0441\u0442\u0443\u0449\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0445 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u043e\u0432 <em>ls -la \/proc\/{pid}\/fd.<\/em><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u044c heap. <br \/>\u0422\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043a\u043e\u0434 \u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043f\u0438\u0448\u0435\u0442 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e.<\/p>\n<pre><code class=\"go\">package main   import (   \"log\"   \"net\/http\"   _ \"net\/http\/pprof\"   \"os\" )   var someMap = make(map[*http.Request][]int)   func main() {   log.Printf(\"pid: %d\\n\", os.Getpid())     http.HandleFunc(\"\/foo\/bar\", func(writer http.ResponseWriter, request *http.Request) {      someMap[request] = make([]int, 10000)      \/\/delete(someMap, request)        writer.Write([]byte(\"foo bar response\"))   })     log.Fatal(http.ListenAndServe(\":9090\", nil)) }<\/code><\/pre>\n<p><em>\u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0445\u0438\u043f\u0430 \u043f\u043e\u0441\u043b\u0435 2000 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/em><\/p>\n<figure class=\"full-width\"><\/figure>\n<p><strong>3) runtime.MemStats<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0443\u043d\u043a\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b, \u043d\u043e \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u043e\u0431\u0435\u0434\u0438\u0442\u044c \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c? \u0422\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>runtime.MemStats<\/em>. \u042d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0445\u0438\u043f\u0435 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0445\u0438\u043f\u0435, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u043e\u0432 \u0437\u0430\u043d\u044f\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0441\u043f\u0430\u043d\u0430\u043c\u0438 (\u0435\u0434\u0438\u043d\u0438\u0446\u044b \u0432 Go \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0442.\u0435. Go \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0443\u0447\u0438 \u043d\u0430 \u0441\u043f\u0430\u043d\u044b).<\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0432\u043e\u0441\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u2013 \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0434\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\/\u0443\u0447\u0430\u0441\u0442\u043a\u0430 \u043a\u043e\u0434\u0430, \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 <em>MemStats<\/em>.<\/p>\n<p><strong>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0417\u0434\u043e\u0440\u043e\u0432\u043e, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0438\u043b\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430, \u043d\u043e \u0442\u0430\u043a \u0431\u044b\u0432\u0430\u0435\u0442 \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0438 \u043f\u043e\u0438\u0441\u043a \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u043c. \u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433. \u0415\u0441\u043b\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u043d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u043d\u0434\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0442\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u0435\u0433\u043e, \u0447\u0435\u043c \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u0447\u0430\u0441\u044b \u043d\u0430 \u0442\u0440\u0435\u0439\u0441\u0438\u043d\u0433 \u0438 \u0434\u0435\u0431\u0430\u0433\u0433\u0438\u043d\u0433.<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<p><a href=\"https:\/\/go.dev\/blog\/pprof\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e pprof<\/a><br \/><a href=\"https:\/\/github.com\/google\/pprof\/blob\/main\/doc\/README.md#interpreting-the-callgraph\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0447\u0438\u0442\u0430\u0442\u044c \u0433\u0440\u0430\u0444<\/a><br \/><a href=\"https:\/\/www.youtube.com\/watch?v=xxDZuPEgbBU\" rel=\"noopener noreferrer nofollow\">Profiling &amp; Optimizing in Go \/ Brad Fitzpatrick<\/a><br \/><a href=\"https:\/\/go.dev\/blog\/pipelines\" rel=\"noopener noreferrer nofollow\">Go Concurrency Patterns: Pipelines and cancellation<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/724402\/\"> https:\/\/habr.com\/ru\/post\/724402\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-347182","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=347182"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347182\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}