Because GC costs are amortized in order to limit the effect on the performance (pauses) of the program - you should almost never manually invoke a GC.

That being said, there are "memory profilers" that can detect live/dead objects in order to detect memory leaks - use them during development (and most can be used in production with some caveats).

-----Original Message-----
From: Christophe Meessen
Sent: Mar 6, 2020 9:55 AM
To: golang-nuts
Subject: [go-nuts] Re: Memory leak or GC feature ?

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/b8103128-8ae8-42fb-8b3f-4d5265632246%40googlegroups.com.




-- 
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/510361487.825.1583510954972%40wamui-molly.atl.sa.earthlink.net.

Reply via email to