codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

25 / 72 topics
22Error Handling23Testing in Go24Benchmarking25Profiling26Code Organization and Packages
Tutorials/Go (Golang)/Profiling
🐹Go (Golang)

Profiling

Updated 2026-04-20
5 min read

Introduction

Profiling is a critical aspect of software development, enabling developers to identify performance bottlenecks and optimize their code for better efficiency. In the Go programming language, profiling tools are built into the standard library, making it easy to gather insights into CPU usage, memory allocation, and other metrics.

This tutorial will guide you through the process of profiling a Go application using various tools available in the standard library. We'll cover CPU profiling, memory profiling, and block profiling, providing real-world examples and best practices for each.

Prerequisites

Before diving into profiling, ensure that you have the following:

  • A working knowledge of Go.
  • A Go development environment set up on your machine.
  • Basic understanding of command-line tools.

Profiling Basics

Go provides several built-in profilers that can be used to analyze different aspects of your application's performance. The net/http/pprof package is particularly useful for web applications, as it allows you to start a profiling server that can be accessed via HTTP.

Enabling Profiling in Your Application

To enable profiling in your Go application, import the net/http/pprof package and register its handlers with an HTTP server. Here's how you can do it:

package main

import (
    "log"
    "net/http"
    _ "net/http/pprof" // Importing this package registers all default pprof handlers
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, world!"))
}

func main() {
    http.HandleFunc("/", helloHandler)
    log.Println("Starting server at port 8080")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}

In this example, the net/http/pprof package is imported and all default profiling handlers are registered. You can then access various profiling endpoints at http://localhost:8080/debug/pprof/.

Accessing Profiling Endpoints

Once your application is running, you can access the following profiling endpoints:

  • CPU Profile: http://localhost:8080/debug/pprof/profile
  • Heap Profile: http://localhost:8080/debug/pprof/heap
  • Block Profile: http://localhost:8080/debug/pprof/block

These endpoints can be accessed using tools like curl or directly in a web browser.

CPU Profiling

CPU profiling helps you identify which functions are consuming the most CPU time. This is crucial for optimizing performance and reducing latency.

Generating a CPU Profile

To generate a CPU profile, you can use the following command:

go tool pprof http://localhost:8080/debug/pprof/profile

This command will start a profiling session that runs for 30 seconds by default. You can specify a different duration using the -seconds flag.

Analyzing the CPU Profile

Once the profile is generated, you'll be dropped into an interactive pprof shell where you can analyze the data. Here are some common commands:

  • top: Displays the top functions consuming CPU time.
  • list [function]: Shows detailed information about a specific function.
  • web: Opens a web interface for visualizing the profile.

For example, to see the top 10 functions consuming CPU time, you can run:

(pprof) top

This will display a list of functions sorted by their CPU usage.

Memory Profiling

Memory profiling helps you identify memory leaks and optimize memory usage. It provides insights into how much memory is being allocated and where it's coming from.

Generating a Heap Profile

To generate a heap profile, use the following command:

go tool pprof http://localhost:8080/debug/pprof/heap

This will start a profiling session that captures the current state of the heap.

Analyzing the Heap Profile

Once you have the heap profile, you can analyze it using similar commands as in CPU profiling:

  • top: Displays the top functions consuming memory.
  • list [function]: Shows detailed information about a specific function.
  • web: Opens a web interface for visualizing the profile.

For example, to see the top 10 functions allocating memory, you can run:

(pprof) top

This will display a list of functions sorted by their memory allocation.

Block Profiling

Block profiling helps you identify where your application is spending time waiting for locks or other synchronization primitives. This is useful for identifying potential concurrency issues.

Enabling Block Profiling

To enable block profiling, you need to set the GODEBUG environment variable:

export GODEBUG=blockprofile=1000000

This sets the block profile rate to 1,000,000 samples per second. You can adjust this value based on your needs.

Generating a Block Profile

To generate a block profile, use the following command:

go tool pprof http://localhost:8080/debug/pprof/block

This will start a profiling session that captures blocking events.

Analyzing the Block Profile

Once you have the block profile, you can analyze it using similar commands as in CPU and memory profiling:

  • top: Displays the top functions where blocking occurs.
  • list [function]: Shows detailed information about a specific function.
  • web: Opens a web interface for visualizing the profile.

For example, to see the top 10 functions with the most blocking events, you can run:

(pprof) top

This will display a list of functions sorted by their blocking time.

Best Practices

Here are some best practices for profiling your Go applications:

  • Profile in Production: While it's important to profile in development, consider running profiling tools in a production-like environment to get accurate data.
  • Use Sampling Profiling: For CPU and memory profiling, use sampling instead of tracing. This provides a good balance between performance overhead and accuracy.
  • Analyze Regularly: Make profiling an ongoing part of your development process. Analyze profiles regularly to catch performance issues early.
  • Optimize Iteratively: Use profiling data to identify bottlenecks and optimize them iteratively. After making changes, re-run the profiler to ensure improvements.

Conclusion

Profiling is a powerful tool for understanding and optimizing the performance of your Go applications. By leveraging the built-in profiling tools in the standard library, you can gain valuable insights into CPU usage, memory allocation, and blocking events. Regularly analyzing these profiles will help you identify and address performance issues, leading to more efficient and scalable applications.

Remember, profiling should be an integral part of your development workflow. Use it to guide optimization efforts and ensure that your application performs well under real-world conditions.


PreviousBenchmarkingNext Code Organization and Packages

Recommended Gear

BenchmarkingCode Organization and Packages