Simplest guide to Garbage Collection.

Simplest guide to Garbage Collection.

Taking the trash out, coder style.

·

2 min read

When writing your beloved code you constantly take up space in the memory by allocating it to objects but what will happen if a certain part of your program is not currently in use and is sipping down on that precious memory juice of yours? That's where Garbage Collection kicks in.

In layman's terms, it's reclaiming memory that has been assigned to objects but isn't being used in any part of our program at the moment is called GC.

That is still very technical, let's look at an example.

Say you declare a variable "a" with

int a;.

And then proceed to point towards an object

a = 10;.

But now you want to change it just because you can

a=20;

What happens is that the object "10" is without a reference variable now but still resides in the previously allocated memory, hence garbage.

But now, how do we perform this mystic art of garbage collection? It depends.

Every programming language has its own approach to GC. The malloc() and dealloc() methods are used in C programming to manage memory allocation and deallocation. C# developers, on the other hand, do not have to worry about garbage collection, it takes care of itself.

I hope this was helpful. (>‿◠)✌

I am currently on my DSA journey with Java. I'll be updating this blog with all the new things that I'll learn along the way.