TNS
VOXPOP
Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
0%
At work, but not for production apps
0%
I don’t use WebAssembly but expect to when the technology matures
0%
I have no plans to use WebAssembly
0%
No plans and I get mad whenever I see the buzzword
0%
Operations / Software Development / WebAssembly

Time to Get the Garbage out of WebAssembly

WebAssembly modules encompass different programming languages implemented inside them, so the ways garbage collection is used for these languages vary.
Jan 24th, 2024 8:03am by
Featued image for: 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.

This means that a single deployment of an application in a WebAssembly module should theoretically be able to run on and be updated across a multitude of different devices, including servers, edge devices, multiclouds, serverless environments and more. Wherever there is a CPU capable of running instruction sets, WebAssembly is designed to run applications written in an increasing number of languages that it can host in a module. It can now accommodate Python, JavaScript, C++, Rust and others.

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.

High-level programming languages, such as Rust and C++, require manual memory management or manual garbage collection. On the other hand, languages like Java, Python and JavaScript generally rely on automatic garbage collection or need to be accompanied by it. In this way, “Wasm has had great support for languages like C, C++ and Rust from the very beginning, Daniel Lopez Ridruejo, founder and former CEO of Bitnami (now part of VMware), told The New Stack.

“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.“

Indeed, since most popular programming languages will be compiled to Wasm, they will also require garbage collection, Ivan Mikushin, principle engineer at Broadcom, said during his talk “Wasm GC: What Exactly Is It (and Why Should I Care) during WasmCon in September. “This poses a small problem that most languages people use — I mean, we all use other languages — but most developers out there use Java, JavaScript, Python, Go and many more languages that all require garbage collection,” Mikushin said.

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.

WasmGC thus introduces structured heap types and arrays, providing support for non-linear memory allocation, Steiner writes. Each WasmGC object possesses a fixed type and structure and this simplifies the generation of efficient code by virtual machines (VMs) to access their fields without the risk of deoptimizations encountered by dynamic languages like JavaScript, Steiner writes. “This proposal thereby adds efficient support for high-level managed languages to WebAssembly, via struct and array heap types that enable language compilers targeting Wasm to integrate with a garbage collector in the host VM,” Steiner writes.

“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.”

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.