Boxing Memory concerns [closed]
开发者_如何转开发
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this questionprivate double _value;
public object Value
{
get
{
return _value;
}
}
I am doing code reviews and noticed this in our codebase. It is not a typo, but my concerns are with the boxing. That is involved when the getter is called, and given that this code is in a type, that is generated hundreds of times and the getter is called frequently I can see an issue with memory.
Are my concerns correct with memory? I mean are we doubling up due to the boxing?
Yes, the code snippet that you've shown will indeed cause boxing. You're forcing the run-time to convert a double
into an object
. If you want to prove it to yourself or a colleague, check the compiled IL for the tell-tale box
and unbox
instructions.
However, while you're correct in looking out to avoid boxing wherever possible, the actual performance penalty is not always as significant as the hype makes it out to be. Before making breaking changes to your codebase, invest some time profiling to make sure that the code you're spending your time on is really a performance bottleneck.
It's not particularly clear given the specific example above why you need to return type object
in the first place. Since you're just returning the value of a private field, you could simply change the property to return type double
, instead.
Alternatively, you could convert the property to a generic method. (Properties can't be generic, but methods can, and if you're doing computationally-intense work inside the getter, it probably should be a method anyway.) Generics alleviate the problem of boxing, but still allow you an immense degree of flexibility in what type is returned (similar to returning type object
).
Yes, it's going to be creating a new object every time you call the getter.
Yes, this will take memory.
Is it a problem though? Only you can say. It depends how often it happens, and whether there's any better alternative. Is there some reason why it needs to be declared to return object
instead of double
? Is it going to be assigned to an object
type variable anyway (in which case the boxing's going to happen regardless)?
Boxing is one of those performance concerns which is real, but often exaggerated. If it's happening hundreds of thousands of times per second in your app, that's probably significant. If it's happening hundreds of times per hour, your concern should be more about whether the API is appropriate than the performance cost.
You're right to be a bit concerned about this, if nothing else it's definitely an anti-pattern.
However, rather then explain it here, this article is a really good read:
http://www.codeguru.com/csharp/csharp/cs_syntax/article.php/c5883
Hope this helps!
The actual overhead is platform specific. The size of Double is fixed on all platforms, but when the value is boxed it will have a reference to the runtime type object. The size of the reference is platform specific. So in total the object will be 16 bytes on 32 bit and 24 bytes on 64 bit. A new object will be created for each call. If that is going to be a problem, I can't say but I doubt it. If in doubt profile it.
精彩评论