In programming languages like C and C++, there are two types of memory allocation: static and dynamic. Static memory allocation is what happens when we create a global or local variable with a single fixed size. The program allocates once when the program is started and never frees it until the application ends execution. Examples in C and C++ include typed variables and arrays. The other type is dynamic memory allocation. We can create these ‘objects’ with any size using malloc(), and the number of bytes they contain can be altered during runtime using realloc(). We can free the memory things allocated this way manually using free().

Image for post

an example of what dynamic memory allocation looks like in C

These things can create unforeseen memory leaks as well as complications, like the need to know the difference between a variable(static) and a pointer (dynamic) or between an array (fixed length) and a linked list (with a size that can change dynamically). People have been using C and C++ both for a very long time, and these languages are probably not going away any time soon, but fortunately, many languages today abstract these concepts away from the programmer.

Languages like Java, C#, JavaScript, and Ruby abstract away all of these things away from the programmer using a system known as Garbage Collection. There are a few approaches that the collector like the one in JS uses. Still, the basic idea is something called the Memory Cycle: we allocate the memory, the program uses that memory, and then when the memory is not needed anymore, the garbage collector frees that memory.

#technology #javascript #memory-management #garbage-collection #cplusplus #c

How JavaScript Turns Complex Memory Management into Magic
1.25 GEEK