I had models = []mongo.WriteModel{} before , then tried with models = nil, and models[:0]. All same result. For []mongo.WriteModel{} , I expected that memory gets freed up, but it does not. Everytime the append runs to mongo.WriteModel{} memory consumption increases. My doubt is if the models object created as global and not within the for loop is causing any issue -?
On Mon, 26 Aug 2024 at 21:48, Marvin Renich <m...@renich.org> wrote: > * Gowtham Raj <rgowtham...@gmail.com> [240826 11:31]: > > > > > > I have a sample go snippet which pulls 50k rows from source DB and > inserts > > into target DB. https://go.dev/play/p/IVANZyVUbkm > > > > 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 do a > > copy as suggested in few articles and reset it but it never gives up on > the > > memory and the script consumes more and more as it runs. > > > > Any idea as to who to release the memory of slice every 10k docs ? > > > > I have reset the slice, but memory is not returned back. > > Copying the slice to modelsCopy does nothing but allocate more memory, > which will be freed when the garbage collector gets around to it. Take > out this code. > > The statement > > models = models[:0] > > does not free the memory allocated for the slice's backing array, but it > does cause future appends to reuse slice elements from the beginning of > the array. What you probably want is > > models = []mongo.WriteModel{} > > which will mark the old backing array for collection and create a new > slice. > > ...Marvin > > -- > You received this message because you are subscribed to a topic in the > Google Groups "golang-nuts" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/golang-nuts/Vw04RLUpRcc/unsubscribe. > To unsubscribe from this group and all its topics, 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/Zsyqz8zgxvf8cjYE%40basil.wdw > . > -- 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/CAE-6Q_LOLc1is5ybZbW3D2jVvnnc4QerQZt4aWXz2RMeqOHZow%40mail.gmail.com.