Actually, this is interesting, since it requires the new capacity to be less 
than the existing capacity, I surmise it shares the underlying array, but since 
the new capacity is 0, as soon as you append it is going to allocate a new 
array - which you don’t need or want.

> On Aug 26, 2024, at 10:48 AM, robert engels <reng...@ix.netcom.com> wrote:
> 
> actually, change it to models = models[0:0], not models=models[0:0:0], 
> otherwise you are causing new arrays to be allocated on each loop.
> 
>> On Aug 26, 2024, at 10:45 AM, robert engels <reng...@ix.netcom.com 
>> <mailto:reng...@ix.netcom.com>> wrote:
>> 
>> 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 
>>> <mailto: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 <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/EAE7F3B6-F593-45A0-89F9-8B0C7329269A%40ix.netcom.com.

Reply via email to