In this article we’ll understand garbage collection and deallocating memory, So it can be hard to determine Because it is appropriate to deallocate a variable. when you can only deallocate a variable when you know the variable is no longer in use.
If you don’t want to deallocate a variable and then later need that variable that you deallocated because then it’s basically gone. So, it’s hard sometimes to figure out when it’s not in use or when it is in use. So, here’s an example, a Go example.
func callme() *int
var y *int
y = callme()
If you look at the first function callme(). Inside there we declare this variable x. So, It’s a local variable to this function callme() and what’s returned though is the address of x, f say x, write a pointer to x.
Now, if you see the main function down below, the main function calls callme(). Then, the main function, what happens is since it calls callme() and it gets the return value of callme() actually gets assigned to a variable in the main function. So, this program callme() normally, if you declare a local variable x when the function ends that variable x should be deallocated.
You’re done with it because the function is done with it. But in this example, it’s not the case because is returning a pointer to x. So, now the main, since the main now has that pointed to x since that’s getting returned to the main, the main might still use that pointer to x.
Let’s if you say “callme is now done, I can get rid of its local variable x because maybe main is going to use that local variable because now main has a pointer to it”. Okay. So, this is actually a legal thing to do in the Go language.
So, this is just one example of how pointers especially make it difficult to tell when deallocation is legal and when it’s not. So, deallocation is a complicated thing.
A garbage collection is simply an automatic tool that deals with deallocation. So, this is part of interpreted languages and this is done by the interpreter, so if is, say Java, the Java Virtual Machine or a Python is a Python interpreter something like that. It keeps track of these pointers and it determines when a variable is not in use anymore and when it is.
If that variable decides once is definitely not used, there no more pointers, no more references to that variable then the garbage collector deallocates it. Only when all references are gone.
So, garbage collection is a nice thing. It’s easy for the programmer. Right. The programmer doesn’t have to worry about exactly when deallocation when to do it, when not to do it. So, deallocating memory is a big complex, in other lang say C or something like that. because, they require an interpreter so generally compiled languages like C, C++, they can’t do it, but Go is different.
Go language is different and better in the sense. It is a compiled language which has garbage collection built into it. the Go compiler can figure out when to follow these points is to some extent and figure out when this point you’re still in use.
There are many ways of doing it but generally, you have to keep track of the pointers to a particular object. when all the pointers are gone, then you know that the object can be deallocated.
So, garbage collection in the Go language allows two things. first thing is, it will actually allocate data on the heap and the stack itself. So, we want to put this on the heap and stack. The Go language compiler put that code in there at compile-time, it will figure out this needs to go heap or stack and it will garbage collect appropriately. So, if it’s on the heap it will garbage collected appropriately will see when all the pointers are gone and it will determine when it can be deallocated.
This is a really helpful thing. So, there’s a performance hit but it’s a very simple implementation and garbage collection is so darn useful is probably worth it to put it in Go language. It slows things down a little bit but it is a very great advantage because it makes programming a lot easier. You don’t have to go as far as using a full-on interpreter like you would in an interpreted language.