Memory Leaks leading to undesired memory growth and exhaustion are most common production problems in enterprise Node applications. Closures, a powerful semantic feature in functional programs, used pervasively in Node programs are prominent sources of unanticipated leaks.
I will explain three most common use cases where Closures are used in Node. For each, a) sample code, b) the life-span of the Closure context and which variables the closure keeps live, iii) what memory is retained during the Closure life-span. The intention is to help audience understand Closure's execution environment, their implications to memory footprint, and help them develop efficient code.
Many recent problem determination exercises revealed that memory leaks are the most prevalent production issues, and debugging of them reveals that Closures play a major role in it. Disambiguating Closures and programming best practices around it, I believe the Node community will be able to apply it instantly in their day-to-day job. People with strong procedural and object oriented language background will find this information useful, in making the key differentiation between Closures and normal functions.
Outline/Structure of the Experience Report
An introduction to Closures, how they support the Node.js applications in its asynchronous and event driven programming model, what subtle difference it brings to the perception of live variables and their life span in the context of functions and blocks. The three use cases through which Closure is introduced are: i) Completion handlers where named or anonymous function is passed as a argument to an asynchronous routine, ii) Intermediary functions where an inner function is created in the lexical scope of an outer function, and is returned to the caller for future invocation, iii) Listener functions which gets invoked asynchronously upon certain events occurring in the program. The presentation shows how the memory is retained in each cases, and what are the best practices to follow in each case. It will also use sample code and snapshots taken from heapdump and analysed in Google Chrome developer tools to visually aid the concepts in more impactful manner
Post-mortem investigation of production issues is important for businesses running production Node.js applications. It is also important that all developers are aware of the needs/issues as the choices they make during development will affect how effectively production issues can be investigated. This talk will increase the awareness of this important aspect of Node.js applications allowing developers to make choices that enable debugging of production applications. In turn this will result in Node .js increasingly been seen as a platform that is good for production use.