Thursday, January 15, 2009

[Silverlight 2, C#] Testing Performance, Memory Usage

So after some code reviews and the like, I had decided to try and implement some of my core functionality a little different in order to *hopefully* decrease the memory allocation of my application.

Since Silverlight 2 does not appear to support the System.Diagnostics.Process class, I believe the only way to get similar information is via [Garbage Collector] System.GC.GetTotalMemory

System.GC.GetTotalMemory
public static long GetTotalMemory(
bool forceFullCollection
)

* Set forceFullCollection to true if you can wait for garbage collection to occur before doing the check.

* Returns the estimated number of bytes the application has allocated.

Difference Between GetTotalMemory() vs Windows Task Manager Memory Estimation
I have noticed QUITE a difference between the bytes returned from this function, vs. the Windows Task Manager estimation of the memory used by iexplorer while my app is running.

Siverlight Plugin Memory
One reason that I know for sure, is that the Task Manager takes into account the memory it takes to run the Silverlight plugin. I created an empty Silverlight project and when running it, it appeared to take up between 38,000kb to 40,624kb. Not sure why there was ever a difference, but every time I ran it I got a slightly different value. GetTotalMemory does not appear to include this cost and only counts the memory the app itself allocates.

Unmanaged Memory
GetTotalMemory only counts managed memory allocated by your application. If there is a memory leak for whatever reason, then that cost will not be included in the total.

There may be other reasons that the two memory estimations are different, but those are the two that I have come across so far.

.

1 comment:

Don said...

Not sure if this would help... not exactly related to the retrieval of total mem usage.. but some pretty cool ideas - helped speed up my app considerably:

http://msdn.microsoft.com/en-us/library/cc189071(VS.95).aspx

and

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2008/07/17/silverlight-application-performance-refresh-rate-amp-cpu-usage.aspx