Mixing memory unit messages

One of my pet hates is developers messing up units of memory.  In general, the difference doesn't matter much, or you can work out what they meant to say, but it does annoy me when developers who should know better appear to be confused about what they are trying to say.

From Wikipedia's Byte page, here is a cheat sheet of units

"b" means bits
"B" means bytes
"k" or kilo- is standard prefix meaning 1000
"K" or "Ki", sometimes called kibi- is 1024
"m" or mill-, means 1/1000th
"M" or mega-, means 1000^2
"Mi" or mebi-, means 1024^2

 Unit      Meaning      In bytes
milli-bits1/8000th of a byte
milli-bytes1/1000th of a byte
125 bytes
1000 bytes
Kb or Kib    
128 bytes
KB or KiB    
1024 bytes
125,000 bytes
1,000,000 bytes
131,072 bytes
1,048,576 bytes

What annoys me is when professionals confuse these terms.


In top on Unix it states
KiB Mem:  32893900

but when I do "head -1 /proc/meminfo" it states
MemTotal:       32893900 kB
this is 2.4% less.

I suspect top is correct as it is closer the amount of memory installed and they used "KiB" which lends credibility, but I can't be sure.

In the JVM

The default translation for "KBYTES" is "kbytes" but in Japanese and Chinese it is "KB" which is 2.4% more.

While the JVM appears to be using KiB or kibibytes every where, it refers to "KiB" only three times, but uses "kB" in twice, "Kb" in seven places, "KB" in 127 places and "kilobytes" in three cases.

Similarly "MiB" appears 4 times, "MB" in 87 places, "Mb" three times and "mb" three times.


Question: A computer program writes to memory at 49 mb/s, to disk at 50 mb/s and to the network at 100 mb/s. Which is it writing data at a higher rate to; the memory, the disk or the network?

Answer: It is probably writing to memory most and the network least.  This is because it might be read as "... to memory at 49 MiB/s, to disk at 50 MB/s and to the network at 100 Mb/s. " and one MiB/s is almost 8.4 Mb/s


If you don't know what units you have in mind and which ones you don't, it shouldn't be surprising if someone reading your output is confused as well.

I encourage everyone to use standard units in their code so when you see units you know exactly what they mean.


Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues