Large Memory Objects & Bitmaps

Last modified August 20, 2014

Required

Tutorials:  First NETMF Project

Introduction

Managed systems like NETMF requires a complex memory management. To limit the overhead on small systems, NETMF heap only supports allocating objects up to about 700KBs. Larger buffers can be allocated using a separate heap called the custom heap. With custom heap, you can create very large buffers and very large bitmaps. Internally, these objects are not stored in the standard managed heap but they are in the custom heap, a seperate memeory region.

Tip

The following code requires the Microsoft.SPOT.Hardware assembly.

Management of Custom Heap

Unlike the regular heap which is fully managed, the custom heap is also managed but a few important points need to be considered. In order for objects in custom heap to be cleared automatically, you must fulfill three requirements: The object reference must be lost. The garbage collector must run, you probably need to force it. The system must come idle in order for finalize to run and “Dispose” the object. We will not go into details on garbage collectors, dispose, or finalizer. What you need to know is that large objects that are on custom heap are not easily cleared from the system so you must always dispose the object when you are done using it.

Creating, referencing, and Disposing Custom Heap objects:

using System;
using Microsoft.SPOT.Hardware;
class Program
{

    public static void Main()
    {
        Byte value;
        // allocate a 1MB buffer
        LargeBuffer lb = new LargeBuffer(1024 * 1024);
        // use the buffer
        lb.Bytes[5] = 123;
        // ....
        value = lb.Bytes[5];

        // when done, dispose the object to empty the memory
        lb.Dispose();
    }
}

The better option is to use the “using” statement. This statement will automatically call Dispose once the execution is leaving the “using” curly brackets.

using System;
using Microsoft.SPOT.Hardware;
class Program
{
    public static void Main()
    {
        // allocate a 1MB buffer
        using (LargeBuffer lb = new LargeBuffer(1024 * 1024))
        {
            // use the buffer
            lb.Bytes[5] = 123;
            // ...        
        }
        // Dispose was called automatically
        // ...
    }
}

Large Bitmaps

Bitmaps larger than 192,000 pixels are automatically allocated on the custom heap. For example, a 800x480 bitmap will need 800x480x4 bytes, that is about 1.5MB. This large bitmap is allocated on the custom heap and so it should always be disposed when it is no longer needed, best by utilize the “using” statement. 

The example uses two classes in the namespace "Microsoft.SPOT.Presentation.Media" (Colors and Bitmap). Interestingly enough, the namespace is in two different assemblies; both are required, they are Microsoft.SPOT.Graphics and Microsoft.SPOT.TinyCore.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Media;
class Program
{
    public static void Main()
    {
        using (Bitmap largeBitmap = new Bitmap(800, 480))
        {
            // assumign we have a display that is 800x480
            // draw a circle
            largeBitmap.DrawEllipse(Colors.Green, 100, 100, 10, 10);
            // draw other things
            // ...
            largeBitmap.Flush();//flush the Bitmap object to the display
        }
        // once we are here largeBitmap Dispose was automatically called
        // ...
    }
}
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.

* Indicates required fields.
This form is only for feedback not support.
Review our how to find information guide on locating helpful resources.