DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Deno vs. Node.js: The Showdown Nobody Asked For But Everyone Needed
  • Charge Vertical Scaling With the Latest Java GCs
  • Node.js Performance Tuning: Advanced Techniques to Follow
  • How To Capture Node.js Garbage Collection Traces

Trending

  • MCP Servers: The Technical Debt That Is Coming
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • The Future of Java and AI: Coding in 2025
  • Scaling Microservices With Docker and Kubernetes on Production
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. How To Analyze Node.js Garbage Collection Traces

How To Analyze Node.js Garbage Collection Traces

Explore the process of enabling GC traces, interpreting the trace data, and the right tools and knowledge needed to study the Garbage Collection behavior.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Apr. 25, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
1.3K Views

Join the DZone community and get the full member experience.

Join For Free

Is your Node.js application experiencing unresponsiveness or performance bottlenecks? The problem could have originated because of long-running Garbage Collection pauses or memory leaks. In such circumstances, you might want to study your Node.js application’s Garbage Collection performance. 

In this post, we’ll walk you through the process of enabling GC traces, interpreting the trace data, and the right tools and knowledge needed to study the Garbage Collection behavior.
Analyze Node.js GC Traces graphic

How To Enable Node.js Garbage Collection Traces

There are a few approaches to enable the Node.js Garbage Collection traces. The easiest and most straightforward approach is to pass the –trace-gc flag along with your usual invocation command. 

Example:

node --trace-gc my-script.mjs


Once the –trace-gc flag is enabled, your Node.js application will start generating Garbage Collection traces in the console output. These traces provide valuable insights into memory usage, GC events, and potential performance bottlenecks. Garbage Collection traces would look something like this:

[721159:0x61f0210]  1201125 ms: Scavenge 27.7 (28.8) -> 26.8 (29.8) MB, 0.5 / 0.2 ms  (average mu = 
0.999, current mu = 0.970) allocation failure 
[721166:0x5889210]  1201338 ms: Scavenge 30.7 (32.1) -> 29.7 (33.1) MB, 0.6 / 0.3 ms  (average mu = 
0.998, current mu = 0.972) allocation failure 
[721173:0x54fc210]  1202608 ms: Scavenge 26.8 (28.3) -> 25.8 (29.3) MB, 0.7 / 0.4 ms  (average mu = 
0.999, current mu = 0.972) allocation failure 
[721152:0x54ca210]  1202879 ms: Scavenge 30.5 (31.8) -> 29.6 (32.8) MB, 0.6 / 0.2 ms  (average mu = 
0.999, current mu = 0.978) allocation failure 
[721166:0x5889210]  1202925 ms: Scavenge 30.6 (32.1) -> 29.7 (33.1) MB, 0.7 / 0.3 ms  (average mu = 
0.998, current mu = 0.972) task 
[721159:0x61f0210]  1203105 ms: Scavenge 27.7 (28.8) -> 26.7 (29.8) MB, 0.4 / 0.2 ms  (average mu = 
0.999, current mu = 0.970) allocation failure 
[721173:0x54fc210]  1204660 ms: Scavenge 26.8 (28.3) -> 25.8 (29.3) MB, 0.5 / 0.2 ms  (average mu = 
0.999, current mu = 0.972) allocation failure 


How To Analyze Node.js GC Log

Garbage Collection traces contain a rich set of information such as how many objects were created, how many objects were garbage collected, how long each Garbage Collection event took to complete, how much memory was reclaimed after every GC event, whether there were any memory leaks, etc. However, it’s quite a tedious and time-consuming process to interpret the GC traces manually. 

You may consider using the GCeasy online tool to analyze the Node.js GC traces. You can go to the GCeasy tool, sign up for a free account, and upload the GC trace. The tool will instantly parse the GC traces and generate a report that contains vital Garbage Collection analysis metrics and graphs. For your reference, here is the live report generated by the tool. 

Node.js GC Trace Analysis Report

The report provides a rich set of graphs, metrics, and statistics around Garbage Collection overhead added to the Node.js application. Below are some of the excerpts from the GCeasy report.

Heap usage graph
Figure 1: Heap usage graph

The "Heap usage" graph reports the memory trend after every GC event. You can notice that after GC events, memory usage drops.

GC Duration Time Graph
Figure 2: GC Duration Time Graph

The "GC Duration Time" Graph indicates the time taken by each GC event to run. The red triangle indicates it’s a full GC event and the green square icon indicates it’s a young (or minor) GC event. You can notice that in general, full GC events (i.e., red triangle) are running less frequently, but take more time. On the other hand, Young GC events (i.e., green square) are running more often but take less time. Full GC runs on all the regions in the Node.js memory, whereas minor GC runs only on the young region of the memory.

Reclaimed Bytes Graph
Fig 3: Reclaimed Bytes Graph

The "Reclaimed Bytes" graph shows the amount of memory reclaimed after each GC event. You can notice that full GC events are reclaiming more memory than young GC events.

GC Statistics section
Figure 4: GC Statistics section

Besides giving a graphical visualization of the Garbage Collection behavior, the tool also gives a lot of statistical metrics. The "GC statistics" section above reports the total number of GC events, their average time, min/max time, standard deviation, etc. These metrics are vital to studying the overhead added by the automatic garbage collection to the Node.js application.

GC Causes section
Figure 5: GC Causes section

Another interesting section in the report is the "GC Causes" section. This section reveals the reasons why the Garbage Collection got triggered in the application. For example, in this application, 50,965 GC events were triggered because of "Allocation Failure." This happens when there is a lack of memory in the young generation.

Conclusion

Node.js garbage collection analysis provides insights into your application’s memory management, performance, and stability. By understanding and interpreting GC traces, you can uncover hidden performance bottlenecks, identify memory leaks, and optimize memory usage for optimal application performance. Armed with this knowledge, you can make informed decisions to optimize your code, fine-tune garbage collection parameters, and address any underlying issues affecting performance.

Node.js garbage collection Performance

Published at DZone with permission of Ram Lakshmanan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deno vs. Node.js: The Showdown Nobody Asked For But Everyone Needed
  • Charge Vertical Scaling With the Latest Java GCs
  • Node.js Performance Tuning: Advanced Techniques to Follow
  • How To Capture Node.js Garbage Collection Traces

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!