Software Development

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 MeaningIn bytes
mb
milli-bits1/8000th of a byte
mB
milli-bytes1/1000th of a byte
kb
kilo-bits
125 bytes
kB
kilo-bytes
1000 bytes
Kb or Kib
kibi-bits
128 bytes
KB or KiB
kibi-bytes
1024 bytes
Mb
mega-bits
125,000 bytes
MB
mega-bytes
1,000,000 bytes
Mib
mebi-bits
131,072 bytes
MiB
mebi-bytes
1,048,576 bytes

What annoys me is when professionals confuse these terms.

In UNIX:

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.

Puzzle

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

Conclusion

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.

Reference: Mixing memory unit messages from our JCG partner Peter Lawrey at the Vanilla Java blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jacob Zimmerman
Jacob Zimmerman
9 years ago

For the longest time, there wasn’t a Mi or Ki prefix, and the meaning of k, K, and M were ambiguous, meaning factors of 1000 or 1024. They defined the Mi and Ki prefixes to help avoid that, but they’re not well-known, so it’s understandable that people screw THAT up. Mixing up b and B is inexcusable, since they’ve been that way since really early one, I believe. And using lower case m shouldn’t mean anything, since you can’t do 1/1000th or 1/1024th of a byte or bit. In any case, I think a lot of it has to do… Read more »

Back to top button