Microsoft Research has been experimenting with integrating safe manual memory management with garbage collection (GC) in the .Net runtime. The goal is to give developers both the convenience and safety of automated memory management and the opportunity to improve performance by freeing objects from memory manually.
The effort, called Project Snowflake, is the subject of a paper published this week by Microsoft Research and co-authors from the University of Cambridge and Princeton University. With Snowflake, programmers could choose between allocating objects in the GC heap or the manual heap. Snowflake combines the open source .Net runtime with a facility to manage memory manually without compromising performance or safety. Existing applications run unmodified using the GC heap, with no performance degradation.
“Experimental results from our .Net CoreCLR implementation on real-world applications show substantial performance gains especially in multi-threaded scenarios: up to 3x savings in peak working sets and 2x improvements in run time,” the researchers said.
Microsoft’s model for manual memory management builds on the notion of unique owners of manual objects, with locations in the stack or heap holding only the reference to an object allocated on the manual heap. The concept of shields is introduced to enable safe concurrent sharing of manual objects. The shield creates state in local thread storage to prevent de-allocation while the object is being used.
While garbage collectors such as the .Net GC offer high throughput through fast thread-local bump allocation and collection of young objects, studies show that GC can introduce performance overhead compared to manual memory management, the researchers said. These overheads are amplified in big data analytics and real-time stream processing applications, partly due to the need to trace through large heaps, they explained.
“This trend is likely to continue as modern servers make use of ever larger memories – sizes of hundreds of gigabytes, or even terabytes, are already common,” the researchers note. Manual memory management addresses the problem by avoiding the tracing of the object graph to free objects and allows programmers to use their own knowledge of object lifetimes to free the objects at specific locations in a program. This improves both throughput and memory usage, according to the researchers. But manual memory management is “typically unsafe” and can lead to crashes and vulnerabilities. It also cancels out the productivity benefits of GC.
Project Snowflake modifies the CoreCLR runtime in Microsoft’s open source .Net implementation, known as .Net Core, and extends the standard libraries with APIs that use manual memory. A flexible programming model is leveraged for manual memory management that allows objects to be allocated and deallocated at arbitrary program locations, and shared concurrently and safely among multiple threads.
Microsoft’s design puts no overhead on garbage collections or operations such as write barriers. Programmers who want to optimize applications must incrementally change code to allocate objects in the manual heap and explicitly deallocate them. Manually managed objects are guaranteed full type- and temporal- safety, including in the presence of concurrent accesses. Developers get dynamic managed exceptions for use-after-free situations but no crashes or security vulnerabilities, according to the report.