Can we not simply connect a battery to a RAM to prevent data loss during power cuts?
Short answer: You might be able to "connect a battery to a RAM to prevent data loss during power outage", but this depends on the type of RAM.
SRAM (Static Random Access Memory) is not DRAM, explained below. SRAMs are found in many electronic devices such as a modern (home) thermostat (uses a battery to retain the settings if the power goes out) and in an alarm clock (battery keeps the time and can power unit for a short period), etc. Hard drives typically have some SRAM in them masquerading as "32MB of disk cache." Specialized SRAM is even present inside the CPU, as it's blazing-fast L1, L2, and L3 cache memory.
Lithium "coin cell" primary (non-rechargeable) batteries are commonly used for SRAM retention. Modern SRAMs can have lower working voltages, such as 1.8 V, requiring more circuitry to back with a 3.0 V battery. The supply current is very small (even nano-amps) so data can be retained for a long time from a small battery. Many industrial machines (robots, variable-frequency drives, programmable logic controllers, etc.) employ a SRAM backup battery to retain critical data through (frequent) power cycles.
The acronym SRAM means "Static Random Acccess Memory." Static, because its contents stay whatever state they were programmed, indefinitely, but only as long as power is applied. SRAM is very fast but bulky physically and is relatively expensive, so only finds niche roles even today. SRAM and DRAM are volatile types, meaning that their contents are lost if power is lost.
EEPROM (Electrically-Erasable Programmable Read-Only Memory) is an old, non-volatile digital storage medium, meaning data persists after power-off. These were originally "programmed" or "fused" at the bit-level, but modern ones are somewhat faster and can handle data in blocks. These are still much slower than SRAM or DRAM. Due to this extreme slowness, limited capacity, and a limited write/erase capability, they only find use in roles such as parameter storage and tiny programs for microcontrollers, etc. It is infeasible to backup main system memory to EEPROM due to the large amount of time involved.
FLASH memory, commonly seen as a USB Flash Drive and in SSD Hard Drives, is another non-volatile storage medium. It too is much slower than other types of RAM, and has a finite write limit, so is not used for main memory. A SSD hard drive is nothing more than a whole bunch of these, all used in parallel, to increase speed and capacity.
DRAM (Dynamic Random Access Memory) is a different beast altogether. "Dynamic" because it is always changing, and the contents will change (be lost) without intervention. DRAM is relatively dense (capacity-wise) and inexpensive, so is used for computer main system memory. Unfortunately it is also slower* than SRAM and needs to be "refreshed" continuously, else its contents are lost within a short time depending on temperature. Normal operation of the PC's memory controller keeps the data in DRAM refreshed continuously so that it is always available. If power is turned off, the memory controller still needs to refresh the data while running on backup battery power. Laptops, for example, do this when suspending to RAM. Because this refresh is an active process, it draws more current, and thus the battery is typically much larger than a coin cell. If the battery is depleted, then the DRAM contents are lost and the machine is forced to cold-boot.
A promising new technology is FeRAM. (Ferro-Electric RAM) is quite fast, non-volatile, and has a high endurance. It is new technology, so is quite expensive and capacity is limited, so has very limited roles.
*Aside: to get around the slowness of DRAM, a PC uses these massively in parallel. First, a whole stick is accessed at once (8x speed since there are 8 chips on it), then if the motherboard supports dual-banking, two modules at once (2x8 = 16x), triple-banking = 24x, etc. This is how a module marked "PC3-10666" can possibly perform like one chip running at 10,666MHz. From a 666MHz bus: 666*2 (DDR means two transfers per clock) * 8 chips/module = 10,666.
Can we not simply connect a battery to a RAM to prevent data loss during power cuts? So can you tell me why is this not done?
Of course we do! It's called battery-backed SRAM, and it's widely used in embedded systems. These days, the cost of NVRAM technology (such as conventional EEPROM, or new FRAM) is low, they can do the same thing without power, so it's not as common as before, but it's still here. And it's very likely that your desktop computer still has one inside to keep the BIOS settings.
Image source: ST M48T128Y datasheet
Applications include:
Date/Time. The coin cell on your computer motherboard powers the real-time clock chip, which has SRAM inside that keeps the time. It's the reason that your desktop computer remembers the time even if power is removed.
Calibration Parameters. A piece of test equipment such as an oscilloscope can save the current calibration data inside SRAM (A common problem of old test equipment is losing all calibration data when the battery is dead).
Runtime Parameters. A microcontroller can save important runtime data in a battery-backed SRAM, so that the data never loses even if the microcontroller is reset due to a power outage or crashes. Similarly, a handheld scientific calculator can save your variables.
- Many desktop computers still use this technique to retain some BIOS settings, along with the date/time. It is kept because people found that it is easy to remove the battery and reset everything if the BIOS setting is bad. Early PCs used real dedicated SRAM chips, modern PCs use an integrated solution. Although modern UEFI firmware mostly uses NVRAM, but some still have a battery-backed SRAM in addition to NVRAM.
Cryptography and Security. A security hardware can save the private key inside a battery-backed SRAM, with a
tempertamper-detection mechanism that erases and disconnects the SRAM if intrusion is detected, permanently destroys the private key. Similarly, in the extreme case, the program itself is saved in SRAM to frustrate any reverse-engineering efforts by competitors.
We don't do this on desktop computers. The large DRAM on computers use a lot of power. Also, most operating systems and software is not designed to continue working after a power loss anyway. You can implement something like hibernation, but if so, why don't you just use hibernation? Overall, it's not done, not because it's impossible, it's simply because nobody really wants to do it.
For a PC, that's more or less what "hibernate on low battery" is. The contents of RAM are written to disk in the hibernation file. Since you need the whole system to be on while doing this, it needs quite a lot of power, and is therefore only suitable for systems which would have a battery anyway like laptops.
Similarly it's possible to boot quickly from hibernation; Intel call this "rapid start" and it's been available on some systems for a long time.
The main limiting factor is that RAM is so large that it takes many seconds to write it all to Flash.