Time to Get the Garbage out of WebAssembly
Garbage collection has become a standard feature for WebAssembly in its development, showing progress on many fronts. But what is garbage collection? Strictly defined according to the Wikipedia definition, it involves managing memory by removing redundant or excess memory, such as addressing memory leaks.
Wasm’s connection to garbage collection has much to do with how WebAssembly is designed to deploy applications written in the language of the developer’s choice, enabling simultaneous deployment in various and disparate environments. “Disparate” here means that WebAssembly runs on a CPU and only requires a device, server, etc., capable of running a CPU instruction set.
Different applications written in diverse programming languages should be able to coexist within a single module, although this capability is still largely under development. Meanwhile, given that WebAssembly modules encompass different programming languages implemented inside them, the ways garbage collection is used for these languages vary.
“The widespread availability of garbage collection support in popular Wasm runtimes will accelerate the drive to bring languages that require this functionality such as Java and Kotlin to be supported as first-class citizens. It will also further the goal of seamless interoperability between modules written in different languages,” Ridruejo said. “I am really excited about Garbage Collection support. Together with other proposals, such as Threading, will help bring many popular languages and their rich ecosystems to WebAssembly, whether it is running in the browser or the server side.“
Work in Progress
At this time, the so-called “WasmGC” for garbage collection exists as a proposal of the WebAssembly Community Group to integrate garbage collection functionality for WebAssembly. The development of garbage collection for WebAssembly is par for the course given its design, and especially, its backward compatibility.
This means there are new standards that can be incrementally added into WebAssembly, “which is key if these things are running in browsers for a long time or in hosted services,” Tyler Rockwood, a senior software engineer at Redpanda Data, said during the Linux Foundation live webinar “How Wasm Simplifies Streaming Data Pipelines.” “WebAssembly has been built to be modular and backward compatible, So all these new features that have been added to it are sort of like added on to the side,” Rockwood said. “What support is there is evolving — very rapidly even.”
However, while WasmGC is still relatively limited compared to its potential, advances have been made. WasmGC is now enabled by default in Chrome, for example, which Thomas Steiner, a developer relations engineer at Google, recently communicated in a blog post. Steiner points out the current Wasm MVP implementation is limited to handling “only numbers,” specifically integers and floats, in linear memory. With the ongoing implementation of the reference types proposal, Wasm is able to retain external references.
“In simplified terms, this means that with WasmGC, porting a programming language to Wasm means the programming language’s garbage collector no longer needs to be part of the port, but instead the existing garbage collector can be used.”
Comparative Latency Factors
Some Wasm applications are used in networking to improve latency. Runtime performance is important, of course, but it is the latency performance that counts in this case, Sehyo Chang, chief technology officer at InfinyOn, said. This is because, Chang said, latency plays a crucial role in determining the overall user experience in any application. A slow response time can greatly impact user engagement and lead to dissatisfaction, potentially resulting in lost sales opportunities, Chang said.
This is where both Wasm and garbage collection come into play: Wasm can be used to replace Kafka for lower latency data streaming. Streaming technology based on Java, like Kafka, can experience high latency due to garbage collection and Java Virtual Machines, Chang said. However, using WebAssembly technology allows for stream processing without these penalties, resulting in a significant reduction of latency while also providing more flexibility and security, Chang said.
“WASM gives much flexibility on choosing how data streaming is implemented. WASM logic implemented in Rust does not need garbage collection which will reduce latency and resource consumption,” Chang told The New Stack. “Other Wasm applications can be written in Python or with an alternative garbage-collected language for non-latency critical applications.”