Does my NodeJS application leak memory? – 1

A Gentle Introduction

A journey of a thousand miles begins with a single step – Lao Tzu

One of the key requirements for writing software for emergency management is consistent performance. Every time user feedback and/or testing reveals performance issues, the question that always comes to my mind is whether the application is leaking memory or if is it something else. The last thing an emergency software developer wants to hear is that the alerts did not get to the recipients in time or that emergency personnel experienced sluggish performance.

Simply put, a memory leak is when an application can no longer efficiently manage its memory resources resulting either in a crash or sluggish performance. Putting it differently, a memory leak occurs when freed memory is not reclaimed for reuse. Depending on the size and frequency of leak an application could either crash or behave erratically. While all platforms and programming languages are susceptible to memory leaks, I will be restricting my scope to NodeJS/V8.

Memory leaks in the front end (or client) code can go unnoticed for a long time or may never be detected. This is because the user could shut down the browser or refresh the page before a potential memory leak causes a problem. This is not the case when you are writing backend server real time applications for messaging, authentication and Internet of Things. A server issue affects all its clients not just one user. It is not practical to restart the server every time there is an issue. 

The go to response of a developer is to scour the internet and typically end up examining heap dumps or watching the growth of memory. If one has not understood the theory behind memory leaks and garbage collection this exercise usually results in frustration. For example, just pure memory growth for some period of time does not necessarily mean that there is a leak.

I went through the same frustrations  like most of my fellow developers and decided that I had to find a simpler way to check for leaks. This was very important to me as all my work relates to developing NodeJS backend server apps.

One of the things we tend to ignore is the power of the logs. Both in terms of analyzing them and of implementing them in our code. Depending on the type of log, the vitals of the server app can be provided in realtime. This can provide warning of an upcoming problem, be it leak or a security threat.

There are a ton of log analysis and management tools, including monitoring tools available, but that creates a learning curve for a specific product which may not be available in the near future. Many also do not fit the budget of a small startup. Many free open source tools tend to have difficulty in keeping up with the evolution of NodeJS/V8.

In my quest to find something that was not dependent of any external product or that required code modification, I decided to stick with understanding the garbage collection trace events of V8/NodeJS. My journey took me through analyzing the V8 source code, thousands of lines of garbage collection traces, including a quick detour into the world of Linux internals.

In the series of posts that follow I will describe my journey on how I use the V8 logs to look for leaks starting with some basic information on memory management

Internet of Things and the power of Text Messaging

Text messaging or SMS(Short Message Service) is well known for alerts and notifications. Be it in a crisis or a marketing campaign. It is actually the most powerful medium for quick and effective dissemination of messages.

This is for good reason. In 2013, every 97 out of 100 citizens used a mobile phone. That is about  7 billion users. Compare this to the fact that 40 out 100 citizens are internet users.

SMS does have its shortcomings such as unreliable delivery or spoofing but it still remains the most ubiquitous of communication technologies which fed the frenzy of a USD 100 Billion global market in 2014.

Impressive statistics aside, SMS comes with its inherent power of simplicity and availability. It is native to every mobile phone, smart or otherwise. It works from day one of the service, and everyone knows how to use it. 7 Billion of them!

But, we have a new phenomenon growing and that is the Internet of Things(IoT). Essentially IoT consists of sensors(they see and hear), actuators(they do things) managed by a controller. A control system with internet thrown into the mix.

Experts estimate Billions of devices(sensors and actuators) will be connected. The most popular device in this fray is the mobile phone. Use of SMS now takes IoT to another level. Every mobile phone with SMS becomes a sensor and an actuator.

This is akin to using SMS for online banking authentication. A authentication code is received via SMS(sensor) and the user enters the code when prompted(actuator).

Using SMS allows IoT to reach users around the world and not only in developed cities and countries. A rice farmer in a developing part of the world would appreciate control over his crops via IoT if he could use a technology like SMS. Expecting him to watch dashboards or install apps on smart phones is not feasible.

The world is catching up to this concept, I think. No wonder the Global (application to person) A2P SMS market is expected to reach USD 70.32 Billion in 2020.

Companies like Nexmo, Twilio, Tropo and others are doing a fantastic job of bringing SMS to the doorstep of every app developer.

At the end everyone should be able to benefit from IoT, all 7 billion of them. SMS is one way.

In this regard posts by Tropo (Controlling the Internet of Things with Tropo ) and Mosaic NetworX(SMS and the Internet of Things: Text Messaging Between Connected Devices) are worth a read.

 

The many connected worlds of Internet of Things

The Internet of things seems to have caught the fancy of every one from the everyday consumer to the government. It is exciting indeed to think what we could accomplish by having devices talk to each other over the internet. The excitement is growing rapidly and smart devices are being released with such ferocity that one wonders that when  billions of devices are connected in the next few years will there be order or chaos.

From tracking pets to  doctors monitoring their patient’s health to cars being controlled via smart phones. From power plants to smart grids to smart cities.

Even though the concept of a connected world or the Internet of Things is being accepted with fervor, intuition dictates that we do this with a grain of salt. 

Every example that we see of a connected world actually is a very small connected world within a large number of connected worlds. A network of connected worlds so to speak.

Consider the home automation system. It is one connected home leading to a network of many homes in the same network. The network is then a collection of home installs for a given company. This means that a city could have multiple company networks like the cable companies have today. 

Some IoT installs use WIFI while some use ZigBee while some Bluetooth. Each minor connected world tends to behave independently of the other. Some for business reasons while some for convenience of implementation.

History has shown that many of these small worlds,  will gravitate towards a few technological survivors . Others will fall by the wayside only to have gratified their creators that they were part of this revolutionary development. 

But in all this there remains the problem of interoperability.  It is critical that we start planning on how to make sure that there is a common standard so that all the small interconnected worlds  can communicate with each other without sacrificing their innovational privacies.

I do realize that there are many IoT standards in the fray competing to become the de-facto standard. But if history of standards is any indication this is going to be an uphill task.

As a developer this is very worrisome. The so called connected world is destined to becoming the fragmented world. Very soon and very quickly.

Worth reading is a post by David Evans in Tech Crunch titled “We Need To Get The Internet Of Things Right