Let’s talk about a core computer science concept: garbage collection. It’s key to managing memory for many languages. How does your preferred language handle it?
Computer science is the study of computers, algorithmic processes, computational machines, systems and computation itself. It includes theory, design, development and much more about computers. It is a vast and complex field, where there’s always something new to pick up and learn.
We’re going to learn about a very important computer science concept in this article: garbage collection—an important concept that’s related to how computers and programming languages handle memory. We’re going to cover how garbage collection works in some programming languages, the benefits of it and why it’s so important for the programming itself.
Computers only understand one single language called machine language. Machine language is a set of instructions that are passed to the computer and expressed in binary.
Imagine programming only using 1s and 0s. Pretty boring, right?
Higher-level languages were created to make it easier for developers to program. They abstract the complexity and make programming simpler and understandable.
The first important higher-level programming language was Fortran, created back in 1957 by IBM for scientific and engineering applications.
A programming language is a formal language that computers understand. We pass a set of instructions using a programming language and we will get an output.
Memory management plays an important part in how computer programs work. Memory management keeps track of each memory location and provides ways for the allocation and removal of data.
Memory management is the process of how computers control and access memory.
Memory management is the process of controlling and coordinating the way a software application accesses computer memory. – Deepu K Sasidharan
When software is executed on your computer, there are two important memory location parts that it uses: Stack and Heap.
Stack is temporary memory space. The data is added or removed using the last-in-first-out (LIFO) method.
Stack is not a good memory location when you need to store big data. It’s not possible to remove a specific object from it. For fetching a specific data value in a stack, the computer needs to go over all the objects on top first.
Heap is the part of your memory where data may be allocated at random access. It is where the dynamically allocated memory resides and it can be allocated or deallocated without any order.
Memory management is a very important topic for developers. Knowing exactly how the computer and programming languages handle memory is a very good skill. It helps developers write better, more performant and resilient code.
Regardless of the memory management technique, learning about how the code is managing the memory is important. Depending on what we want to build, sometimes our programs can be very memory-expensive.
Garbage collection is a process for automatic memory management. The collector manages to collect garbage code (data, objects, functions, variables) or memory occupied by objects that are no longer in use.
Garbage collection plays an important part in some modern languages. It is implemented differently in different languages, though. Higher-level languages are more likely to have garbage collection as a standard feature.
Garbage collection frees developers from manual memory management. For example, when you’re working with low-level languages such as C language, most of the time you are going to manually allocate and deallocate the memory.
The C language provides several functions for handling memory allocation and management. The most used are:
Working with C sometimes can be very painful. You need to care about memory management, making a safe copy, deallocating at the right time and correctly, etc.
There’s a library for garbage collection in C and C++ called Boehm GC. It is a conservative garbage collector for replacing the for C malloc or C++ new.
Java is another language that makes use of garbage collection. It is automatically done by the JVM (Java Virtual Machine), a virtual machine that enables a computer to run Java programs.
Unlike in C, you don’t need to handle the memory management using functions. In Java, there are four types of garbage collection:
Choosing the right garbage collection for your Java application really depends on a lot of factors—the way your application handles memory, the lifetime of your objects, how your machine works, etc.
JavaScript works with garbage collection. When you create something in JavaScript—for example, a function—the value is allocated. When this function or other creation is no longer used, it is deallocated from memory automatically.
You might have heard of memory leaks before. Memory leaks usually happen when the memory is incorrectly allocated or when data is allocated in memory but cannot be accessed by the running code.
Rust, for example, has no garbage collection. The moment that the code is no longer used and would become garbage, it gets freed immediately. You don’t have to manually release memory—the language does it automatically for you.
Garbage collection, used the right way, can make a developer’s life easier when it comes to memory management. When you’re done with some code, the memory occupied by this code will automatically be cleaned up.
Garbage collection can be good or bad in different situations. It will depend on a bunch of variables—which programming language you’re using, how you’re structuring your application, what you’re using it for, etc.
Leonardo is a full-stack developer, working with everything React-related, and loves to write about React and GraphQL to help developers. He also created the 33 JavaScript Concepts.