{"id":7896,"date":"2023-08-02T07:33:22","date_gmt":"2023-08-02T06:33:22","guid":{"rendered":"https:\/\/www.scichart.com\/?post_type=blog_scichart&p=7896"},"modified":"2023-12-19T15:45:26","modified_gmt":"2023-12-19T15:45:26","slug":"debugging-javascript-webassembly-memory-leaks","status":"publish","type":"blog_scichart","link":"https:\/\/www.scichart.com\/blog\/debugging-javascript-webassembly-memory-leaks\/","title":{"rendered":"Debugging JavaScript and WebAssembly Memory Leaks using the SciChart.js debug tools"},"content":{"rendered":"

One of the decisions we took when building SciChart.js \u2013 our high-performance JavaScript chart library<\/strong><\/a> \u2013 was to pioneer the use of WebAssembly<\/a> with JavaScript. This allowed us to reuse our C++ code base targeting OpenGL and WebGL which had already been well tested on our windows and mobile versions of SciChart, and bring our award-winning technology into the browser.<\/p>\n

The main benefits of our unique tech stack are not only code-reuse and efficiency, but also enabling extremely performant charts & data visualisation for our users. SciChart.js is the fastest JavaScript chart library<\/strong><\/a> available for plotting big data, and in some cases outperforms traditional HTML5 Canvas or SVG charts by over 100x. SciChart enables a whole host of applications in JavaScript: such as embedded systems, scientific applications and medical applications, that were previously impossible. Having a faster chart component also means more it’s efficient: the library can be run on devices with lower power processors, meaning a cheaper cost of production for embedded systems.<\/p>\n

The technology of WebAssembly<\/a> with JavaScript is fantastic, but it doesn’t come without its challenges which we will go into below, and then talk about our solutions.<\/p>\n

JavaScript vs. WebAssembly Memory Model<\/h2>\n

WebAssembly has a completely different memory model to JavaScript. While JavaScript has a garbage collector, which automatically cleans up the memory of variables which are no longer required, WebAssembly simply does not. An object or buffer declared in Wasm memory must be deleted by the caller, if not a memory leak will occur. Memory leaks can occur in both JavaScript and WebAssembly, and care and attention must be taken by the developer to ensure that memory is correctly cleaned up when using SciChart.js.<\/p>\n

How Memory Leaks are caused in JavaScript<\/h3>\n

Despite being a Garbage-Collected managed programming language, it’s still extremely easy to create a memory leak just in vanilla JavaScript. Here are a couple of ways that is possible to inadvertently leak memory in a JavaScript app:<\/p>\n