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

家住魔仙堡 提交于 2019-11-28 10:55:57

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.

Oleg

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!