What is meant by "Nominal storage allocation" in the context of primitive data type allocation size?
Looking at this table describing the data types in VB.
One of the columns is labeled "Nominal s开发者_如何学Pythontorage allocation". What does this mean? Why is the word "nominal" here?
I believe that in this context, "nominal" means the number of bytes taken up by the actual data contained in these data types, excluding whatever storage the CLR uses to track the values, e.g. the heap allocation that happens when a value type is boxed.
EDIT
On reading the linked article, I noticed the following section:
Memory Consumption
When you declare an elementary data type, it is not safe to assume that its memory consumption is the same as its nominal storage allocation. This is due to the following considerations:
Storage Assignment. The common language runtime can assign storage based on the current characteristics of the platform on which your application is executing. If memory is nearly full, it might pack your declared elements as closely together as possible. In other cases it might align their memory addresses to natural hardware boundaries to optimize performance.
Platform Width. Storage assignment on a 64-bit platform is different from assignment on a 32-bit platform.
So basically this is saying that total storage per value type is nominal storage + whatever padding may be used to align the value at a word boundary + possible heap allocation - again, at the discretion of the runtime.
"Nominal" refers to the fact that the runtime is free to allocate as much space as makes sense for a given variable. Although a short only needs two bytes to store its data, the runtime may allocate 4 bytes for the variable. For example, a short stored in a register of a 32-bit machine is "taking up" 4 bytes. Similarly, the stack may work the same way in that 4 bytes of stack space are used when passing a short as a parameter, for example, to maintain word alignment. Memory access is generally faster when memory is aligned on their natural word boundaries, so the runtime may "waste" space to align memory. On 64-bit machines, the same scenarios would probably use 8 bytes each.
精彩评论