Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table map memery leak #214

Open
zgame opened this issue Dec 25, 2018 · 6 comments · May be fixed by #515
Open

table map memery leak #214

zgame opened this issue Dec 25, 2018 · 6 comments · May be fixed by #515

Comments

@zgame
Copy link

zgame commented Dec 25, 2018

  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
Copy link
Author

zgame commented Dec 25, 2018


	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
Copy link
Author

zgame commented Dec 26, 2018

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

@yuin
Copy link
Owner

yuin commented Jan 7, 2019

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

@markuya
Copy link

markuya commented Apr 6, 2020

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

@zgame
Copy link
Author

zgame commented May 14, 2020

thx

@chyh1990
Copy link

@zgame you can check #283

VigorFox added a commit to VigorFox/gopher-lua that referenced this issue Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants