Main BLOGGER
Google
WWW THIS BLOG
Sunday, March 06, 2005
 
Art of Memory Optimization
Type Sizing
Example 1.
struct BadValueType { char c1; int i; char c2;}
struct GoodValueType { int i; char c1; char c2;}
struct GoodValueType2{ short i; char c1; char c2;}

Properly aligning and sizing this type reduced it from 12,8 to 6 bytes.

Example 2.
Suppose we have a field "state" with type "string" in the Address type, if we ignore the various U.S. territories, then the state field has 50 possible values. It might be worth considering an enumeration here since it would remove the need for a reference type and store the value directly in the class. The base type for the enumeration could be a byte rather than the default int, resulting the field requiring 1 byte rather than 4.

This situation brings to light one of the more common trade-offs in computing: speed versus memory. It is often possible to optimize memory usage at the expense of some CPU cycles, and vice versa.

Object Pooling
This is especially useful when an instance of an object is repeatedly used, or if the object has an expensive initialization aspect to its construction such that it's better to reuse an existing instance than to dispose of an existing one and to create a completely new one from scratch.

we can achieve this by overloading new() operator.

more on:
http://msdn.microsoft.com/msdnmag/issues/05/01/MemoryOptimization/default.aspx



<< Home

Powered by Blogger

Google
WWW THIS BLOG