views:

1633

answers:

1

How can i manage program memory in C# code ? (in WinCE)

+1  A: 

Are you asking how you change the system memory division under Windows CE? P/Invoking SetSystemMemoryDivision would be the way to go for that (that's what the Smart Device Framework's MemoryManagement class does, and an example of the raw code declaration can be found over at pinvoke.net), but there are caveats to how it's going to work for you. For example, Windows Mobile (and older Pocket PCs) has its own internal memory manager for handling program and object store allocation, and up through at least WinMo 5.0, it would just re-adjust what you set, so your setting would be irrelevant.

Also, setting the division may or may not be permanent. It typically gets stored in the registry as an override, but you have to persist the registry and that assumes that the device supports persisting the registry and that it doesn't override your setting itself.

Here's the code we use for storage memory in the SDF. Program memory is a little more complex because you have to determine how much total there is and subtract from that.

    /// <summary>
    /// Gets or sets the amount of allocated memory for Storage,
    /// in kilobytes
    /// </summary>
    public static int SystemStorageMemory
    {
        get
        {
            int storePages = 0;
            int ramPages = 0;
            int pageSize = 0;

            NativeMethods.GetSystemMemoryDivision(ref storePages, 
                                                  ref ramPages, 
                                                  ref pageSize);

            return (storePages * (pageSize  >> 10));
        }
        set
        {
            NativeMethods.SetSystemMemoryDivision(
                   (value << 10) / m_memoryPageSize);
        }
    }

The NativeMethods class contains these:

    [DllImport("coredll.dll", SetLastError = true)]
    public static extern bool GetSystemMemoryDivision(
                                   ref int lpdwStorePages, 
                                   ref int lpdwRamPages, 
                                   ref int lpdwPageSize);

    [DllImport("coredll.dll", SetLastError = true)]
    public static extern bool SetSystemMemoryDivision(
                                   int dwStorePages);
ctacke