GoLang Memory Management

Why Golang?

One, created at Google and growing rapidly now having around 90,000+ repositories. Go can be used for cloud and systems programming and extended to the game server development and handling text-processing problems.

Two, many big companies use Go for their projects, like Google, Dropbox, Soundcloud, Docker, Cloudflare, BBC etc.

Three, Go is fast to learn, develop, compile, deploy, run

Four, Golang is created with cloud computing in mind, which will arguably define the future of the tech world. It has built-in testing facilitates for testing your code.

Five, Go supports cross-compilation with lots of tutorials and instructions on how to do it best.

Last, Garbage collector

Overview of Memory Management in Go

Go supports automatic memory management, such as automatic memory allocation and automatic garbage collection, which avoids many lurking bugs.

Memory block is a continuous memory segment and memory blocks may have different sizes. In Go, memory block may host multiple value such as struct, array and Slice etc.

When Memory block is allocated in Golang?

Typically, a functional parameters and local variables are allocated on the stack. In Go dynamic memory block is allocated mainly using new and make. New allocates exact one memory block that is used to create struct type value, whereas, make creates more than one memory block and returns the reference, like a slice, map or channel value. The literal created with the make, supports built-in append function to extend memory if required.

Where Memory block is allocated?

A memory block created by calling new function may be allocated on heap or stacks whereas memory block created by make is allocated on heap.

Go uses OS function mmap similar to TCMalloc (Thread-Caching Malloc) to allocate memory in heap.

Go uses goroutine, which maintain a small stack (memory segment) act as memory pool for some memory blocks. The maximum stack size is 1 GB on 64-bit systems, and 250 MB on 32-bit systems, can be set by SetMaxStack function.

Go supports stacks just to make Go programs run more efficiently. Go compiler allocate all memory block on the heap can be used by multiple goroutines, so here garbage collector comes into the picture.

Flags like -gcflags -m use to analyze memory block. Also, we can view each package-level variable allocated on the heap, and the variable is referenced by an implicit pointer that is allocated on a global memory zone.

When Memory block is collected in Go?

  1. Package-level allocated memory variables will never be collected.
  2. Goroutine stack collected automatic (not by garbage collector) while exiting.
  3. The garbage collector (GC) collects only allocated memory in a heap only if is not referenced i.e. unused memory blocks.

N.B. To debug Go uses Stack() to stack trace of goroutines.

How Compiler detects unused memory block?

In Go, garbage collection (GC) works in two phases, the mark phase, and the sweep phase. GC uses the tri-color algorithm to analyze the use of memory blocks, visits all grey global and stack objects and mark them as black and this process repeats till the last grey object and in sweep phase memory blocks will be collected.

garbage collector

GC maintains a table for every allocated memory with a reference count. Once the count comes to ZERO the object in heap marked as grey. The objects of the black set are guaranteed to have no pointers to any object of the white set. However, an object of the white set can have a pointer to an object of the black set because this has no effect on the operation of the garbage collector. The objects of the grey set might have pointers to some objects of the white set. Finally, the objects of the white set are the candidates for garbage collection.

Garbage collector starts when a threshold is satisfied defined in environment variable GOGC. Using SetGCPercent (package runtime/debug) function GOGC can be set, default GOGC=100 and negative percentage disables garbage collection.

GCStats collect information about recent garbage collections through GetGCStats function.

When variable Escapes to the heap?

Here “escapes to the heap,” means it will be dynamically allocated on the heap at runtime, because it is passed to a function argument that escapes itself.

x1 is not escaped to the heap, as it is not passed to any function where x2 escapes to the heap.

Some cause variables escape to the heap:

  1. Pointers or values as an argument to the function.
  2. Sending pointers or values containing pointers to channels: AS compiler cannot determine when goroutine will receive the data on a channel and when can be free.
  3. Pointers in a slice: slice may be on the stack but the referenced data escapes to the heap.
  4. Arrays of slices: It may get reallocated because an append can exceed its capacity.
  5. Methods on an interface type: As the interface gets type and value at runtime.

Debugging In GO:

We can use below flags for debugging Go memory:

  1. go tool compile -S testigGC.go
  2. go build -gcflags ‘-m -m’ testigGC.go

Reference:

TCMaloc: http://goog-perftools.sourceforge.net/doc/tcmalloc.html

https://cmc.gitbook.io/go-internals/chapter-i-go-assembly

https://blog.cloudflare.com/how-stacks-are-handled-in-go/

Have more questions on GoLang Memory Management? 

 
Share:

Related Posts

A Deep Dive into 5G Service-Based Architecture (SBA)

5G technology roll out signifies an immense revenue opportunity for telecom industry.

Share:
Technical Documentation

Technical Documentation Review and Tips

Technical reviews are vital for effective and quality documentation. To make this happen, have documentation and its reviews listed as one of the deliverables – just like development or testing. This will place priority on the process, and ensure everyone involved understands the importance of proper and thorough reviews.

Share:
Technology Trends 2024

Technology Trends 2024- The CXO perspective

In the rapidly evolving landscape of 2024, technology trends are reshaping industries and redefining business strategies. From the C-suite perspective, executives are navigating a dynamic environment where artificial intelligence, augmented reality, and blockchain are not just buzzwords but integral components of transformative business models. The Chief Experience Officers (CXOs) are at the forefront, leveraging cutting-edge technologies to enhance customer experiences, streamline operations, and drive innovation. This blog delves into the strategic insights and perspectives of CXOs as they navigate the ever-changing tech terrain, exploring how these leaders are shaping the future of their organizations in the era of 2024’s technological evolution.

Share:
Technology Trends 2024

The Winds of Technology Blowing into 2024

As 2023 draws to a close, the digital landscape is poised for a seismic shift in 2024. Generative Artificial Intelligence (Gen AI) continues its integrative streak, disrupting industries from B2B to healthcare. Networking trends emphasize simplicity, while the synergy of cloud and edge computing with Gen AI promises real-time workflows. Quantum computing, cybersecurity, intelligent automation, and sustainable technology are key players, reshaping the technological fabric. Join us as we navigate the transformative currents of 2024, unraveling the impact on enterprises in our forthcoming article. Stay tuned for the tech evolution ahead!

Share:
Generative AI Shaping Future Industries

[Infoblog] Generative AI Shaping Future Industries

Generative AI is at the forefront of innovation, harnessing the power of machine learning algorithms to create new and original content, from images and music to entire virtual environments. This infographic depicts how Gen AI is evolving industries and shaping its future.

Share:

Enhancing vCenter Capabilities with VMware vCenter Plugins: A Deep Dive

 vCenter Server is one of the most powerful tools in VMware’s product portfolio, enabling efficient management of virtualized environments. One of the most used features in vCenter is the vCenter plugin, which extends the capabilities by providing custom features such as 3rd Party system discovery, and provisioning, providing a unified view, allowing administrators to manage vSphere, and 3rd Party systems seamlessly.

Share: