Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Tracing Memory Allocations with Allocation Profilers

Hello! Welcome to the fourth lesson in our module on Memory Leak Detection and Analysis.

In our last session, we focused on a detective-style approach to memory leaks. Using heap snapshots and the Retainers panel, we learned how to trace the reference chain for a specific leaked object back to its root cause. This technique is invaluable for finding objects that are created and incorrectly held in memory forever.

Today, we shift our perspective from static snapshots to dynamic analysis. Instead of just asking what is in memory, we'll ask when and where memory is being allocated. Our goal is to use the Allocation Timeline and Allocation Sampling profilers to trace memory allocations and identify their origins in the code.

These tools help us hunt down a different class of memory-related performance problems. Sometimes, the issue isn't a permanent leak, but excessive memory "churn"—the rapid allocation and disposal of temporary objects. This forces the garbage collector (GC) to work overtime, leading to pauses, stuttering, and what users perceive as "jank," even if no memory is permanently lost. For a senior developer, mastering these tools is key to building highly responsive, fluid applications.

Two Profilers, Two Questions

The Memory panel offers two primary tools for analyzing memory allocations over time:

  1. Allocation timeline: Answers the question, "When is memory being allocated?" It provides a visual timeline of memory usage, showing new allocations as they occur.
  2. Allocation sampling: Answers the question, "Where (in which function) is memory being allocated?" It attributes allocations to specific JavaScript functions, helping you find the "heavy hitters" in your code.

Let's explore the first one.

1. Visualizing Memory Over Time: The Allocation Timeline

The Allocation Timeline profiler is your go-to tool when you suspect a user action is causing memory to grow or churn. It works by taking frequent snapshots of the heap and tracking the differences.

To get a clear idea of what this profiler does and how it differs from Allocation Sampling, let's start with a short video.

Only The Best Developers Understand How This Works

This video from Web Dev Simplified provides a concise introduction to both the Allocation Timeline and Allocation Sampling profilers, explaining their primary purpose.

Watch from 09:59 to 11:53. Pay close attention to the distinction between the two profilers and the demonstration of the blue bars that appear in the timeline as memory is allocated.

As you saw, the timeline is marked with bars:

  • Blue bars represent objects that were allocated and are still live at the end of the recording. A steadily growing series of blue bars is a classic sign of a memory leak.
  • Gray bars represent objects that were allocated but have since been garbage collected during the recording session. A lot of gray bars might indicate high memory churn.

For a more detailed breakdown of how to read the timeline report, the official documentation is an excellent resource.

How to Use the Allocation Timeline Tool

This official Chrome DevTools documentation explains how to interpret the Allocation Timeline's visual output, including the meaning of the bar colors and how to zoom in on specific timeframes for analysis.

Please read the sections 'How the tool works' and 'Read a heap allocation profile'. This will reinforce your understanding of the blue vs. gray bars and how to interact with the timeline.

Practical Example: Finding a Leak with the Timeline

Now, let's see the Allocation Timeline in action to find a memory leak. The following clip demonstrates a situation where a Map is continuously growing, a scenario that is visualized perfectly by the timeline.

Only The Best Developers Understand How This Works

Continuing with the same video, this segment shows how to use the Allocation Timeline to spot a memory leak caused by a continually growing data structure.

Watch from 11:53 to 13:53. Notice how the continuous allocation of memory shows up as an uninterrupted wall of blue bars. By stopping the recording and inspecting the largest objects, the presenter quickly identifies the leaking Map which is consuming a huge percentage of the application's memory.

This example highlights the power of the timeline for visualizing memory growth that occurs during a specific period. By recording only while the problematic action is happening, you can isolate the allocations related to that action.

2. Pinpointing the Source: The Allocation Sampling Profiler

While the timeline shows you when allocations happen, the Allocation Sampling profiler tells you which functions are responsible. It samples memory allocations at intervals and aggregates the data by function, giving you a call tree of memory usage. This is particularly effective for finding sources of high memory churn.

Because it samples rather than tracking every single allocation, it has lower performance overhead than the Allocation Timeline, making it suitable for longer recording sessions.

Let's watch a demonstration.

