Friday, October 10, 2025
HomeGolangRubbish collector - Technical Dialogue

Rubbish collector – Technical Dialogue


I’ve international map duplcates, I’m working the map inside a for loop and every time I do
duplcates = make(map[int]bool). I need to know if the earlier reminiscence of the map is free once we do duplcates = make(map[int]bool) once more?

Hello and welcome! So that you’re doing one thing like this:

bundle foremost

var duplicates = make(map[int]bool)

func foremost()  {
	for {
		duplicates = make(map[int]bool)
	}
}

Properly, on this case GC will allocate the minimal quantity of reminiscence mandatory for a map (the basis of the hash map solely) every time, and it’ll launch every allocation in type of bunch, since GC runs at a particular time “periodically” (see the GC algorithm). So the reply isn’t any, not every time, it might be too costly.

I examined it with heap and hint pprof and from what I can inform, reminiscence might be reused and GC calls weren’t very costly. The code. I used map with 100000 components, with out altering the dimensions dynamically. However as you possibly can see from the heap analyse:

the house itself was not an excessive amount of. And if we take a look on the hint, the heap measurement stays everlasting, which suggests, that GC gathering reminiscence very effectively.
I gathered statistics inside 60 seconds utilization of this system. Common GC runtime is 1,968,642 ns on my machine and imho it is usually not an excessive amount of.
To sum this up, the reminiscence allotted in your map might be reused on each make name. I’d recommend if you already know approximate measurement of the map, cross its measurement into the make name.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments