{"id":433,"date":"2026-05-26T09:00:00","date_gmt":"2026-05-26T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/"},"modified":"2026-05-26T09:00:00","modified_gmt":"2026-05-26T03:30:00","slug":"go-memory-garbage-collector","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/","title":{"rendered":"Go Garbage Collector and Memory Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Go garbage collector cleans up memory that your program no longer uses, so you never call free by hand. In this post, you will learn how Go decides where values live and how the collector finds unused memory. Then we will measure it with real tools and cut down allocations in a simple benchmark.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-pointers-explained\/\">Go pointers explained<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-slices-arrays-explained\/\">Go slices and arrays explained<\/a>. We also point to <a href=\"https:\/\/smitabharat.com\/blog\/go-profiling-pprof-guide\/\">Go profiling with pprof<\/a> when you need deeper tools. So if pointers feel new, read that post first.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the Go Garbage Collector?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, the Go garbage collector, or GC, is a part of the runtime. Its job is to free memory you no longer need. When your code creates a value on the heap, the GC keeps track of it. Later, when nothing points to that value, the GC reclaims the space.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a restaurant with a helper who clears tables. Guests leave plates and glasses behind. Then the helper removes them once the guests are gone. Meanwhile, the kitchen keeps cooking. Similarly, the Go garbage collector cleans up while your program keeps running.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go&#8217;s collector is concurrent, which means it runs at the same time as your code. As a result, pauses are very short, usually well under a millisecond for typical programs. For that reason, the Go garbage collector suits web servers and other low-latency services.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why the Go Garbage Collector Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You do not manage memory by hand in Go, but memory still affects speed. So here is why the Go garbage collector is worth understanding.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Fewer bugs<\/strong>: you cannot forget to free memory or free it twice.<\/li>\n\n\n\n<li><strong>Predictable latency<\/strong>: short pauses keep web servers responsive.<\/li>\n\n\n\n<li><strong>Tuning knobs<\/strong>: two settings let you trade memory for speed.<\/li>\n\n\n\n<li><strong>Faster code<\/strong>: fewer allocations mean less work for the GC.<\/li>\n\n\n\n<li><strong>Better debugging<\/strong>: you can explain why a service uses too much memory.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because of this, most Go performance work begins with a simple question: how many allocations does this code make?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Go Garbage Collector Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go values live in one of two places. First, local values that do not outlive their function usually go on the stack. The stack is cheap and cleaned up automatically. Second, values that must live longer go on the heap, and only the heap needs the GC. The compiler decides this during escape analysis.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-1024x640.png\" alt=\"Go Garbage Collector diagram showing the stack, the heap and the mark and sweep step\" class=\"wp-image-418\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Short-lived values sit on the stack and vanish when a function returns. Values that escape live on the heap, where the garbage collector marks the reachable ones and frees the rest.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When the heap grows enough, the collector starts a cycle. First, it marks every value that your program can still reach, starting from global variables and goroutine stacks. Then it sweeps the heap and frees everything that was not marked. Because most of this work runs beside your code, the program barely stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Stack vs Heap<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The two memory areas behave very differently, so the table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Stack<\/th><th>Heap<\/th><\/tr><\/thead><tbody><tr><td>Speed<\/td><td>Very fast<\/td><td>Slower<\/td><\/tr><tr><td>Cleaned up by<\/td><td>Function return<\/td><td>Garbage collector<\/td><\/tr><tr><td>Size<\/td><td>Small per goroutine, grows as needed<\/td><td>Large, shared<\/td><\/tr><tr><td>Used for<\/td><td>Short-lived local values<\/td><td>Values that outlive the function<\/td><\/tr><tr><td>GC cost<\/td><td>None<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So you want most values on the stack. Fortunately, the compiler does that for you whenever it can prove the value does not escape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What Makes a Value Escape<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A value escapes to the heap when its address outlives the function. For example, returning a pointer to a local variable forces the variable onto the heap. Storing a value in an interface can also make it escape. The same is true when you send it on a channel or capture it in a closure. Fortunately, you can check the compiler&#8217;s decision with <code>go build -gcflags=-m<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Garbage Collector: GOGC vs GOMEMLIMIT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go gives you two settings to tune the collector. However, they answer different questions, as the table below shows.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>GOGC<\/th><th>GOMEMLIMIT<\/th><\/tr><\/thead><tbody><tr><td>Question it answers<\/td><td>How much can the heap grow before the next cycle?<\/td><td>What is the most memory the process should use?<\/td><\/tr><tr><td>Default<\/td><td>100 (heap can double)<\/td><td>Off<\/td><\/tr><tr><td>Higher or bigger value<\/td><td>Fewer GC cycles, more memory<\/td><td>More memory allowed before extra GC<\/td><\/tr><tr><td>Typical use<\/td><td>Trade memory for CPU<\/td><td>Stay inside a container limit<\/td><\/tr><tr><td>Set with<\/td><td><code>GOGC=200<\/code> or <code>debug.SetGCPercent<\/code><\/td><td><code>GOMEMLIMIT=1GiB<\/code> or <code>debug.SetMemoryLimit<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-1024x640.png\" alt=\"Go Garbage Collector diagram comparing GOGC with GOMEMLIMIT\" class=\"wp-image-419\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">GOGC decides how far the heap may grow before the next collection, while GOMEMLIMIT sets a soft upper limit for total memory use.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Start with the defaults, because they suit most programs. Later, use <code>GOMEMLIMIT<\/code> in containers so the GC works harder before the system kills your process. On the other hand, raise <code>GOGC<\/code> only when you have spare memory and want less CPU use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Garbage Collector Hands-On: Measure and Reduce Memory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s watch the collector at work. Because we run small programs on Go 1.24, every number below is real.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: See Stack and Heap Decisions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, here are two small functions. One returns a pointer. In contrast, the other keeps its value local.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type User struct {\n\tID   int\n\tName string\n}\n\n\/\/ escapes returns a pointer, so the value must live on the heap.\nfunc escapes() *User {\n\tu := User{ID: 1, Name: \"Asha\"}\n\treturn &amp;u\n}\n\n\/\/ stays keeps the value local, so it can live on the stack.\nfunc stays() int {\n\tu := User{ID: 2, Name: \"Ravi\"}\n\treturn u.ID\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now build with <code>go build -gcflags=-m .<\/code> and look for the key line. As a result, the compiler tells you exactly what moved.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main.go:17:2: moved to heap: u<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Line 17 is the <code>u<\/code> inside <code>escapes<\/code>. In contrast, the <code>u<\/code> inside <code>stays<\/code> never appears in the list. That means it stays on the stack, so the GC never has to track it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Watch the Heap with MemStats<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we allocate about 20 MB, drop the references and force a collection. Then we print the heap size at each step. This shows the Go garbage collector doing its work.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func stats(label string) {\n\tvar m runtime.MemStats\n\truntime.ReadMemStats(&amp;m)\n\tfmt.Printf(\"%-14s heap=%4d KB  gc runs=%d\\n\", label, m.HeapAlloc\/1024, m.NumGC)\n}\n\nfunc main() {\n\tstats(\"start\")\n\n\tdata := make([][]byte, 0)\n\tfor i := 0; i &lt; 200; i++ {\n\t\tdata = append(data, make([]byte, 100*1024)) \/\/ 100 KB each\n\t}\n\tstats(\"after alloc\")\n\n\tdata = nil \/\/ drop the references\n\truntime.GC()\n\tstats(\"after GC\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output below is real.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>start          heap=  71 KB  gc runs=0\nafter alloc    heap=20883 KB  gc runs=3\nafter GC       heap=  72 KB  gc runs=4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The heap grew to about 20 MB. Meanwhile, the collector ran three times on its own. After we set <code>data = nil<\/code> and called <code>runtime.GC()<\/code>, the heap dropped back to 72 KB. This shows that the collector frees memory only when nothing points to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Watch GC Cycles with gctrace<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Meanwhile, the <code>GODEBUG=gctrace=1<\/code> setting prints one line for every cycle. For example, this is the first line from our run.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gc 1 @0.015s 4%: 0.10+1.2+0.077 ms clock, 0.20+0.70\/0.47\/0+0.15 ms cpu, 3-&gt;3-&gt;1 MB, 4 MB goal, 0 MB stacks, 0 MB globals, 2 P<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s read the key parts. First, the <code>0.10+1.2+0.077 ms clock<\/code> part shows two tiny pauses around a concurrent mark. Next, <code>3-&gt;3-&gt;1 MB<\/code> gives three heap sizes. They are the size at the start, the size at the end of marking and the live data that remained. Finally, <code>4 MB goal<\/code> is the heap size that triggers the next cycle.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Reuse Memory with sync.Pool<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If your code creates many short-lived buffers, a <code>sync.Pool<\/code> lets you reuse them. As a result, the GC has less to clean. For example, a busy web server can reuse one buffer per request instead of making thousands of new ones.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var pool = sync.Pool{New: func() any { return make([]byte, 0, 4096) }}\n\nbuf := pool.Get().([]byte)\nbuf = append(buf[:0], \"reuse me\"...)\nfmt.Println(\"pooled buffer:\", string(buf))\npool.Put(buf[:0])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The pool may drop items at any GC, so never store anything in it that you cannot recreate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Preallocate to Cut Allocations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, the simplest trick is to give <code>make<\/code> the size you expect. To prove it, this benchmark builds a slice of 1000 numbers two ways.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func BenchmarkAppendNoHint(b *testing.B) {\n\tb.ReportAllocs()\n\tfor i := 0; i &lt; b.N; i++ {\n\t\tvar s []int\n\t\tfor j := 0; j &lt; 1000; j++ {\n\t\t\ts = append(s, j)\n\t\t}\n\t}\n}\n\nfunc BenchmarkAppendWithHint(b *testing.B) {\n\tb.ReportAllocs()\n\tfor i := 0; i &lt; b.N; i++ {\n\t\ts := make([]int, 0, 1000)\n\t\tfor j := 0; j &lt; 1000; j++ {\n\t\t\ts = append(s, j)\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As a result, we see a huge difference on our machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BenchmarkAppendNoHint-2     \t  150099\t      9946 ns\/op\t   25208 B\/op\t      12 allocs\/op\nBenchmarkAppendWithHint-2   \t 2041651\t       577.4 ns\/op\t       0 B\/op\t       0 allocs\/op<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without a size hint, the slice made 12 allocations as it grew. With a hint, it allocated nothing on the heap and ran about 17 times faster. Your numbers will differ. However, the pattern is the same everywhere.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with the Go Garbage Collector<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>**Calling <code>runtime.GC()<\/code> in production code**: this forces a full cycle and hurts speed. So use it only in tests and demos, and let the Go garbage collector decide when to run.<\/li>\n\n\n\n<li><strong>Ignoring allocations in hot loops<\/strong>: small allocations add up quickly. Therefore, check <code>-benchmem<\/code> for hot paths.<\/li>\n\n\n\n<li><strong>Holding references too long<\/strong>: a global map that only grows keeps everything alive. Instead, delete entries you no longer need.<\/li>\n\n\n\n<li><strong>Slicing a huge array for a tiny result<\/strong>: the small slice keeps the whole array alive. So copy the small part out.<\/li>\n\n\n\n<li><strong>Guessing instead of measuring<\/strong>: tuning without data often makes things worse. Use benchmarks and pprof first.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for the Go Garbage Collector<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Measure before you tune<\/strong>, because most programs run fine with the defaults.<\/li>\n\n\n\n<li><strong>Preallocate slices and maps<\/strong> when you know the size, since it avoids repeated growth.<\/li>\n\n\n\n<li><strong>Pass small structs by value<\/strong>, because they can stay on the stack.<\/li>\n\n\n\n<li>**Set <code>GOMEMLIMIT<\/code> in containers**, so the process stays under its limit.<\/li>\n\n\n\n<li>**Reuse buffers with <code>sync.Pool<\/code>** in hot paths, but keep the code simple.<\/li>\n\n\n\n<li>**Run <code>go build -gcflags=-m<\/code>** on slow code, since it shows what escapes.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise the Go garbage collector, write a function that builds a slice of 100000 strings. Benchmark it with and without a size hint, and then compare the <code>allocs\/op<\/code> numbers. After that, print <code>runtime.MemStats<\/code> before and after.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: use <code>b.ReportAllocs()<\/code> in the benchmark. After that, run the program with <code>GODEBUG=gctrace=1<\/code> and count how many cycles each version triggers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages explain the collector and its settings, and they also cover the tools used in this lesson.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/doc\/gc-guide\">A Guide to the Go Garbage Collector<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/runtime\">runtime package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/diagnostics\">Diagnostics in Go<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Go garbage collector frees heap memory that nothing points to.<\/li>\n\n\n\n<li>Escape analysis decides whether a value lives on the stack or the heap.<\/li>\n\n\n\n<li>The collector marks reachable values and sweeps the rest, mostly beside your code.<\/li>\n\n\n\n<li><code>GOGC<\/code> controls heap growth, and <code>GOMEMLIMIT<\/code> sets a soft memory limit.<\/li>\n\n\n\n<li>Fewer allocations mean less GC work, so preallocate and reuse buffers.<\/li>\n\n\n\n<li>Always measure with benchmarks, <code>MemStats<\/code> and <code>gctrace<\/code> before tuning.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you understand how Go manages memory. However, good code also needs a good shape. So next, we will put everything together and learn how to organise a real project with clean architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up: <a href=\"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/\">Go clean architecture for real projects<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>The Go garbage collector cleans up memory that your program no longer uses, so you never call free by hand. In this post, you&hellip;<\/p>\n","protected":false},"author":1,"featured_media":417,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[80,11,82],"swapz_series":[],"class_list":["post-433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-advanced","tag-go-lang","tag-go-performance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Garbage Collector and Memory Explained<\/title>\n<meta name=\"description\" content=\"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Garbage Collector and Memory Explained\" \/>\n<meta property=\"og:description\" content=\"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-26T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Garbage Collector and Memory Explained\",\"datePublished\":\"2026-05-26T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/\"},\"wordCount\":1566,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-memory-garbage-collector-featured.png\",\"keywords\":[\"Advanced\",\"Go Lang\",\"Go Performance\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/\",\"name\":\"Go Garbage Collector and Memory Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-memory-garbage-collector-featured.png\",\"datePublished\":\"2026-05-26T03:30:00+00:00\",\"description\":\"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-memory-garbage-collector-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-memory-garbage-collector-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Garbage Collector featured image: stack, heap and the GOGC setting\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-memory-garbage-collector\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Garbage Collector and Memory Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Garbage Collector and Memory Explained","description":"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/","og_locale":"en_US","og_type":"article","og_title":"Go Garbage Collector and Memory Explained","og_description":"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.","og_url":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-26T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Garbage Collector and Memory Explained","datePublished":"2026-05-26T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/"},"wordCount":1566,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png","keywords":["Advanced","Go Lang","Go Performance"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/","url":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/","name":"Go Garbage Collector and Memory Explained","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png","datePublished":"2026-05-26T03:30:00+00:00","description":"Learn the Go garbage collector step by step: stack vs heap, escape analysis, GOGC and GOMEMLIMIT, with real memory and benchmark output.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-memory-garbage-collector-featured.png","width":2400,"height":1260,"caption":"Go Garbage Collector featured image: stack, heap and the GOGC setting"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Garbage Collector and Memory Explained"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=433"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/417"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=433"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}