WPF依赖属性设计如何节省内存消耗?
我在以下链接中阅读了此内容:-
http://www. informit.com/articles/article.aspx?p=688529&seqNum=2
但是,由于 GetValue 和 SetValue 在内部使用高效的稀疏存储系统,并且 IsDefaultProperty 是静态字段(而不是实例字段),因此与典型的 .NET 属性相比,依赖项属性实现可以节省每个实例的内存。如果 WPF 控件上的所有属性都是实例字段的包装器(就像大多数 .NET 属性一样),那么它们将消耗大量内存,因为所有本地数据都附加到每个实例。
但最终它们被存储在某个地方,它如何节省内存消耗?
I read this in the following link:-
http://www.informit.com/articles/article.aspx?p=688529&seqNum=2
However, because GetValue and SetValue internally use an efficient sparse storage system and because IsDefaultProperty is a static field (rather than an instance field), the dependency property implementation saves per-instance memory compared to a typical .NET property. If all the properties on WPF controls were wrappers around instance fields (as most .NET properties are), they would consume a significant amount of memory because of all the local data attached to each instance.
But eventually they are getting stored somewhere, how does it saves memory consumption ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅以下链接:依赖属性。
只要依赖属性使用其默认状态(这很常见),它就不会占用任何额外的内存,因为将使用默认值。默认值不是按实例存储的,而是按依赖项属性存储的,并由元数据设置。
示例,注意如何将
Brushes.Black
设置为默认值这样想:假设您在 Xaml 中有四个
TextBlocks
这三个 <顶部的 code>TextBlocks 将
Foreground
设置为 黑色,尽管您从未明确将其设置为黑色。他们正在使用默认值。因此,对于上面三个TextBlocks
的Foreground
属性,您只需要一个字段(因为它是静态字段)。不过,对于第四个
TextBlock
,您已将Foreground
显式设置为绿色,以便该值作为Foreground
的本地值插入到字典中这个实例因此需要额外的内存(而且,它最终会出现在下面列表中的第 3 个位置,覆盖Setters
、Triggers
等) 。另外,请参阅 Josh Smith 的以下帖子,这是一本很好的读物: 揭秘依赖属性
编辑: 回答以下评论Duane
如果您显式将该值设置为与默认值相同的值,它仍然会存储为本地值。这可以使用以下 Xaml 轻松验证。
两个
TextBlocks
都将Foreground
设置为 Black,但后者设置了本地值。Style
只能在第一个TextBlock
上设置Foreground
,而不能在后面设置Foreground
,因为样式设置器的优先级低于本地值。See the following link: Dependency Properties.
As long as a Dependency Property uses its default state (which is very common), it won't take up any additional memory since the default value will be used. The default value isn't stored per instance, it is stored per Dependency Property and it's set by metadata.
Example, notice how
Brushes.Black
is set as the default valueThink of it this way: Say you have four
TextBlocks
in XamlThe three
TextBlocks
at the top haveForeground
set to Black although you have never explicitly set it to Black. They are using their default value. So for theForeground
property for the threeTextBlocks
above, you only need one field (since it is a static field).For the forth
TextBlock
though, you have explicitly setForeground
to Green, so that value is inserted into a dictionary as the local value forForeground
on this instance and thus requires additional memory (also, it will end up at place number 3 in the list below, overridingSetters
,Triggers
etc).Also, see the following post by Josh Smith, it's a good read: Demystifying dependency properties
Edit: To answer the comment from Duane
If you explicitly set the value to the same value as the default value, it will still get stored as the local value. This can easily be verified with the following Xaml.
Both
TextBlocks
will haveForeground
set to Black, but the later has a local value set. TheStyle
will only be able to setForeground
on the firstTextBlock
and not the later since Style setters has lower priority than local value.首先,假设您创建一个具有十几个属性的类。创建 100,000 个。您现在有多少个对象引用? 1,200,000。
现在实现一个名为
DependencyObject
的类:构建一个派生类,如下所示:
现在创建 100,000 个
MyDependencyObject
实例并设置它们的Parent
。使用了多少个对象引用(不包括父对象引用)? 300,000。这就是属性值继承在依赖对象中的工作原理。
First, suppose you create a class with a dozen properties. Create 100,000 of them. How many object references do you now have? 1,200,000.
Now implement a class called
DependencyObject
:Build a derived class like this:
Now create 100,000 instances of
MyDependencyObject
and set theirParent
. How many object references are used (not counting the parent)? 300,000.That's how property value inheritance works in dependency objects.