Unable to free memory byte buffer

WBOY
Release: 2024-02-06 09:51:03
forward
854 people have browsed it

Unable to free memory byte buffer

Question content

go 1.18.1

pprof report

3549.93kb 49.73% 49.73%  3549.93kb 49.73%  src/lag_monitor.publishlagmetrictodatadog
     514kb  7.20% 56.93%      514kb  7.20%  bufio.newwritersize
  512.88kb  7.18% 64.11%   512.88kb  7.18%  encoding/pem.decode
  512.69kb  7.18% 71.30%  1536.98kb 21.53%  crypto/x509.parsecertificate
  512.50kb  7.18% 78.48%   512.50kb  7.18%  crypto/x509.(*certpool).addcert
Copy after login

This code does not seem to release memory. According to pprof, the following functions are the functions that consume the most memory. memory map

func caller() {
 events := make([]string, 0)
 //....
 PublishLagMetricToDataDog(ctx, strings.Join(events, ","))
}

func PublishLagMetricToDataDog(ctx context.Context, events string) error {
msg := `{
    "series": [%v]
}`
b := []byte(msg)
resp, err := http.Post("https://api.datadoghq.com/api/v1/series?api_key="+env.GetDataDogKey(), "application/json", bytes.NewBuffer(b))
if err != nil {
    logger.Error(ctx, "Error submitting event to datadog, err = ", err)
    return err
}
logger.Info(ctx, resp)
return nil
}
Copy after login

The above function is called in a loop. Since there are no global variables and no reference to the byte slice in publishlagmetrictodatadog, I can't pinpoint the memory leak. I read about reset() and truncate() but this does not free the underlying memory.


Correct Answer


You must close the response body for every http response you receive. Failure to do so may result in resource leaks, as you observed.

solution:

    resp, err := http.Post("https://api.datadoghq.com/api/v1/series?api_key="+env.GetDataDogKey(), "application/json", bytes.NewBuffer(b))
    if err != nil {
        logger.Error(ctx, "Error submitting event to datadog, err = ", err)
        return err
    }
    logger.Info(ctx, resp)
    _ = resp.Body.Close() // <--- Add this
    return nil
}
Copy after login

The above is the detailed content of Unable to free memory byte buffer. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!