How to: Improve Performance
The following programming practices can save memory and improve the performance for device applications.
To save memory with Windows Forms and graphics
Load other forms in the background and populate controls with data before using the Show() method.
Limit event handling code to perform only essential tasks, so that pending processes can continue.
Before disposing of a subscriber object, unsubscribe to its events by using the subtraction assignment operator (-=). For more information, see How to: Subscribe to and Unsubscribe from Events (C# Programming Guide). Problems that result from a failure to unsubscribe correctly can resemble a memory leak.
Use an off-screen bitmap. For an example, see How to: Draw Images Off-Screen.
To save memory with data and strings
Avoid using the ToString method of an enumeration because of the performance impact it incurs by searching metadata tables.
Avoid OutOfMemoryException errors. This exception can be thrown by the common language runtime when there is not enough memory to allocate for internal purposes or new object instances. To avoid this exception, avoid programming large methods that consume 64 or more kilobytes of memory.
Remove System.SR.dll, which contains error message strings for exception dialog boxes. You can deploy your application without this file to save memory. The .NET Compact Framework dynamically loads error strings contained in System.SR.dll if the file is present.
If this .dll file is not present on the device, all exceptions contain the message "Cannot load resource assembly." During development, however, it is useful to add a reference to System.SR.dll to your Visual Studio project so you will see meaningful exceptions.
Limit the number of open SqlCeCommand objects and dispose of them when finished.
To save memory when interoperating with native code
In platform invoke operations, use blittable types, which have a common representation in both managed and unmanaged memory, such as Int32 or IntPtr. Blittable value types that are larger than 32 bits are more quickly passed by reference than by value. For more information about blittable types, see .NET Compact Framework Blittable Types.
If you expect your native COM object to return S_FALSE as a common case, or other non S_OK HRESULT values, set the PreserveSig field to true and make the managed signature match the native signature. This avoids the overhead of a try/catch block that is necessary when the runtime translates HRESULT values to exceptions on your COM calls.
Do as much work as you can in one platform invoke call rather than using multiple calls.
To save memory in collections
Use indexers if the collection is based on an array.
Whenever possible, specify the size of your collection, because dynamic resizing can greatly increase excess storage.
Use generic collections to avoid the boxing and unboxing overhead for value types. Defining your own optimized collection results in the best performance.
To save memory in XML
Use UTF-8, ASCII, and UTF-16 character encodings, which are faster than ANSI and Windows codepage encodings.
Avoid using a schema for parsing, because it requires additional validation work.
Avoid the following when populating a DataSet:
The following guidelines improve performance when using XML deserialization:
Keep element and attribute names as short as possible because every character must be validated.
XML based on attribute data is faster than XML based on element data.
Use the XmlNodeReader.Skip method when applicable.
Consider binary serialization when performance becomes critical.
Use one XmlSerializer instance per type for XML serialization to reduce the amount of time spent searching for metadata.
To save memory when using a Web service
Save a remote DataSet and its schema to the device as XML.
Make a simple Web service method call during your splash screen because the first call is slower than subsequent calls.
Be careful to handle network and data errors.
In some cases, manually serializing the DataSet as an XML string before making a Web service call results in better performance.
To save memory in advanced programming
Process large operations asynchronously.
Avoid virtual calls. The .NET Compact Framework runtime virtual calls are approximately 30 percent slower than static or instance calls. The .NET Compact Framework does not use vtables because of constrained resources, so methods must be called by traversing the class and interface hierarchy, which is an expensive operation. The .NET Compact Framework maintains a cache of resolved virtual calls, so in most cases calls do not need to be reinterpreted.
Use fields instead of properties where possible.
Use reflection with caution. Using reflection for investigative purposes with un-instantiated classes can impact the performance of the instantiated objects in your application.
Ensure that your managed resources have fully qualified type names and are accurate in your RESX file. They must have the correct version and the PublicKeyToken fields. The effort to find the most appropriate substitute for an improperly specified type impacts performance.
Note that in some cases reading application data directly from the file may be sufficient and more efficient than using ResourceManager. A ResourceManager may probe multiple locations in the file system to find a best matching satellite assembly before it locates your resource binary. Use appropriate tools for the job.