But why would you want to allocate a new array? This would only be required if 
the MongoDB call were async, and the array cannot be reused.

If you keep allocating new arrays you are putting needless pressure on the GC.

> On Aug 26, 2024, at 11:18 AM, Marvin Renich <m...@renich.org> wrote:
> 
> * Gowtham Raj <rgowtham...@gmail.com <mailto: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 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/Zsyqz8zgxvf8cjYE%40basil.wdw 
> <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/BFC0E448-0FBF-438E-807B-3874C9ED18E6%40ix.netcom.com.

Reply via email to