In this post we show how you can print the state of the current memory usage in Go. To do this, we’ve created a function PrintMemUsage() to help out, so you can call this when ever you need to know.
All the info we need can be acquired through the runtime package, which allows us to read the state of the memory into the MemStats struct. It returns stats like how much memory the program is using, how much of it the OS has allocated to it and the number of garbage collections.
packagemainimport("runtime""fmt""time")funcmain(){vardata[][]intfori:=0;i<4;i++{// Example data:
// Allocate memory using make() and append to data var (so it doesn't get
// garbage collected). This is to create an ever increasing memory usage
// which we can track. We're just using []int as an example.
a:=make([]int,0,999999)data=append(data,a)// Print our memory usage at each interval
PrintMemUsage()time.Sleep(time.Second)}// Clear our memory and print usage, unless the GC has run 'Alloc' will remain the same
data=nilPrintMemUsage()// Force GC to clear up, should see a memory drop
runtime.GC()PrintMemUsage()}// PrintMemUsage outputs the current, total and OS memory being used. As well as the
// number of garage collection cycles completed. For info on each,
// see: https://golang.org/pkg/runtime/#MemStats
funcPrintMemUsage(){varmruntime.MemStatsruntime.ReadMemStats(&m)fmt.Printf("Alloc = %v MiB",m.Alloc/1024/1024)fmt.Printf("\tTotalAlloc = %v MiB",m.TotalAlloc/1024/1024)fmt.Printf("\tSys = %v MiB",m.Sys/1024/1024)fmt.Printf("\tNumGC = %v\n",m.NumGC)}
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.
Convert an io.ReadCloser to String
–
Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings.
First we’ll look at the problem, then we have two different solutions.
Convert HEIC to JPG in Go
–
The “High Efficiency Image File Format” or HEIF is an image format often used by Apple devices. Although called HEIF, the file types are often heic (presumably the ‘c’ stands for container?) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files.
In our example, we’re trying to convert these heic files back into jpg files though - so we can use them elsewhere, display them or what ever we choose.
Sleeping in Golang
–
Sleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided.
In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.