alloc vs totalalloc in memory

2,032 views
Skip to first unread message

Alexander Mills

unread,
Sep 10, 2020, 7:39:10 PM9/10/20
to golang-nuts
I have this helper func to print memory usage (detect a memory leak?)


func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}

I am seeing output like this:
Alloc = 2481 MiB TotalAlloc = 24038 MiB Sys = 2752 MiB NumGC = 114

what is the difference between Alloc and TotalAlloc .. ?
thx

Alexander Mills

unread,
Sep 10, 2020, 7:41:11 PM9/10/20
to golang-nuts
I stole the memory code from here:
https://golangcode.com/print-the-current-memory-usage/
> --
> 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/zLxLN03h6LQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20f0b65f-a420-4ab1-9a44-57e7414c7bd5n%40googlegroups.com.



--
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

Ian Lance Taylor

unread,
Sep 10, 2020, 9:40:34 PM9/10/20
to Alexander Mills, golang-nuts
On Thu, Sep 10, 2020 at 4:39 PM Alexander Mills
<alexande...@gmail.com> wrote:
>
The link above, after "For info on each", explains the difference. If
the explanation is unclear, tell us more.

Ian

Alex Mills

unread,
Sep 11, 2020, 8:01:36 PM9/11/20
to golang-nuts
The explanations I find online arent very clear to me :(

Ian Lance Taylor

unread,
Sep 11, 2020, 8:06:35 PM9/11/20
to Alex Mills, golang-nuts
On Fri, Sep 11, 2020 at 5:01 PM Alex Mills <al...@channelmeter.com> wrote:
>
> The explanations I find online arent very clear to me :(

Well, OK, but I don't know what I can say other than to repeat the
information from that link.

Alloc is bytes of allocated heap objects.

TotalAlloc is cumulative bytes allocated for heap objects.

If that is not clear, what is not clear about it?

Ian
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6c889d9b-e2f1-4583-b310-8954ce456852n%40googlegroups.com.

Alexander Mills

unread,
Sep 11, 2020, 8:14:16 PM9/11/20
to Ian Lance Taylor, Alex Mills, golang-nuts
Well why does TotalAlloc keep climbing up (increasing) in my program, but HeapAlloc remains the same?

Screenshot from 2020-09-11 17-12-32.png


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/zLxLN03h6LQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXQ4eedgwwnxT0Hg_3%2BWmjexX1NriT26U1AksEVmrW8sg%40mail.gmail.com.

Robert Engels

unread,
Sep 11, 2020, 9:01:51 PM9/11/20
to Alexander Mills, Ian Lance Taylor, Alex Mills, golang-nuts
Because memory is reclaimed - the alloc is the “live” heap. TotalAlloc is a cumulative counter of byte allocated - bytes freed do not affect the counter. 

On Sep 11, 2020, at 7:14 PM, Alexander Mills <alexande...@gmail.com> wrote:


Well why does TotalAlloc keep climbing up (increasing) in my program, but HeapAlloc remains the same?

<Screenshot from 2020-09-11 17-12-32.png>

Wojciech S. Czarnecki

unread,
Sep 12, 2020, 8:20:58 AM9/12/20
to golan...@googlegroups.com
Dnia 2020-09-11, o godz. 17:13:22
Alexander Mills <alexande...@gmail.com> napisał(a):

> Well why does TotalAlloc keep climbing up (increasing) in my program,

| CUMULATIVE: increasing or increased in quantity, degree, or force by successive additions. |
It means that every allocation made _adds_ to an ever increasing "cumulative counter".

Think a cash register - every single item's price is added to "TOTAL" (sum, cumulative) variable.

> but HeapAlloc remains the same?

> [image: Screenshot from 2020-09-11 17-12-32.png]

Please do NOT post screenshots here.

--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE
Reply all
Reply to author
Forward
0 new messages