• Level Up Coding
  • Posts
  • LUC #55: Understanding and Addressing The Silent Perfomance Killer—Frontend Memory Leaks

LUC #55: Understanding and Addressing The Silent Perfomance Killer—Frontend Memory Leaks

Plus, database caching strategies explained, what is clean architecture and how does it work, and cookies vs sessions

This week’s issue brings you:

READ TIME: 4 MINUTES

A big thank you to our partner Postman who keeps this newsletter free to the reader.

Who doesn’t love a custom theme?

Postman released custom themes in their v11 release. Check it out.

Understanding and Addressing Frontend Memory Leaks

As applications get more complicated and powerful, there is an increasing need to improve performance.

To help meet performance goals engineering teams have turned their attention to improving system inefficiencies, such as memory leaks.

Garbage collection is an important component in memory management. It is an automated process that identifies and frees up memory that's no longer in use by the application.

Memory leaks result from poorly managed memory allocation. When memory is not properly freed after it is used, leaks occur which causes unnecessary resource consumption.

Frontend memory leaks can have a significant impact on the performance of web applications. They can cause pages to load slowly, use up unnecessary memory, and occasionally cause crashes.

They can be a serious problem in single-page applications (SPAs) where pages remain in the browser for extended periods, allowing memory leaks to build up.

Memory leaks can be caused by various factors, but some common culprits include the following:

Uncleared event listeners

Event listeners are attached to elements in the DOM, forgetting to remove these listeners prevents the element from being garbage collected. This means that the element will continue to consume resources, even if they’re no longer part of the DOM.

Out-of-scope references

Closure functions that refer to variables outside of their scope keep these variables in memory.

Memory leaks occur if references to objects are not cleared.

Long-lived variables

Variables that contain large objects should be cleared once they're no longer needed to limit unnecessary memory usage.

Lingering resources

Open connections or streams to external resources can lead to a considerable amount of resource consumption.

Detecting memory leaks is a difficult challenge.

Browser developer tools such as memory profilers can help identify areas that need the most attention.

These tools have advanced capabilities to help further investigate potential memory leaks, such as capturing heap snapshots and detailed memory allocation tracking.

Monitoring tools should be used to track trends and identify spikes in memory usage and drops in performance.

Advanced tools provide garbage collection insights that let you know how often the garbage collector process runs, and how much memory is freed up each time.

The best way to avoid memory leaks is to set up processes that prevent them from reaching production. Thorough code reviews and testing are key lines of defense.

Load tests are particularly helpful as memory leaks tend to become more apparent during high levels of load.

To effectively catch memory leaks during code reviews, developers must have a solid understanding of the best practices for avoiding them. Below are some key approaches to ensure optimal memory usage and prevent potential issues:

  • Clear event listeners once they are no longer needed.

  • Avoid global variables.

  • Release connections or streams to external resources when they are no longer in use.

  • Clear large data structures and remove their references.

Improving memory management and monitoring for memory leaks can lead to better, more reliable web applications that deliver a seamless user experience while minimizing resource wastage.

Database Caching Strategies Explained (Recap)

Five of the most common methods:

Cache-aside strategy — applications control caching by checking the cache before the database. Ideal for dynamic data but requires manual management, increasing complexity.

Write-through strategy — writes simultaneously to the cache and database, ensuring consistency. Suitable for high reliability but increases write latency.

Write-behind strategy — caches data first and updates the database asynchronously. Boosts responsiveness but risks data loss if the cache expires before syncing.

Read-through strategy — automatically caches data on a miss, simplifying code. Initial cache misses can delay retrieval.

Write-around strategy — writes directly to the database, refreshing the cache on read requests. Prevents cache saturation, best for write-intensive applications without immediate read access needs.

What is Clean Architecture, and How Does it Work? (Recap)

Clean Architecture offers a prescriptive approach to creating systems with interchangeable components, focusing on maintainability, flexibility, and encapsulation of business logic.

Core layers of clean architecture:

1) Entities layer: Centralizes enterprise-wide business rules.

2) Use cases layer: Encapsulates and implements application-specific business rules.

3) Interface adapters layer: Bridges internal logic and external interfaces.

4) Frameworks and drivers layer: Manages interactions with external technologies.

The essence of Clean Architecture lies in its domain-centricity, placing business logic at the forefront. By enforcing separation of concerns and a dependency rule, it ensures that changes in frameworks and interfaces have minimal impact on the core business logic.

Cookies vs Sessions — What’s The Difference? (Recap)

HTTP is stateless; it treats each client-server request as new, with no memory of previous interactions. That’s where cookies and sessions come in.

Cookies

  • Stored on the user's device and sent with each request.

  • Ideal for maintaining login status, user preferences, and tracking activity.

  • Due to their ease of use and capacity to save user preferences between sessions, cookies are a popular choice for preserving user state in applications.

  • Methods like token-based authentication (e.g., JWT) can enhance or replace traditional session cookies, increasing flexibility and security.

Sessions

  • Data stored on the server, providing a secure environment for sensitive information.

  • Use cookies for client-side identifiers but manage data server-side.

  • Best for managing sensitive data during user interactions, like authentication or transactions.

That wraps up this week’s issue of Level Up Coding’s newsletter!

Join us again next week where we’ll explore database types, the HTTPS workflow, Git branching strategies, and more.