Plugging Memory Leaks With The Chrome Dev Tools - Ben Dilts

Ben Dilts from UtahJS demonstrates how to use the Allocation Sampling profiler (which he calls an 'Allocation Profile') to find the functions generating the most memory in a game application.

Watch from 26:25 to 28:55. Observe how he uses the 'Heavy (Bottom Up)' view to sort functions by the total memory they allocated. He then drills down into the call tree to find the exact line of code responsible for creating objects.

A Real-World Use Case: The Problem with Garbage

The true power of this tool shines when debugging performance issues caused by excessive garbage collection, not just permanent leaks. The next clip presents a compelling real-world case from Lucidchart where the team faced a massive performance bottleneck.

Plugging Memory Leaks With The Chrome Dev Tools - Ben Dilts

This is a fantastic real-world example. The presenter describes a scenario where 80% of a document's load time was spent in the garbage collector due to excessive temporary object creation. The Allocation Sampling profiler was the key to solving it.

Watch from 28:55 to 34:59. Listen to the description of the problem: the issue wasn't what was left at the end, but the 3+ gigabytes of garbage created during loading. He also introduces the 'Chart' view, which provides a flame chart visualization where the width of a function block is proportional to the memory it allocated, making it easy to spot the biggest offenders.

This example from Lucidchart is crucial. It shows that a major performance problem can exist even when the final heap is clean. The cost of creating and then cleaning up gigabytes of temporary objects can bring an application to its knees. The Allocation Sampling profiler is the specific tool designed to diagnose this exact problem.

To round out your understanding, here is the official documentation for this profiler.

Fix memory problems | Chrome DevTools

Finally, review the official documentation for a step-by-step guide to using the Allocation Sampling profiler.

Read the section titled 'Investigate memory allocation by function'. It provides a concise summary and shows the interface you'll be working with.

Test your understanding!

You are profiling a single-page application that includes a data grid. When you sort a large column, the UI briefly freezes.

You suspect a memory-related issue. You use the Heap Snapshot tool, taking one snapshot before the sort and one after. The Comparison view shows that while many objects were created, almost all of them were garbage collected, and the final heap size is nearly unchanged.

Which profiler should you use next to diagnose the freeze, and what specific pattern would you be looking for in its output?

Show answer

You should use the Allocation Sampling profiler next.

The fact that the heap size returns to normal indicates this isn't a classic memory leak. The freeze is likely caused by high memory churn—the sorting algorithm is creating a massive number of temporary objects (e.g., for comparison, temporary arrays, or data transformation). This forces a major garbage collection event, which blocks the main thread and causes the UI to freeze.

You would start an Allocation Sampling recording, perform the sort, and then stop the recording. In the results, you would look at the Heavy (Bottom Up) view to identify the functions at the top of the list. These are the functions that allocated the most memory during the sort operation. By examining their call stacks, you can pinpoint the part of the sorting logic that is inefficient and refactor it to be more memory-conscious (e.g., by modifying data in place instead of creating new copies).

Conclusion

Today, you've added two powerful, dynamic memory analysis tools to your arsenal. You've learned to move beyond static heap analysis and investigate how memory is allocated over time and by which parts of your code.

Key Takeaways:

  • Allocation Timeline is your tool for visualizing memory allocations over time. Use it to answer "When is memory being created?" It's perfect for correlating memory growth or churn with specific user interactions. The blue bars reveal objects that are retained, making it great for spotting leaks as they happen.
  • Allocation Sampling is your tool for attributing memory creation to its source. Use it to answer "Where is memory being created?" It helps you find functions that are "heavy hitters" in terms of allocation, which is the key to solving performance issues caused by garbage collection pressure, even when there are no permanent leaks.
  • Problems like UI jank or freezes are often caused by high memory churn (many gray bars in the timeline), not just permanent leaks (growing blue bars).

In our next and final lesson, we will synthesize what we've learned about memory analysis and apply it to a ubiquitous and challenging context: detecting memory accumulation across route changes in single-page applications. This will be a practical application of the skills you've built throughout this module.

Can't find a good explanation? Sign up and we'll make it for you

Sign up