While letting the program run for a few hours I see that after some time 
the memory usage vary in saw teeth. Some of the garbage is reclaimed after 
800 calls to GC(). 

However, it never gets back to the low level it had initially. Initial 
lowest Alloc is 146520. It then grows steadily up to 163040. Then suddenly 
drops to 156896, and grows slowly again to 163520. Then drops suddenly to 
157376. Etc.
So memory is indeed a little bit partially reclaimed from time to time. So 
the GC kind of works, but the GC() function doesn’t behave as I was 
expectiong.

Le vendredi 6 mars 2020 16:55:24 UTC+1, Christophe Meessen a écrit :
>
> The documentation of the GC() function states:
>
> "GC runs a garbage collection and blocks the caller until the garbage 
> collection is complete. It may also block the entire program."
>
> Based on the documentation, I assumed that a garbage collection would be 
> really complete by calling GC. By complete I mean that no garbage is left 
> over.
>
> Apparently It’s not the case. Is it possible learn a bit more on this ? 
> Why would the GC not garbage collect everything when GC() is called ? 
>
> It would have been convenient for detecting memory leaks to be able to 
> compare memory Alloc before and after the checked task and a really 
> complete GC. 
>
>
>
>
> Le vendredi 6 mars 2020 15:26:33 UTC+1, Volker Dobler a écrit :
>>
>> This is normal behaviour and not a leak.
>> Nothing is leaking in your code (and it is generally
>> hard to leak RAM). The allocations will be reclaimed.
>>
>> V.
>>
>> On Friday, 6 March 2020 14:11:37 UTC+1, Christophe Meessen wrote:
>>>
>>> I wanted to check my program for go routine and memory leaks. In doing 
>>> so I detected what resemble a memory leak while my program was doing 
>>> nothing. 
>>>
>>> Here is a minimal program that reproduces the problem. The program 
>>> collects and prints the total number of bytes allocated and the number of 
>>> blocks. 
>>>
>>> package main
>>>
>>> import (
>>> "runtime"
>>>
>>> func main() {
>>> var m runtime.MemStats
>>> ticker := time.NewTicker(20 * time.Second)
>>> for {
>>> runtime.ReadMemStats(&m)
>>> println("status: memUsed:", m.Alloc, "allocs:", m.Mallocs-m.Frees, 
>>> "numGC", m.NumGC)
>>> <-ticker.C
>>> runtime.GC()
>>> }
>>> }
>>>
>>> What I see is a slow but steady increase of memUse and allocs. The 
>>> memUse grows by 4 to 96 bytes every 40 to 60 seconds. 
>>> Is this a feature of the GC or is this a memory leak in one of the 
>>> called functions ? 
>>>
>>> Note that I use println because the "leak" is much more important when I 
>>> use fmt.Println of log.Println. I also use ticker because I was told it 
>>> would be better than time.Sleep, but I don’t see any significant difference 
>>> in the "leak" when I use one or the other. 
>>>
>>>

-- 
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/e53fb283-4b76-48f5-b5ed-319f6cf4fbb8%40googlegroups.com.

Reply via email to