heap ballast

133 views
Skip to first unread message

Mike Mitchell

unread,
Feb 12, 2024, 3:34:02 PMFeb 12
to golang-nuts
Purely for the sake of experiment (this is not a production environment thing), I have tried to test out the Heap Ballast technique of reducing the number of garbage collections with a small heap size, which I read about in a blog post by Twitch, and I think I must have misinterpreted the use case of such a technique. Can anyone help clarify for me? Thank you in advance

My program consists of a webserver in the main func

 http.ListenAndServe(":6000", http.DefaultServeMux)

and I create a small ballast on my Digital Ocean 1 GB ram droplet (the twitch blog post said they made a ballast of 10 <<30) but I did 10 << 20

 ballast := make([]byte, 10<<20)

I then sent a million requests to it 

ab -k -c 8 -n 1000000 "http://127.0.0.1:6000/"

I also did the same without the heap ballast. 

The results (see attached images) surprised me because there was basically the same number of garbage collections with and without the ballast. The only difference the ballast seems to have made was that it increased HeapIdle enormously.  How do people use Heap Ballast to reduce the number of GCs?

 go version go1.21.1 linux/amd64

I have read many people referring to this Heap Ballast technique as a way to reduce the number of garbage collections with a small heap size. In fact, this is what it says in the Twitch article

  1. At steady state, our application was triggering ~8–10 garbage collection (GC) cycles a second (400–600 per minute).

  2. 30% of CPU cycles were being spent in function calls related to GC

  3. During traffic spikes the number of GC cycles would increase

  4. Our heap size on average was fairly small (<450Mib)

  1. At steady state, our application was triggering ~8–10 garbage collection (GC) cycles a second (400–600 per minute).

  2. 30% of CPU cycles were being spent in function calls related to GC

  3. During traffic spikes the number of GC cycles would increase

  4. Our heap size on average was fairly small (<450Mib)





without_ballast.png
with_ballast.png

Sean Liao

unread,
Feb 12, 2024, 3:57:39 PMFeb 12
to golang-nuts
The modern way to handle this is with GOMEMLIMIT
https://tip.golang.org/doc/gc-guide

- sean
> --
> 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/7cee5b4e-24cc-4579-b5c9-62b93298c931n%40googlegroups.com.

Mike Mitchell

unread,
Feb 12, 2024, 4:23:35 PMFeb 12
to golang-nuts
Thank you for your response Sean. I actually know that this situation can be handled with GOMEMLIMIT. The attached svg is a graph with GOMEMLIMIT set to 150MiB and it shows there were only 17 garbage collections (much fewer than than 629 that I had with the heap ballast).

However, the point of my question was to ask about the Heap Ballast technique.  The key word of my original post was the opening "Purely for the sake of experiment..." I was hoping someone could explain why the Heap Ballast technique did not reduce the number of garbage collections (as shown in the with_ballast.png).   

 If you or someone else knows, I'd be grateful to hear.  

--
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/YmpDtEkZcAc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
with_ballast.png
without_ballast.png
2024-gogc-5000-mem-150mib.svg
Reply all
Reply to author
Forward
0 new messages