Why do programming languages need garbage collection?

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:

  • We cannot grow your stack-allocated objects dynamically,
  • We need dynamically growing objects like Arrays, LinkedList, Trees
  • We might need objects that could be larger than what Stack can fit in
  • We might need to share the same object across multiple threads
  • We do not want our functions to copy and pass bulk objects

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

  • Memory Leak: Leading to an eventual process crash
  • Dangling Pointer: Program behaving unpredictably

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:

  • 00:00 Basics of Memory Management
  • 04:39 Why do we need heap allocations?
  • 06:59 Explicit Deallocation constructs
  • 09:26 Memory leak - when we do not delete an allocated object
  • 11:00 Dangling pointer - when we dereference an already freed object
  • 15:08 Why we need automatic garbage collection

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.

You can also follow me on your favourite social media LinkedIn, and Twitter.

Yours truly,

Arpit

arpitbhayani.me

Until next time, stay awesome :)

No alt text provided for this image

I teach a course on System Design where you'll learn how to intuitively design scalable systems. The course will help you

  • become a better engineer
  • ace your technical discussions
  • get you acquainted with a massive spectrum of topics ranging from Storage Engines, High-throughput systems, to super-clever algorithms behind them.

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.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了