gopher-lua icon indicating copy to clipboard operation
gopher-lua copied to clipboard

table map memery leak

Open zgame opened this issue 7 years ago • 6 comments

  1. What version of GopherLua are you using? : newest
  2. What version of Go are you using? : 1.10
  3. What operating system and processor architecture are you using? : windows
  4. What did you do? : game
  5. What did you expect to see? :
  6. 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!

zgame avatar Dec 25 '18 06:12 zgame


	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)
		}
	}

zgame avatar Dec 25 '18 07:12 zgame

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 avatar Dec 26 '18 09:12 zgame

@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.

yuin avatar Jan 07 '19 02:01 yuin

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)
	}
}

markuya avatar Apr 06 '20 09:04 markuya

thx

zgame avatar May 14 '20 05:05 zgame

@zgame you can check #283

chyh1990 avatar May 25 '20 08:05 chyh1990