The following programming practices can save memory and improve Device ApplicationsProgramPerformance.
Saves memory using Windows Forms and graphics
-
ProvideBeginupdateAndEndupdateMethod controls use these two methods. The controls that provide these two methods include: , , , And .
-
Use And Method.
-
Load other forms in the background, use data to fill the control, and then use Method.
-
Process eventsCodeIt is restricted to executing only important tasks so that the suspension process can continue.
Use the off-screen bitmap. For examples, see How to: Draw background images.
-
Override , And Instead of adding a key event handler.
Use Data and strings to save memory
-
InForUse the integer variable ( Or ), Instead of using object variables.
-
Avoid Enumeration Method, because it will search for metadata tables, thus affecting performance.
-
Avoid Error. If you do not have enough memory for internal use or for new object instances, the Common Language Runtime Library may cause this exception. To avoid this exception, avoid writing large methods that occupy 64 K or more memory.
Remove system. sr. dll, which contains the error message string in the exception dialog box. You can use this file when deploying an application to save memory. If yesSystem. sr. dll. NET Compact framework dynamically loads the error string contained in system. sr. dll.
If the. dll file does not exist on the device, all exceptions contain the message "unable to load resource assembly ". However, adding the system. sr. dll reference to the Microsoft Visual Studio 2005 project during development is always useful, as this way you will see a meaningful exception.
-
The string is unchangeable. Therefore, each time you modify the string, a new Object. Use .
-
If you know that The exact serialization format.DatetimeUse Method. Otherwise,DatetimeThe analyzer will try to apply multiple regional-specific formats in sequence.
-
Restrict open Number of objects, and release them after completion.
Saves memory when interacting with local code
In the Platform call operation, you can directly copy the data type to the local structure. This type has a common representation in the managed and unmanaged memory, suchInt32Or . For value types that can be directly copied to the local structure with more than 32 bits, passing by reference is faster than passing by value. For more information about types that can be directly copied to the local structure, see types that can be directly copied to the local structure in. NET Compact framework.
-
Used for parameters in the function Signature And Attribute can reduce unnecessary sending and processing.
Use ManuallyIntptrAnd between the managed object, such , , And .
-
Use And Method.
If you expect your local COM object to normally return s_false, or other non-s_ OK hresult values Set the fieldTrueAnd match the managed signature with the local signature. This avoids the need for the runtime to convert the hresult value to an exception on the com call.Try/CatchSystem overhead of a block.
-
Use one platform call as much as possible, and use as few calls as possible.
Saves memory in the collection
If the set is based on arrays, use the indexer.
Once you have the opportunity, specify the size of the set, because dynamic resizing can greatly increase the additional storage.
Use a generic set to avoid system overhead caused by packing and unboxing value types. Define your own optimization set to achieve optimal performance.
Saving memory in XML
-
Use And Replace (Because it occupies a large amount of memory ).
Specify And To improve performance. If And Two attribute values apply, which can significantly improve performance.
-
Uses UTF-8, ASCII, and UTF-16 character encoding, which is faster than ANSI and Windows code page encoding.
-
Avoid using an architecture for analysis because it requires more verification work.
-
When filling in data from an XML Source Columns should be mapped to properties and typedDataset.
-
FillDatasetAvoid filling in the following items:
When XML deserialization is used, the following guidelines can be followed to improve performance:
Keep element and attribute names as short as possible because each character must be verified.
XML based on attribute data is faster than XML based on element data.
If applicable, use Method.
If the performance is very important, binary serialization should be considered.
-
Each type serialized for XML uses one Instance to reduce the time required to search for metadata.
-
Since serialization of a large number of XML files may exhaust the memory, you should consider using And Generate a custom binary serialization mechanism.
Saves memory when using Web Services
Read and WriteDatasetDiffGram should be used. For more information, see .
In XML formatDatasetAnd its architecture are saved to the device.
Create a simple web service method call during the initial screen, because the first call is slower than the subsequent call.
Handle network and data errors with caution.
In some cases, you can manuallyDatasetSerialize to an XML string and then execute a web service call to improve performance.
Saves memory in Advanced Programming
-
asynchronous processing of large operations.
-
avoid virtual calls .. NET Compact framework virtual invocation is about 30% slower than static or instance invocation. Due to resource constraints,. NET Compact framework does not use vtable. Therefore, methods must be called through conversion classes and interface hierarchies, but this is a very costly operation .. NET Compact framework maintains the cache of resolved virtual calls. Therefore, in most cases, you do not need to re-interpret the call.
-
use fields instead of attributes as much as possible.
-
when defining value types, rewrite and method. If they are not overwritten, the runtime will common versions of the methods used in the class.
-
Use ing with caution. Using ing with a non-instantiated class will affect the performance of the instantiated object in the application.
-
make sure that the managed resources in the resx file have a fully qualified name, and these resources are correct. They must have the correct version and publickeytoken field. Finding the most appropriate alternative type for an unspecified type affects performance.
-
note that in some cases, it is sufficient to directly read application data from a file. higher efficiency. ResourceManager detects multiple locations in the file system before locating your binary resources to find the most matched ancillary assembly. Use appropriate tools for jobs.
Source: http://msdn2.microsoft.com/zh-cn/library/1766918e (vs.80). aspx