16

I am freshly installing a distribution and want to set up a swap partition so that it is compatible with hibernate.

First, I must determine the amount of physical memory the system has.

$ grep MemTotal /proc/meminfo
MemTotal:    38740876 kB

Now as far as I know:

  • 1 KiB = 1 kibibyte = 1024 bytes
  • 1 kB = 1 kilobyte = 1000 bytes

However, RHEL docs here say that the unit in /proc/meminfo is in kibibytes.

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/s2-proc-meminfo

Who is wrong? The unit hard-coded in Linux, or the RHEL documentation?

9
  • 1
  • 29
    Never assume kibi- or kilo- unless explicitly stated or both are used in separate context in the same program or document. KB and kB have been used for 1024 and 1000 prefixes long before KiB came onto the scene that they are routinely used and misused both intentionally and not. Sadly the only way to guess which is meant is by context and experience.
    – Mokubai
    Aug 18 at 9:34
  • 8
    Practically, the time saved by using hibernation will be offset by the time spent investigating why hibernation doesn't work.
    – user1686
    Aug 18 at 9:50
  • 2
    when talking about RAM or block size it obviously makes zero sense to use 1000, all the units must be a power of 2
    – phuclv
    Aug 19 at 1:31
  • 2
    @AndreKR: "What are you asking? The docs clearly state it's kibibyte. Do you have reason to believe that is wrong?" OP believes that kB always means "1000 byte". Under this (faulty) assumption, asking why the UI says one thing and the docs say another thing makes perfect sense.
    – Heinzi
    Aug 19 at 14:53

2 Answers 2

35

Both "kilobyte" and "kB" are also sometimes used to indicate binary (1024 byte) units. This usage well predates the invention of dedicated binary "kibi-" prefixes, especially in contexts where true decimal units would make little or no sense.

For example, RAM usage is always measured in binary units (e.g. a "4kB page" is always 4096 bytes, never 4000), so the more familiar "kilo-" or "mega-" prefixes are typically used even if the actual sizes are binary.

In your case (I checked the kernel code that creates the /proc/meminfo file), the kernel internally counts memory in terms of free pages (which are typically 4k or 16k but always power-of-two) and its show_val_kb() function uses a bit shift operation (which is equivalent to a multiplication by power-of-two, producing binary units again) to convert the page count into a kilobyte value:

static void show_val_kb(struct seq_file *m, const char *s, unsigned long num)
{
        seq_put_decimal_ull_width(m, s, num << (PAGE_SHIFT - 10), 8);
        seq_write(m, " kB\n", 4);
}

(On x86 and x86_64 systems PAGE_SHIFT is always 12, so a memory page is 1 << 12 bytes which is equal to 1×212 i.e. 4096, and the above function ends up multiplying the page count by 2(12−10) before displaying it as "kB".)


The 'lsblk' and 'fdisk' disk partitioning tools from util-linux also work with binary units by default. For example, +2G in fdisk is equivalent to +2GiB. (However, fdisk also accepts +2GB if you want a decimal-sized partition for some reason.)

4
  • 2
    To clarify, the - 10 is equivalent to a right shift by 10 bits, which is equivalent to a division by 2^10, i.e. 1024. So the result is indeed in kibibytes (KiB).
    – jcaron
    Aug 19 at 13:33
  • 3
    Basically, the Linux kernel (and the Unix kernel it's based on) far predate hard drive manufacturer's invention of the 1000-based system of measurement for selling hard drives at higher apparent capacity than they actually are. Ergo, the kernel reports in real kB.
    – FreeMan
    Aug 19 at 15:29
  • 7
    @FreeMan The prefix kilo (or k) has meant 1000 since 1792. It is consistent with the metric system for kB to mean 1000 bytes. It is more useful in computing to have units for powers of two, but probably wiser to have not called them kilobytes. At the end of the day, the issues far predate hard drive manufacturers and aren't about deception.
    – prosfilaes
    Aug 19 at 19:39
  • 1
    @FreeMan If a CPU can transfer one byte per clock cycle, how long would it take a 1-MHz CPU to transfer 1 MB of data to RAM? Can you fully copy 1 GB of RAM onto a 1 GB SD card?
    – Nayuki
    Aug 19 at 20:35
9

From the comments it seems you are under the impression that the unit kB means 1000 bytes and that if you meant 1024 bytes you would have to write KiB.

But that is not how it is used in computing.

kB has always meant 1024 bytes and pretty much still does.

The word "kibibyte" and the unit symbol KiB were introduced by IEC in 1998 in a well-intended attempt to clarify that the k/K in kB/KB are not an SI prefix.

In practice however those are rarely used, and apart from hard drive capacity (for marketing reasons) you can safely assume kB to mean 1024 bytes ("1 kilobyte"), especially in software that was introduced before 1998.

7
  • 3
    Having k-/kilo- change its meaning based on context is very bad. SI defines kilo- as 1000 over a century ago; it is computer manufacturers who abused the definition to mean 1024 because it's "close enough". Please stop perpetuating the myth that kB means 1024 bytes as it is harmful.
    – Nayuki
    Aug 19 at 20:34
  • 8
    @Nayuki It is not computer manufacturers- ordinary practitioners in the field used the term kilobyte to mean 1024 long before there were storage manufacturers who cared. Moreover, storage manufacturers typically prefer the kilobyte = 1000 definition, because they can deliver less and claim more. Honestly the ambiguity is not a problem, because if you're in a position where the ambiguity is a problem, then you also know the ambiguity exists and you need to be careful about it.
    – David
    Aug 19 at 23:11
  • 3
    In particular, the kB in the proc file system dates back at least to Linux 2.0.1, circa 1996, well before KiB was standardized. And it's now effectively part of the ABI and can't be changed without breaking old programs that might do scanf("MemTotal: %ld kB"). So whether or not it's "wrong", it's definitely 1024 in this specific context and always will be. Aug 20 at 19:36
  • 2
    @mulaz yeah, but datarates/speeds are almost always measured in x bits per second, indicated by the small b instead of capital B in Mbps/Gbps/kbps. Aug 21 at 15:26
  • 1
    @Scott-СлаваУкраїні nitpick - small b means 'bit'. As for the rest, everything is context-dependent. In the context of datarates, M in Mbps (almost) always means 10^6. Also, honestly, who the hell remembers anything these days. Much more reliable just to look it up. Or be OP, and be confused by the docs literally telling you the exact thing you need to know. Aug 21 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.