⚠️ This lesson is retired and might contain outdated information.

Memory allocation and garbage collection in Javascript

Yonatan Kra
InstructorYonatan Kra
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 2 years ago

In this lesson, we will learn what are memory allocation and garbage collection. We will also learn how to monitor garbage collection and its effect on performance. Finally, we will learn how you can help the JS engine run faster by pre allocating memory.

Instructor: [00:01] We are going to compare two functions. The first is buildDynamicArray, which accepts an integer n and iterates n times. In every iteration, it pushes the index into the array.

[00:13] The second function is buildPreallocatedArray, which also accepts an integer n, instantiates an array of size n and fills it with 0s. It then iterates n times and adds the index value in the correct array index. The app runs 20,000 times with n=1,000.

[00:30] I'll start the process and they will work a second apart from each other. We can see that buildDynamicArray took much longer to run. I'll run the process again and we see that the results are consistent.

[00:49] Let's head to the Performance tab and take a deeper look. I start recording and start the process again. The process is done, so I'll stop the recording. Let's focus on the build dynamic area and we could search for something called Minor GC. We can see it repeats 277 times.

[01:11] GC stands for garbage collection. Garbage collection is a periodic process that is run by the JavaScript engine. It clears the memory from objects that no longer needed. It has a bigger impact the more we allocate and delete objects in our code.

[01:26] If we focus on the buildPreallocatedArray, we see that the garbage collection repeats only 77 times. We found the difference in performance. Let's see how pre-allocation is better than dynamic.

[01:42] BuildDynamicArray creates an empty array and pushes new elements to the array. The JavaScript engine needed to extend the array and find it a consecutive place in memory. It allocated the memory needed for the bigger array, then copied the existing array to the newly allocated memory and pushed the new element to the array.

[02:02] Finally, it marked the old small array to be garbage collected. This is why we have a lot of garbage collection instances. buildPreallocatedArray, on the other hand, instantiates an array of n members. Because the array was pre-allocated, there was no need for dynamic allocation during the for loop. With that, less need of garbage collection.

[02:26] As you recall, we still had 77 garbage collection instances in the pre-allocated array. These garbage collection instances are because we did allocate the array every time we entered the function and released it when the function was done.

[02:40] To summarize, we saw memory allocation and garbage collection effects on our code's efficiency. Pre-allocation reduced memory allocation and garbage collection, which improved performance.

egghead
egghead
~ 24 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today