In this post, we’ll discuss the deallocating variable’s memory and how they are referenced. Variables, they are all referring to some data somewhere in memory.
The variables after a long time have to be deallocated in memory. So allocated and deallocated. that means if you declare a variable once and your code is running your space needs to allocate somewhere in memory for that variable.
If it’s an integer value, there has to be some allocated space dedicated to holding that integer and at some point, that space has to be deallocated. When you’re done using it. So, when you’re done using your variable value, you want to be able to say “Oh that space is now free and it can be used for other purposes.” So, that’s memory deallocation, when you make memory space available for other purposes.
Apart from that, you eventually will run out of memory and your machine. So for an example, you look at following the piece of code.
var val = 4
It declares a variable Val, var Val = 4, so the process when it runs it has to allocate a memory location for Val to hold it. if you call this function f 100 times, then it’s going to allocate 100 different spaces for this variable Val right. because the Val goes away after the function completes the Val goes away it’s going to allocate it again it should go away, you want it to go away. Actually every time if you don’t deallocate it, you’ll execute, every time you execute this function. if you will get a new variable Val allocated and so you’ll have all these variable spaces allocated in memory and really you don’t need them anymore.
It means once a particular function call ends you no longer need the space for the Val that it was using. So, if you have to deallocate this memory. You have to say “Look this variable memory is now free, ” because otherwise you would eventually use up all your variable space and you might think well. how we’re going to use it my space we’ve got Val number of the gig in my memory system.
When you have to memory deallocation this space in a timely fashion. Now in order to discuss how space is de-allocated, we got to discuss a little bit about where space store in memory.
The memory is a big thing but there are two big chunks of memory that are relevant to us right now, the stack and the heap.
------ Heap -------
var val = 10
------ Stack -------
var val = 10
Now, the stack is an area of memory that dedicates to function calls, mainly dedicated to function calls. So, one of the things stored in the stack and the local variables for a function. So many time you call a function there can be variables that you define in that function and generally, they go into the stack.
They allocate variable in the stack area of the memory and they’re deallocated. If they are allocated in the stack, they are deallocated automatically when the function completes.
Now, this is different a little bit different for Golang. but how it works in regular languages. Go change this a little bit, okay. But the stack is the area of these local variables where when the function is done, the variables are deallocated.
The heap is a persistent region of memory where when you allocate something on the heap it doesn’t go away. because, the function that allocated it is complete, that heap memory explicitly deallocates it somehow in another language.
So, if you are using C, then you would have to explicitly memory deallocation this. Now Go does a tweak on this. Actually, it is main to understand those memory variables in the stack. which is the part that automatically go away when the variable. it will deallocate automatically if the function is done or in the heap where it’s persistent.
Now if you’re in another language like C, then you have to manually memory deallocation things on the heap. The stuff that’s on the stack you don’t have to manually deallocate it, it will go away when the function completes. But kinds of stuff on the heap you have to manually explicitly deallocate it.
If you were working in C, and if you want to allocate memory on the heap, you would call a function called malloc.
val = malloc(32);
It will allocate 32 bytes of memory and Val variable will be appointed to that and then later, that’s how you allocate it. Later when we want to free Val and it will free that space deallocating it. So, error-prone but fast. So, actually, mean is, it’s error-prone because it’s easy to make a mistake in your allocation and memory deallocation.
So, it’s error-prone in that sense but it is fast. The implementations are very fast. What happens with memory deallocation in an interpreted language is that the interpreter does it and that can take time. So, if you working on compiled language like C, you would have to do that manually.