18

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?

user3840170
  • 280
  • 2
  • 14
Jacob Birkett
  • 381
  • 3
  • 11
  • 1
    https://lore.kernel.org/lkml/CAHp75Vf__Cb2=TDQRF4R5q8bfAQev2-smcdEMWz32MvYjGnT0Q@mail.gmail.com/ – Kamil Maciorowski Aug 18 '22 at 09:21
  • 30
    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 '22 at 09:34
  • 1
    As a minimum for hibernate swap needs to be equal to at the total amount of physical RAM present (+ plus a couple of MB for overhead). But actually you need as much swap as the worst case RAM (real and swapped) in-use when the hibernation is started. This last consideration is often overlooked. The 2x physical RAM number often cited is reasonably safe as most users will never hit that limit. (If you do hit that limit the system should have had a RAM upgrade long before that. Ideally you want enough RAM for all running software without having to do much swapping.) – Tonny Aug 18 '22 at 09:47
  • 8
    Practically, the time saved by using hibernation will be offset by the time spent investigating why hibernation doesn't work. – u1686_grawity Aug 18 '22 at 09:50
  • 1
    What are you asking? The docs clearly state it's kibibyte. Do you have reason to believe that is wrong? – AndreKR Aug 18 '22 at 22:11
  • 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 '22 at 01:31
  • @user1686 I don't share that experience; I hibernate my Debian laptop and desktop daily, and for me it just works. – marcelm Aug 19 '22 at 08:11
  • 3
    @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 '22 at 14:53
  • @Heinzi Ah, good point. In light of that I have added a better answer. – AndreKR Aug 19 '22 at 15:22

2 Answers2

39

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.)

user3840170
  • 280
  • 2
  • 14
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 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 '22 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 '22 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 '22 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 '22 at 20:35
11

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.

AndreKR
  • 640
  • 6
  • 18
  • 4
    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 '22 at 20:34
  • 9
    @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 '22 at 23:11
  • 4
    In particular, the `kB` in the proc file system dates back at least to [Linux 2.0.1](https://github.com/kalamangga-net/linux-2.0/blob/9b96c06c0bc4fb57cee3935a7a812bc3fb025a70/fs/proc/array.c#L289), 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. – Nate Eldredge Aug 20 '22 at 19:36
  • > "kB has always meant 1024 bytes and pretty much still does." <- usually except with datarates/speeds, where 100mbit line is usually 100*1000^3 (and not 1024) – mulaz Aug 20 '22 at 20:31
  • I often find it useful to pay attention to letter case: ‘KiB’ is unambiguously 1024 bytes; ‘kB’ is unambiguously 1000 bytes; ‘KB’ has historically been one or the other, but usually the former; ‘kiB’ is unambiguously a clueless or sloppy writer. Exceptions to this are very rare; this answer isn’t one. – user3840170 Aug 21 '22 at 11:07
  • 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. – Tamoghna Chowdhury Aug 21 '22 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. – Tamoghna Chowdhury Aug 21 '22 at 19:22