views:

193

answers:

4

In .NET framework, Knowing how much each data types consumes memory is useful, but it's so forgettable. Is there any way to remember it?

If I know for example, that the int data type consumes 4 bytes, it's useful. But this kind of data are so forgettable specifically when I use other software such as SQL Server that memory consumption there might be so different from the .NET framework.

Is there any way to remember them instead of seeing documentations?

+1  A: 

I don't know if something like that is worth consciencely memorizing. I'd recommend creating a cheat sheet and refer to that as needed. In time you'll memorize it, or at the ones you use most frequently.

Jay Riggs
A: 

Knowing the size of the basic datatypes is sometimes useful when you have to operate with low-level structures (for example, if you have to exchange data with C or assembler programs, or data from specific hardware interface), or when you have to process lots of data and need an estimation about memory consumption. But, in practice, you have only to deal with a handful (less than a dozen), so what's so hard about it to remember them?

Doc Brown
+1  A: 

I don't know why you're finding it useful to remember these numbers. You really don't need to bother. It's not like "back in the day", when we'd have to add up the sizes of data structures to make sure it all fit within 32 kilobytes.

Be glad that we're past that point.

John Saunders
I have had a few interviews where this was asked but that was before managed code
Perpetualcoder
-1. There are still many, many reasons this information is useful. Writing an XNA game? Designing a cache-efficient data structure when processing millions of records? Designing a binary protocol or compact file format? This is the kind of thinking that is marginalizing our ptofession.
Robert Fraser
@Robert: sorry, but your kind of thinking is what wastes people's time. Whatever few of us need to know this information can go find out. 99% of us don't need to know or care. Times have changed. We no longer need to ask interviewees if they know how overlays work.
John Saunders
@John: In a recent SO question, I found out that any data type smaller than the machine WORD size is read and written atomically, while if you're reading a larger data type (i.e. a long or double on x86), it's possible to read a half of one value and half of another. But of course, multithreading is *so* 2009.
Robert Fraser
@Robert: depending on the size of a machine WORD is very 20th Century.
John Saunders
A: 

For primitive types "if it sounds big, it is big" :-). A "long" is bigger than a "short". A "double" is bigger than a "float" (and something that floats around would be light, i.e. fairly small). A "byte" is bigger than a "nybble". This doesn't work for a decimal, though (decimal sounds tiny, but it's actually the largest primitive type)

In fact a double sounds like it'd be double the regular size, and if you think of int as a good baseline, than a double is indeed double the size of an int.

Robert Fraser