Dependency Properties - II Contd.(Memory Footprint)
Tuesday, June 10, 2014 Category : Dependency Property, WPF, XAML 0
To demonstrate the memory footprint of dependency properties vs CLR properties, as described in my last post, I profiled the sample application's memory usage in the following 4 cases:
Dependency properties perform worse than CLR properties in terms of memory when most of the times, the value of the property will be over-ridden from the default value. Obviously, you would still have to use dependency properties in case you want your value to be dynamic-bound in the XAML of the calling page, since CLR properties do not support that at all. More information on dynamic binding and dependency properties can be found here.
- Dependency property with values overridden a lot
- Dependency property left with default value most of the time
- CLR property with values overridden a lot
- CLR property left with default value most of the time
Dependency property with values overridden a lot
For this case, I created a dependency property for Quantity in last post's user control. I then used this control inside the datagrid on the main page as a column, so this control repeats for as many items I have in the grid. Next, I bound the Quantity field in XAML of the main page to a value different from the default value. Then, I profiled the application and got the following:
CLR property with values overridden a lot
Next, I converted the dependency property in the last step to a CLR property and profiled again:
If you compare the above figures, you see the following:
| All instances and allocations (live instances) | All instances and allocations (live bytes - total) | |
|---|---|---|
| Dependency Property | 118,493 | 6,989,272 |
| CLR Property | 118,492 | 6,981,018 |
Conclusion: Over-riding Defaults with Dependency Properties
There are some other figures that can be compared as well but from the above, it is obvious that:Dependency properties perform worse than CLR properties in terms of memory when most of the times, the value of the property will be over-ridden from the default value. Obviously, you would still have to use dependency properties in case you want your value to be dynamic-bound in the XAML of the calling page, since CLR properties do not support that at all. More information on dynamic binding and dependency properties can be found here.
Dependency property left with default value most of the time
Again, in the base sample, I used dependency property for Quantity but this time, I did not change the value of this property in the calling page, but left it at default. I profiled the application and this time, I got the following results:CLR property left with default value most of the time
Next, I changed the dependency property to a CLR property, still leaving it at default value in the calling page:
If you compare the figures this time, you see the following:
| All instances and allocations (live instances) | All instances and allocations (live bytes - total) | |
|---|---|---|
| Dependency Property | 118,325 | 6,970,616 |
| CLR Property | 118,330 | 6,980,248 |
Other figures also seem to be lower for dependency property as compared to CLR property.
Conclusion: Over-riding Defaults with Dependency Properties
Dependency properties reduce the memory footprint of the application but only if the usage of this dependency property is such that each usage does not specify a different value.
If most of the controls on the page use the default value of the dependency property, the memory usage will be lower than CLR property. However, if most of the controls on the page explicitly set a different value of the dependency
property, the memory usage will be higher than CLR property