From the course: Learning Go

How memory is allocated and managed - Go Tutorial

From the course: Learning Go

How memory is allocated and managed

- [Instructor] When you run a Go application with the go run command, you're depending on the Go runtime that's installed on your computer. And when you compile and build a binary Go application, the runtime is included. Either way, your application depends on the runtime which operates in the background in dedicated threads. Like other managed languages, such as Java and C#, you don't have to explicitly allocate or de-allocate memory in your code; it's all managed for you in the background. When you use complex types, such as maps, that is collections of key value pairs, you have to initialize them correctly. There are two built-in functions to be aware of called make() and new(), and you'll use them to initialize these complex objects. There's a difference between them though. The new() function allocates, but doesn't initialize memory. When you allocate an object using the new() function, you'll get back a memory address indicating the location of the map, but the map object itself has zeroed memory storage. And if you try to add a key value pair to the map, it'll cause an error. In contrast, the make() function both allocates and initializes memory. You'll get back that memory address just like you do with new(), but the storage is non-zero and is ready to accept values. Let's take a look at this bit of code. The first line declares a map object named M and says that the keys are strings and the associated values are integers. And I'm declaring that with the new() function. On the next line, I try to add a key value pair, known as an entry, to the map. I create a new value with the key named "theAnswer" and a value of 42. And then I try to output the contents of the map, but I'm not going to get to that third line of code. Instead, the app will crash and I'll see a panic error. It's telling me that I'm dealing with a map with zero memory storage. There's just no place to put the data. And so there's a panic and the application crashes. To fix this, wrap your declaration in the make() function which does initialize memory. In this version of the code, I'm once again saying that it's a map object that contains strings as keys and int as values, but this time, I initialize with make(), and now when I try to add an entry, it'll succeed. And when I output it, I get this output. So whenever you use these complex objects, it's critical that you wrap the initialization in the make() function if your intent is to immediately add data to the object. Memory is de-allocated automatically by the garbage collector that's built into the Go runtime. The garbage collector runs in the background and each time it kicks in, it looks for objects that are out of scope or set to nil so it can clean out the memory and return that memory to your memory pool. The garbage collector was completely rebuilt in Go 1.5 for very low latency to reduce the pauses that happen when you're running Go applications. And now, the process for de-allocating memory goes by so quickly that you're very unlikely to notice it, even on slower computers. Here are some links to some more information about garbage collection. You can find a lot of details in the developer documentation under the runtime package and, after the release of version 1.5, the talk referenced in the second URL has lots of technical details about low-latency garbage collection in Go.

Contents