It is working as expected. You allocate models to hold 10k elements - you never put more than 10k elements in it, so it will not consume additional memory. (You should probably make the comparison == 10000, not > 10000 to avoid the extra array allocation.
GC is non deterministic - so it will only free memory if/when it needs to. I would perform a heap dump to understand exactly what is consuming the memory if it is continually increasing - I suspect it may be in the MongoDB driver - or it is just dead objects waiting to be collected. > On Aug 26, 2024, at 8:24 AM, Gowtham Raj <rgowtham...@gmail.com> wrote: > > Hello team, > > MongoDB Go bulk write takes up an slice of interfaces which is slice of > operations. > https://go.dev/play/p/2vcHx5EFK5x > While doing so I create a empty slice and add operations to it. When the > length of slice is > 10K, I do a bulk write to DB. > > The problem here is this script eats more memory as it runs, I suspect issue > is with the slice which gets appended periodically. I try to reset the slice > but it never gets freed up. Alloc in runtime.MemStats grows to 1 GB > immediately within 1 minute. > > Any idea as to who to release the memory of slice every 10k docs ? > The length and cap of slice goes back to zero, but GC does not clean it up. > Not sure if since its in the for loop the variable is still being referenced. > How do solve this problem ? > Regards, > Gowtham > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/0be9320a-e004-49b0-a88f-79158c1ed0d2n%40googlegroups.com > > <https://groups.google.com/d/msgid/golang-nuts/0be9320a-e004-49b0-a88f-79158c1ed0d2n%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/281D4ADC-D62F-4EEB-84CF-62D143E3F8BC%40ix.netcom.com.