Reader small image

You're reading from  Learn Data Structures and Algorithms with Golang

Product typeBook
Published inMar 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789618501
Edition1st Edition
Languages
Right arrow
Author (1)
Bhagvan Kommadi
Bhagvan Kommadi
author image
Bhagvan Kommadi

Bhagvan Kommadi, Founder, Architect Corner has around 18 years experience in the industry ranging from large-scale enterprise development to incubating software product startups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is currently working as CTO of Crystal Delta Solutions. He is the member of IFX forum and an Individual member of Oracle JCP. He has developed Go language based blockchain solutions in retail, education, banking, and financial services sectors. These blockchain solutions were based on Chain Core (Go language based), Ethereum and Hyperledger blockchain platforms. He has experience in building high transactional applications using Java, C, C++, C#, Python, Go, Ruby and JavaScript frameworks. He has reviewed books such as Beyond Software Architecture-Creating and sustaining winning solutions by Luke Hohmann and Algorithms of the intelligent Web by Dr. Haralambos (Babis) Marmanis.
Read more about Bhagvan Kommadi

Right arrow

Memory Management

Memory management is a way to control and organize memory. Memory divisions are called blocks, and they are used for running different processes. The basic goal of memory management algorithms is to dynamically designate segments of memory to programs on demand. The algorithms free up memory for reuse when the objects in the memory are never required again. Garbage collection, cache management, and space allocation algorithms are good examples of memory management techniques. In software engineering, garbage collection is used to free up memory that's been allocated to those objects that won't be used again, thus helping in memory management. The cache provides in-memory storage for data. You can sort the data in the cache into locale-specific groups. The data can be stored using key and value sets.

This chapter covers the garbage collection, cache...

Technical requirements

Garbage collection

Garbage collection is a type of programmed memory management in which memory, currently occupied by objects that will never be used again, is gathered. John McCarthy was the first person to come up with garbage collection for managing Lisp memory management. This technique specifies which objects need to be de-allocated, and then discharges the memory. The strategies that are utilized for garbage collection are stack allocation and region interference. Sockets, relational database handles, user window objects, and file resources are not overseen by garbage collectors.

Garbage collection algorithms help reduce dangling pointer defects, double-free defects, and memory leaks. These algorithms are computing-intensive and cause decreased or uneven performance. According to Apple, one of the reasons for iOS not having garbage collection is that garbage collection...

Cache management

Cache management consists of managing static, dynamic, and variable information:

  • Static information never changes
  • Dynamic information changes frequently
  • Variable information changes less frequently than dynamic information

The object cache is stored in various data structures, such as maps and trees. Maps have a key as an identifier and a value, which is an object.

Cache objects can be related to memory, disks, pools, and streams. Caches have attributes related to time to live, group, and region. A region consists of a collection of mapped key-values. Regions can be independent of other regions. Cache configuration consists of defaults, regions, and auxiliaries.

A typical cache manager has the following features:

  • Memory management
  • Thread pool controls
  • Grouping of elements
  • Configurable runtime parameters
  • Region data separation and configuration
  • Remote synchronization...

Space allocation

Each function has stack frames associated with individual memory space. Functions have access to the memory inside the frame, and a frame pointer points to the memory's location. Transition between frames occurs when the function is invoked. Data is transferred by value from one frame to another during the transition.

Stack frame creation and memory allocation is demonstrated in the following code. The addOne function takes num and increments it by one. The function prints the value and address of num:

///main package has examples shown
// in Go Data Structures and algorithms book
package main

// importing fmt package
import (
"fmt"
)

// increment method
func addOne(num int) {
num++
fmt.Println("added to num", num, "Address of num", &num)
}

The main method initializes the variable number as 17. The number value and address...

Concepts – Go memory management

In Go, programmers don't need to worry about coding a variable's value placement in memory and space allocation. Garbage collection in Go is overseen by the memory manager. The GOGC variable is used to set a value for the initial garbage collection target percentage. Garbage collection is activated when the proportion of freshly allotted data to the live data that remains after the previous garbage collection reaches the target percentage. The default value of the GOGC variable is 100. This setting can be turned off, which stops garbage collection. The current implementation of garbage collection in Go uses the mark-and-sweep algorithm.

Some of the best practices that you can follow to improve memory management are as follows:

  • Small objects can be combined into larger objects
  • Local variables that have escaped from their declaration...

Summary

This chapter covered the garbage collection, cache management, and memory space allocation algorithms. We looked at reference counting algorithms, including simple, deferred, one-bit, and weighted. The mark-and-sweep and generational collection algorithms were also presented with code examples.

The next chapter will cover the next steps we can take after going through this book.

Questions

  1. Which factors are considered when choosing a garbage collection algorithm?
  2. In which reference counting algorithm are program-variable references ignored?
  3. What is the type of reference counting algorithm in which a single-bit flag is used for counting?
  4. In which reference counting algorithm is a weight assigned to each reference?
  5. Who invented weighted reference counting?
  6. Which garbage collection algorithm was proposed by Dijkstra?
  7. What class handles concurrency when the mark-and-sweep collector is running?
  8. What are the criteria for promoting objects to older generations?
  9. Draw a flow chart for the cache management algorithm.
  10. How do you get indirect memory access outside a method's stack frame?

Further reading

The following books are recommended if you want to know more about garbage collection:

  • Design Patterns, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
  • Introduction to Algorithms – Third Edition, by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
  • Data structures and Algorithms: An Easy Introduction, by Rudolph Russell
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Data Structures and Algorithms with Golang
Published in: Mar 2019Publisher: PacktISBN-13: 9781789618501
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Bhagvan Kommadi

Bhagvan Kommadi, Founder, Architect Corner has around 18 years experience in the industry ranging from large-scale enterprise development to incubating software product startups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is currently working as CTO of Crystal Delta Solutions. He is the member of IFX forum and an Individual member of Oracle JCP. He has developed Go language based blockchain solutions in retail, education, banking, and financial services sectors. These blockchain solutions were based on Chain Core (Go language based), Ethereum and Hyperledger blockchain platforms. He has experience in building high transactional applications using Java, C, C++, C#, Python, Go, Ruby and JavaScript frameworks. He has reviewed books such as Beyond Software Architecture-Creating and sustaining winning solutions by Luke Hohmann and Algorithms of the intelligent Web by Dr. Haralambos (Babis) Marmanis.
Read more about Bhagvan Kommadi