<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sarvjeet Rajvansh on Medium]]></title>
        <description><![CDATA[Stories by Sarvjeet Rajvansh on Medium]]></description>
        <link>https://medium.com/@rajvanshsarvjeet?source=rss-372e505e677a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I082Z7c5apHjAeGZJM1-NQ.jpeg</url>
            <title>Stories by Sarvjeet Rajvansh on Medium</title>
            <link>https://medium.com/@rajvanshsarvjeet?source=rss-372e505e677a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 13:24:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajvanshsarvjeet/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Go pprof: The Hidden Superpower for Go Performance]]></title>
            <link>https://medium.com/@rajvanshsarvjeet/go-pprof-the-hidden-superpower-for-go-performance-4405e55badc6?source=rss-372e505e677a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4405e55badc6</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Sarvjeet Rajvansh]]></dc:creator>
            <pubDate>Sun, 02 Nov 2025 19:00:37 GMT</pubDate>
            <atom:updated>2025-11-02T19:00:37.396Z</atom:updated>
            <content:encoded><![CDATA[<p>Performance tuning in Go isn’t about guessing — it’s about <em>measuring</em>. And the tool that makes that possible is <strong>pprof</strong>, Go’s built-in performance profiler.</p><h3>What is pprof?</h3><p>pprof (short for <em>performance profiler</em>) is part of Go’s standard tooling. It allows you to collect and analyze data about how your program uses CPU, memory, and other resources.</p><p>With pprof, you can:</p><ul><li>Find where your code <strong>spends CPU time</strong>.</li><li>See which functions <strong>allocate the most memory</strong>.</li><li>Inspect <strong>goroutines</strong>, <strong>blocking</strong>, and <strong>mutex contention</strong>.</li><li>Generate <strong>flame graphs</strong>, <strong>call graphs</strong>, and <strong>top function lists</strong>.</li></ul><p>Think of it as an X-ray for your Go code.</p><h3>Installing and Running pprof</h3><p>You don’t need to install anything extra — pprof is built into the Go toolchain.</p><p>You can collect profiles in two main ways:</p><h3>1. CPU Profiling (Standalone Program)</h3><pre>package main</pre><pre>import (<br>    &quot;os&quot;<br>    &quot;runtime/pprof&quot;<br>)</pre><pre>func heavyComputation() {<br>    for i := 0; i &lt; 1e8; i++ {} // simulate load<br>}</pre><pre>func main() {<br>    f, _ := os.Create(&quot;cpu.prof&quot;)<br>    pprof.StartCPUProfile(f)<br>    defer pprof.StopCPUProfile()</pre><pre>    heavyComputation()<br>}</pre><p>Run it:</p><pre>go run main.go<br>go tool pprof cpu.prof</pre><p>Inside the interactive prompt:</p><pre>(pprof) top<br>(pprof) list heavyComputation<br>(pprof) web</pre><h3>2. Heap (Memory) Profiling</h3><pre>package main</pre><pre>import (<br>    &quot;fmt&quot;<br>    &quot;os&quot;<br>    &quot;runtime/pprof&quot;<br>)</pre><pre>func allocate() []int {<br>    var nums []int<br>    for i := 0; i &lt; 1_000_000; i++ {<br>        nums = append(nums, i)<br>    }<br>    return nums<br>}</pre><pre>func main() {<br>    allocate()</pre><pre>    f, _ := os.Create(&quot;mem.prof&quot;)<br>    pprof.WriteHeapProfile(f)<br>    f.Close()<br>    fmt.Println(&quot;Heap profile written!&quot;)<br>}</pre><p>Run:</p><pre>go run main.go<br>go tool pprof -alloc_space mem.prof</pre><h3>Using pprof with Web Servers</h3><p>If you’re running a Go HTTP server, profiling is even easier:</p><pre>import _ &quot;net/http/pprof&quot;<br>import &quot;net/http&quot;</pre><pre>func main() {<br>    go func() {<br>        http.ListenAndServe(&quot;:6060&quot;, nil)<br>    }()</pre><pre>    // Your app logic here<br>    select {}<br>}</pre><p>Now open:</p><pre>http://localhost:6060/debug/pprof/</pre><p>You’ll see endpoints like:</p><ul><li>/heap — memory allocations</li><li>/profile — 30-second CPU sample</li><li>/goroutine — stack traces of all goroutines</li></ul><p>You can download and analyze profiles like this:</p><pre>go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30</pre><h3>Reading pprof Output</h3><p>When you open a profile with go tool pprof, you’ll see something like:</p><pre>Showing nodes accounting for 4.50s, 90% of 5.00s total<br>Showing top 10 nodes out of 50<br>      flat  flat%   sum%        cum   cum%  Function<br>     3.00s 60.0% 60.0%      3.50s 70.0%  main.heavyComputation<br>     1.00s 20.0% 80.0%      1.00s 20.0%  runtime.gcBgMarkWorker</pre><ul><li><strong>flat%</strong>: time spent <em>inside</em> the function itself.</li><li><strong>cum%</strong>: time spent in the function <em>and</em> its callees.</li><li><strong>flat</strong>: absolute time spent in that function.</li></ul><p>Use:</p><ul><li>top → see which functions cost the most.</li><li>list &lt;func&gt; → see line-by-line hotspots.</li><li>web → open an interactive call graph.</li><li>png, pdf → export visuals.</li></ul><h3>Example: Slice Allocation Profiling</h3><p>Without preallocation:</p><pre>func build() []int {<br>    var nums []int<br>    for i := 0; i &lt; 1_000_000; i++ {<br>        nums = append(nums, i)<br>    }<br>    return nums<br>}</pre><p>With preallocation:</p><pre>func build() []int {<br>    nums := make([]int, 0, 1_000_000)<br>    for i := 0; i &lt; 1_000_000; i++ {<br>        nums = append(nums, i)<br>    }<br>    return nums<br>}</pre><p>Compare with:</p><pre>go test -bench=. -benchmem</pre><p>You’ll notice massive drops in allocations and GC load.</p><h3>Profiling Metrics You’ll See</h3><p>Metric Description <strong>alloc_space</strong> Total bytes allocated (even if freed). <strong>inuse_space</strong> Bytes still in use (live objects). <strong>alloc_objects</strong> Total number of allocations. <strong>inuse_objects</strong> Number of live objects.</p><p>Use the right one for your use case — for long-running services, <em>inuse_space</em> is more meaningful.</p><h3>Pro Tips</h3><p>Always <strong>measure before optimizing</strong> — don’t guess.</p><ol><li>Use go test -benchmem for micro-benchmarking.</li><li>Focus on <em>hot paths</em> (high flat%) not total time.</li><li>Combine CPU + memory profiles for holistic insight.</li><li>Use visualization tools like <a href="https://pprof.me/">pprof.me</a> or pprof -http=:8080 for interactive analysis.</li></ol><h3>TL;DR</h3><ul><li>pprof is Go’s built-in profiler for CPU, memory, and concurrency.</li><li>Collect data with runtime/pprof or net/http/pprof.</li><li>Use go tool pprof to analyze performance bottlenecks.</li><li>Always measure, fix, and re-measure — Go makes that painless.</li></ul><blockquote><em>Go performance isn’t about magic — it’s about visibility. And </em><em>pprof gives you eyes inside your runtime.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4405e55badc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>