Loading...

Memory leaks

In Acquia Node.js Hosting, memory limit of 1.8GB is assigned to each Node.js process. If a process tries to claim more than the assigned memory, the platform restarts the Node.js process forcefully. It usually happens because of memory leak in the application. 

This document provides a series of necessary steps to identify, troubleshoot, and resolve memory leaks in Node.js applications hosted on Cloud Platform. While it does not focus on troubleshooting performance issues in JavaScript applications, the methods presented may also assist in addressing these concerns.

Identifying memory leaks

The following are the methods to identify memory leaks in Node.js applications hosted on Cloud Platform:

Reviewing Out Of Memory (OOM) errors on Cloud Platform

Check for Out Of Memory (OOM) errors on Cloud Platform with the following steps:

  1. Sign in to the Cloud Platform user interface.
  2. Select your application and environment.
  3. In the left menu, click Stack Metrics.
  4. Click the Application tab.
  5. Locate the Out of memory errors section and review the instances where OOM errors have caused application restarts.

    Data points that exceed 0 indicate the time when the Node.js application restarted because of OOM errors.

  6. Locate the Memory usage section and track the application memory usage.
  7. Monitor for a persistent rise in the memory usage that remains high even when the application is idle.

    For example, the following image shows that the memory usage gradually increases to nearly 70% before being reset or terminated by the Linux system an because of an OOM event. The process then restarts and is terminated once more upon reaching the memory constraints.

    The memory usage shown corresponds to the server's capacity. Therefore, it does not reach 100% because memory is still available in the system. 

Examining metrics through APM tools

Application Performance Management (APM) tools such as New Relic provide significant assistance in diagnosing memory leaks. Node.js customers receive complimentary access to a New Relic instance for performance monitoring.

To diagnose memory leaks through APM tools:

  1. Obtain your New Relic credentials by following the instructions on Claiming your New Relic APM Pro account
  2. Log in to New Relic and click APM & Services > Node VMs.

  3. Select the VM instance of your application.

    For example, select container_rootfs in the preceding image.

  4. Examine metrics for the VM instance:

GC pause time

During the Garbage Collection (GC) phase, the application halts and does not process any requests. If the pause time is longer, it indicates that the application is dedicating more time to cleanup tasks. With a memory leak, the garbage collector does not reclaim memory effectively even after substantial GC activity. Significant and recurring spikes in GC pause time suggest a memory leak, which negatively affects the application's performance.

GC pause frequency

An increased frequency of GC pauses suggests more frequent garbage collections, which may point to a memory leak.

Memory usage (MB)

In a well-functioning application, the garbage collector releases memory each time it activates. If the memory is not released at consistent intervals, it suggests a memory leak. In the following chart, the freed-up memory is gradually decreasing after every GC event.

Preventing memory leaks through logging

Logging can be employed to monitor and prevent memory leaks. For example:

  • Utilize the inherent process.memoryUsage() function of Node.js to systematically record memory utilization statistics.
  • Document metrics such as heapTotal, heapUsed, and rss.

When substantial proofs of memory leaks exist, follow these instructions to address them.

Addressing memory leak issues

Heap snapshots:

  • Employ Chrome DevTools or the Node.js --inspect flag to capture heap snapshots.
  • Examine snapshots from different intervals to identify the objects that are not subject to garbage collection.

Analyze Heap snapshots:

  • Check the objects that are increasing in size over time.
  • Be vigilant of detached DOM nodes, untracked listeners, or escalating data structures.

Utilize profiling tools:

  • Apply tools such as clinic.js (formerly node-clinic) or 0x to profile your application and pinpoint memory leaks.
  • Employ v8-profiler-next or node-inspect for a more in-depth analysis.

Identify common sources, such as:

  • Global variables or closures retaining references.
  • Persistent event listeners.
  • Indefinitely expanding caches.
  • Database connections or file handles that remain open.

Revise the code:

  • After pinpointing the leak's origin, alter your code to facilitate memory release.
  • Confirm the removal of unnecessary event listeners.
  • Incorporate weak references (WeakMap, WeakSet) for caches when fitting.

Enhance data structures:

  • Adopt more efficient data structures to avoid unnecessary retention of large objects.
  • Consider streaming data processing for extensive data sets.

Reorganize for improved memory management:

  • Divide extensive functions or modules into smaller, more digestible segments.
  • Incorporate design patterns conducive to adept memory utilization, such as the Flyweight pattern.

Conduct rigorous testing:

  • Verify the resolution of the memory leak by testing your application under simulated operating conditions after modifications.
  • Implement automated tests to mimic actual usage scenarios.

Mitigate future memory leaks:

  • Code reviews:
    • Implement a code review process to catch potential memory leaks before they reach production.
  • Continuous monitoring:
    • Set up continuous monitoring to detect memory issues early.
    • Use alerts to notify you of unusual memory usage patterns.
  • Educate the team:
    • Train your team on best practices for memory management in Node.js applications.
    • Share knowledge about common pitfalls and how to avoid them.

With these instructions, you can systematically detect, diagnose, and rectify memory leaks within your Node.js application, and obtain enhanced stability and efficiency.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation