-2

I am making a slice of a structure (user session)

I wat to manage only the current user session in this

So i add a session when user login And remove when user logout

I was wondering if this can consume memory, if all the removed sessions are present in the underlying array.

  • 5
    That depends. Please show your code. – Adrian Oct 25 '19 at 17:19
  • If you cut it out of the middle of the slice, for example then no. The runtime will most likely reallocate your slice (and its underlying array) and copy over the values. removing something from the end of a slice can preserve the capacity (size of underlying array), which is already consuming memory. Even if the element is released, the go GC flags before sweeping, so it's not freed immediately – Elias Van Ootegem Oct 25 '19 at 17:38
  • Possible duplicate of [Memory leak in golang slice](https://stackoverflow.com/questions/55045402/memory-leak-in-golang-slice/55046506?r=SearchResults#55046506); and [Does go garbage collect parts of slices?](https://stackoverflow.com/a/28432812/1705598) – icza Oct 25 '19 at 20:26
  • Thanka icza for referring the link – anirudh adiga Oct 26 '19 at 05:09

1 Answers1

0

Yes the underlying array will remain so the removed item from the slice will still be there in the memory. best way to handle it is set the item to be removed to 'Zero value' before removing it from the slice.