gpt4 book ai didi

Go json.NewDecoder vs. json.Unmarshal Memory(转到json。NewDecoder与json。解组内存)

转载 作者:bug小助手 更新时间:2023-10-22 16:56:49 26 4
gpt4 key购买 nike



I'm currently learning Go and have built a small API in which I search for and summarize data from several other APIs.

我目前正在学习Go,并构建了一个小型的API,在其中搜索和汇总来自其他几个API的数据。


Now I noticed that if I use the following to access the data of multiple requests

现在我注意到,如果我使用以下方法访问多个请求的数据


json.NewDecoder(resp.Body).Decode(&example)

the memory usage of the API spikes up to about 250mb

API的内存使用量飙升至约250mb


but if I use

但是如果我使用


byteBody, err := io.ReadAll(resp.Body)
err = json.Unmarshal(byteBody, &example)

instead the memory usage is like 150mb less.

相反,内存使用量大约减少了150mb。


does anyone have an explanation for this? Sorry if this is a dump question but im still learning :D

有人对此有解释吗?很抱歉,如果这是一个垃圾问题,但我仍在学习:D


更多回答

Generally using a decoder (json.NewDecoder()) should be more efficient in terms of memory usage as that doesn't require reading and holding all JSON data in memory at once. However, this doesn't apply to all JSON inputs, there may be inputs that give opposite results as in your case. We don't see your inputs, so we can't give more insights. If this matters to you, use whichever is more efficient to you.

通常,使用解码器(json.NewDecoder())在内存使用方面应该更高效,因为这不需要一次读取并保存内存中的所有json数据。然而,这并不适用于所有的JSON输入,可能会有与您的情况相反的结果。我们看不到您的意见,因此无法提供更多见解。如果这对你来说很重要,那就用对你来说更有效的。

Thank you for your answer. So there is no correct or wrong way when using one of this two approaches?

谢谢你的回答。那么,当使用这两种方法中的一种时,没有正确或错误的方法?

There is no solution that's best in all scenarios. You can optimize to certain inputs from certain aspects. Generally json.NewDecoder() uses less memory. If your input JSON is so large that it doesn't fit into memory, then json.Unmarshal() is obviously out of the question, while json.Decoder may easily process that.

没有一个解决方案在所有情况下都是最好的。您可以从某些方面对某些输入进行优化。一般为json。NewDecoder()使用较少的内存。如果您的输入JSON太大以至于无法放入内存,那么就选择JSON。Unmarshal()显然是不可能的,而json。解码器可以很容易地处理它。

1. "memory usage of the API spikes up to about 250mb" All this "memory usage" is complicated. Unless you exactly define what kind of memory usage you are talking about knowbody can help you. 2. 250mb is nothing, absoluteliy nothing, just forget about it. 3. Chances your experiment actually measures what you think are low.

1.“API的内存使用量飙升至约250mb”所有这些“内存使用量”都很复杂。除非你确切地定义了你所说的知识体可以帮助你的记忆用法。2.250mb不算什么,绝对不算什么。你的实验实际上测量你认为低的东西的可能性。

优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com