Home  >  Article  >  Backend Development  >  Unable to free memory byte buffer

Unable to free memory byte buffer

WBOY
WBOYforward
2024-02-06 09:51:03855browse

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

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
}

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
}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete