Why do programming languages need garbage collection?
Our programs need memory, typically in the form of variables and objects, to do their job. The objects are either allocated on Stack or Heap.
Stack allocated objects
A locally declared variable "int a = 10;" is allocated on the stack i.e. the stack frame of the function call and hence when the function returns the stack frame is popped, making the variable non-existent. Hence variables allocated on Stack do not need to be freed explicitly.
Heap allocated objects
A variable allocated on the heap is typically done through functions like the "new" or "malloc". The object space allocated for such entities is in RAM and they outlive the function scope and execution, and hence they need to be explicitly freed as we are done with it.
Why do we need a Heap?
Objects assigned on Heap need to be garbage collected, but why do we need the heap in the first place? There are 3 main reasons:
Garbage Collection: Explicit De-allocation
Primitive programming languages like C and C++ do not have their garbage collection instead expect the developer to not only allocate the object but also deallocate it explicitly. Hence we see the functions like "malloc" and "free".
The objects we allocate using "malloc" will continue to exist unless they are reclaimed using "free". The explicit need to "Free-ing" the allocated object is called Explicit Deallocation.
Although cleaning up the mess we created is a good idea, it is not reliable that we rely on the engineers and developers to always free the objects they allocated. Hence this gives rise to the need for automatic cleanup of unused variables- automatic garbage collection.
The two key side-effects of not cleaning up the unused objects we allocate are
Hence, to reduce human error, and make the process more reliable and performant the runtimes of the programming languages implement their automatic garbage collection.
Here's the video of my explaining this in-depth ?? do check it out
领英推荐
We know how important Garbage Collection is for any programming language. So, today we explore why programming languages need automatic garbage collection in the first place?
In this video, we understand - the basics of memory management, the need to allocate objects on the heap, the constructs of explicit deallocation, what happens when we do not do our garbage collection well, and why we need automatic garbage collection.
Outline:
You can also
Thank you so much for reading ?? If you found this helpful, do spread the word about it on social media; it would mean the world to me.
Yours truly,
Arpit
Until next time, stay awesome :)
I teach a course on System Design where you'll learn how to intuitively design scalable systems. The course will help you
I have compressed my ~10 years of work experience into this course, and aim to accelerate your engineering growth 100x. To date, the course is trusted by 600+ engineers from 10 different countries and here you can find what they say about the course.
Together, we will build some of the most amazing systems and dissect them to understand the intricate details. You can find the week-by-week curriculum and topics, benefits, testimonials, and other information here https://arpitbhayani.me/masterclass.