Wrappers for raw data located in unmanaged memory.

Using the Data type will only place a small object in managed memory, keeping the actual data in unmanaged memory. A proxy class (DataWrapper) is used to safely allow multiple references to the same block of unmanaged memory. When the DataWrapper object is destroyed (either manually or by the garbage collector when there are no remaining Data references), the unmanaged memory is deallocated.

This has the following advantage over using managed memory:

  • Faster allocation and deallocation, since memory is requested from the OS directly as whole pages
  • Greatly reduced chance of memory leaks (on 32-bit platforms) due to stray pointers
  • Overall improved GC performance due to reduced size of managed heap
  • Memory is immediately returned to the OS when data is deallocated

On the other hand, using Data has the following disadvantages:

  • This module is designed to store raw data which does not have any pointers. Storing objects containing pointers to managed memory is unsupported, and may result in memory corruption.
  • Small objects may be stored inefficiently, as the module requests entire pages of memory from the OS. Considering allocating one large object and use slices (Data instances) for individual objects.



struct Data

Wrapper for data located in external memory, to prevent faux references. Represents a slice of data, which may or may not be in unmanaged memory. Data in unmanaged memory is bound to a DataWrapper class instance.



Vladimir Panteleev <>


This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at