table map memery leak
- What version of GopherLua are you using? : newest
- What version of Go are you using? : 1.10
- What operating system and processor architecture are you using? : windows
- What did you do? : game
- What did you expect to see? :
- What did you see instead? :
gopher-lua table map memery leak
GlobalMap= {}
-- create Global Map hash
for i=1,500000 do
GlobalMap[tostring(i)] = i
end
-- clear Global Map hash element
for i,_ in pairs(GlobalMap)do
GlobalMap[tostring(i)] = nil -- memery leak
end
-- if you clear all data, it's ok, but, if you clear some data, memery leak
--GlobalMap = {}
--collectgarbage()
I use map hash , element = nil some memery can't clear , how to do this ? thanks!
if value == LNil {
// TODO tb.keys and tb.k2i should also be removed
delete(tb.dict, key)
} else {
tb.dict[key] = value
if _, ok := tb.k2i[key]; !ok {
tb.k2i[key] = len(tb.keys)
tb.keys = append(tb.keys, key)
}
}
if value == LNil {
// TODO tb.keys and tb.k2i should also be removed
delete(tb.dict, key)
//-------------------- add
lkey := LString(key)
oldIndex := tb.k2i[lkey]
tb.keys = append(tb.keys[:oldIndex],tb.keys[oldIndex+1:]...) // remove keys
delete(tb.k2i, lkey) // remove k2i
for k,v:=range tb.k2i{
if v > oldIndex {
tb.k2i[k] --
}
}
//-------------------- end
} else {
tb.dict[key] = value
if _, ok := tb.k2i[key]; !ok {
tb.k2i[key] = len(tb.keys)
tb.keys = append(tb.keys, key)
}
}
@zgame Thank you to write this patch. Though this patch works well, I suspect this patch causes a moderate performance degradation when the table has larger size.
It is true that this is a tradeoff between memory performance and time performance, but I would like to consider better way to iterate lua tables.
if value == LNil { // TODO tb.keys and tb.k2i should also be removed delete(tb.strdict, key)
//-------------------- add
lkey := LString(key)
if oldIndex,ok := tb.k2i[lkey];ok { // check key
tb.keys = append(tb.keys[:oldIndex],tb.keys[oldIndex+1:]...) // remove keys
delete(tb.k2i, lkey) // remove k2i
for k,v:=range tb.k2i{
if v > oldIndex {
tb.k2i[k] --
}
}
}
//-------------------- end
} else {
tb.strdict[key] = value
lkey := LString(key)
if _, ok := tb.k2i[lkey]; !ok {
tb.k2i[lkey] = len(tb.keys)
tb.keys = append(tb.keys, lkey)
}
}
thx
@zgame you can check #283