开发者

Does using small datatypes (for example short instead of int) reduce memory usage?

My question is basically about how the C# compiler handles memory allocation of small datatypes. I do know that for example operators like add are defined on int and not on short and thus computations will be executed as if the shorts are int members.

Assuming the following:

  • There's no business logic/validation logic as开发者_运维知识库sociated with the choice of short as a datatype
  • We're not doing anything with unsafe code

Does using the short datatype wherever possible reduce the memory footprint of my application and is it advisable to do so? Or is using short and the like not worth the effort as the compiler allocates the full memory ammount of a int32 for example and adds additional casts when doing arithmetic.

Any links on the supposed runtime performance impact would be greatly appreciated.

Related questions:

Why should I use int instead of a byte or short in C#

Integer summing blues, short += short problem


From a memory-only perspective, using short instead of int will be better. The simple reason is that a short variable needs only half the size of an int variable in memory. The CLR does not expand short to int in memory.

Nevertheless this reduced memory consumption might and probably will decrease runtime performance of your application significantly. All modern CPUs do perform much better with 32bit numbers than with 16bit numbers. Additionally in many cases the CLR will have to convert between short and int when e.g. calling methods that take int arguments. There are many other performance considerations you have to take before going this way.

I would only change this at very dedicated locations and modules of your application and only if you really encounter measurable memory shortages.

In some cases you can of course switch from int to short easily without hurting performance. One example is a giant array of ints all of which do also fit to shorts.


It makes sense in terms of memory usage only if you have in your program very large arrays (or collections built on arrays like List<>) of these types, or arrays of packed structs composed of same. By 'large' I mean that the total memory footprint of these arrays is a large percentage of the working set and a large percentage of the available memory. As for advisability, I'd venture that it is inadvisable to use short types unless the data your program operates on is explicitly specified in terms of short etc., or the volume of data runs into gigabytes.


In short - yes. However you should also take care about memory alignment. You may find Mastering C# structs and Memory alignment of classes in c#? useful


Depends on what you are using the shorts for. Also, are you allocating that many variables that the memory footprint is going to matter?

If this program was going to be used on a mobile device or a device with memory limitations then I might be concerned. However, most machines today are running at least 1-2 gb of ram an have pretty decent dual core processors. Also, most mobile devices today are becoming beast mini computers. If your declaring so much that that type of machine would start to die then you have a problem in your code already.

However, in response to the question. It can matter in memory limited machines if your declaring a lot of 4 byte variables when you only need a 2 byte variable to fill them then you should probable use the short.

If you preforming complicated calculations, square roots and such, or high value calculations. Then you should probably use variables with more bytes so you don't risk losing any data. Just declare what you need when you need it. Zero it out if your done with it to make sure C# cleans it up, if your worried about memory limitations.


When it comes to the machine language, at the registry level, I think it is better to align with the registry size as most of the move and arithmetic functions are done at the registry boundaries. If the machine has 32-bit registry set, it is better to align with 32-bit. If the machine has 16-bit registries for I/O operations, it is a good practice to align with 16-bit to reduce the number of operations in moving the content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