The Wonders of Garbage Collection
robertalopez
Posted on July 2, 2020
When I first signed up for this course, I was excited to learn new programming languages, web design, and, weirdly enough, to experience garbage collection. To give more context, garbage collection is a form of automatic memory management used by your computer so that you, the programmer, don’t need to worry about managing your own memory while writing your code. The actual code behind garbage collection is extremely complex and an adventure of its own. Here is another post I found useful in understanding what goes on behind the scenes of garbage collection.
Up to this point, I have only had experience with coding in C, which, along with C++, is one of the few languages that rely on you to completely control your own memory.
What does manual memory management look like?
In ruby, to create an array you could simply so the following:
my_array = [1, 2, 3, 4]
Once declared, you can manipulate and access that array however you need to.
my_array << 5
my_array = [1, 2, 3, 4 ,5]
In, C, I could create an array in a similar way:
int my_array[4] = {1, 2, 3, 4};
What if I want to add to my array? In C, we have to specify the size of our array, so if we need to suddenly add to our array, we would need to use dynamic memory. C uses four main functions to manage dynamic memory: malloc(), realloc(), calloc(), and free(). Malloc and calloc both create a block of memory for you. Realloc allows you to change the amount of memory you need for a block you already created. Free erases all that contents you created in that block of memory. To create our previous array with dynamic memory:
*int my_array;
my_array = (int*)malloc(4 * sizeof(int));
Every value in my_array is empty upon declaration; if I had used calloc instead, then every value would have been set to 0 instead. To fill in my_array I would need a for-loop:
For (i = 0; i < 4; i++) {
my_array[ i ] = i + 1 ; }
What if I wanted to double my array? I would need to inform my computer that my_array needs to be given more space than I initially asked for using the realloc function:
my_array = ( int* )realloc( my_array, 8 * sizeof(int));
I would again need a for-loop to fill in the rest of the expanded array.
for ( i = 4; i < 8; i++) {
my_array[ i ] = i + 1; }
What happens once my program is done?
In C you must make sure to free all of your created memory.
free(my_array);
Failure to do so will cause memory leaks on your computer; a memory leak is memory on your computer that was manually set aside but was never freed, so your computer thinks it’s completely off-limits. Enough memory leaks could cause your entire system to crash.
But don't worry! In ruby garbage collection takes care of it all for us once our program ends, so we don't have to worry about it.
Why would any programming language force manual memory management?
The actual programming behind garbage collection slows your computer down a considerable amount. In order to give the potential for more efficient programming, languages like C, make the user responsible for all their memory management.
Posted on July 2, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024