英文:
Do variables in a loop get garbage collected or do they stay in memory?
问题
这段代码中的变量在循环结束后会被垃圾回收,所以不会永远在内存中存在。如果你想更高效地处理,只需要在将int
转换为string
并追加到byte
之后,及时释放sSteamId
变量的内存。
以下是修改后的代码示例:
for _, id := range steamIds {
sSteamId := strconv.Itoa(id)
requestURI = append(requestURI, ","+sSteamId...)
// 在这里添加代码,手动释放 sSteamId 变量的内存
sSteamId = ""
}
通过将sSteamId
变量设置为空字符串,可以让垃圾回收器更早地回收该变量所占用的内存空间。这样可以更有效地管理内存。
英文:
Does the variable in this bit of code get garbage collected after the loop finishes or do I have X amount of sSteamId
variables floating in memory forever?
If it does, how can I do this more efficiently? I only need sSteamId
long enough to convert an int
to a string
and then append it to a byte
, then it's no longer needed
for _, id := range steamIds {
sSteamId := strconv.Itoa(id)
requestURI = append(requestURI, ","+sSteamId...)
}
答案1
得分: 2
他们将被垃圾回收,因为每次迭代时对它们的引用都会丢失。
英文:
They'll get GC'd as any references to them are lost with each iteration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论