34

So, I was just wondering about how volatile memory storage loses data when the power to them is cut off. But can we not solve that problem by using a battery in conjunction with a RAM and a ROM and when there's a power cut i.e. when the computer didn't receive a shutdown signal the battery kicks in and a controller transfers data from RAM to an EEPROM?

EEPROM - because I guess HDDs and SSDs consume more power. I mean if we could do this then we can maybe program it to explicitly store data in EEPROM, if not data then the OS(only a copy). That way we can also boot faster. There maybe very obvious reasons not to do this. But they are not clear to me. So can you tell me why is this not done?

Volker Siegel
  • 1,072
  • 1
  • 12
  • 25
Vishal Dalwadi
  • 485
  • 1
  • 4
  • 9
  • 9
    Why bother with transferring to EEPROM if the RAM is battery backed up. – Andy aka Oct 07 '19 at 15:17
  • Absolutely no point as other peripherals will get reset and they have nothing to do with the saved RAM data when back from RESET. You have UPS to prevent sudden power downs for the whole system. – Eugene Sh. Oct 07 '19 at 15:22
  • 9
    And the eeprom idea.... Well today a typical mid-end home PC has about 8GB of DRAM. EEPROM of 8GB? MAybe you meant flash after all? – Eugene Sh. Oct 07 '19 at 15:26
  • 8
    They already do that, though not for main memory in a computer; the common term is "battery-backed SRAM". SRAM is too expensive by far to use as main memory, but battery-backed SRAM was used historically for things like BIOS settings (ever heard of "the CMOS battery"?), and it's how saving games worked on NES, SNES, Game Boy, and a few early Game Boy Advance video games. – Hearth Oct 07 '19 at 15:29
  • First off, you don't need to there are already systems that do this: https://www.avadirect.com/Battery-Backup-Unit-w-128-MB-DDR-ECC-Cache-Module-for-SRCU42E-RAID-Controller/Product/2513 Secondly EEPROM is SLOW and takes up significantly more space, so you don't need to do this – Voltage Spike Oct 07 '19 at 15:32
  • 6
    https://en.wikipedia.org/wiki/Non-volatile_random-access_memory – Paddy Oct 07 '19 at 15:37
  • Worth considering is that you also have to be able to get back to a running state after the power blip. If 100% of the system is battery backed up, you have no problem (we call that a UPS). However, if part of the system is backed up, it has to be ready to figure out a working state for all of the components that lost their state during the power outage. That can be an entertaining exercise. – Cort Ammon Oct 08 '19 at 01:04
  • 6
    In laptops the suspend mode powers (and refreshes) the DRAM only. Power requirements are relatively low, usually a week or more in suspend mode is possible before the laptop’s battery is empty and the RAM contents are lost. Some laptops/OSses have a feature where they wake up after a certain time, write the RAM contents to disk (hibernate) and shut down completely. – Michael Oct 08 '19 at 07:35
  • @Andyaka It is battary backed up but we won't know for sure how long the power cut will be for. So what I meant was that we can back it up on a EEPROM and then we won't have to worry about power and the battery draining. Also, now I read in the answers that DRAMs need a lot of power because there are so many refresh cycles, so to rely on battery to keep data intact in RAM would be a bad design decision. – Vishal Dalwadi Oct 08 '19 at 07:49
  • 1
    Some company actually made drives based on RAM, you had to power them externally and they were pretty expensive, reference [HyperDrive](http://www.hyperossystems.co.uk/07042003/products.htm) – DarkBee Oct 08 '19 at 13:11
  • 1
    It's been made at times, but it's never caught on because it's quite expensive. See here for one example: https://arstechnica.com/information-technology/2013/04/memory-that-never-forgets-non-volatile-dimms-hit-the-market/ – Nate S. Oct 08 '19 at 17:15
  • 1
    @VoltageSpike: Battery-backed RAID cache was what I was immediately thinking of. High-reliability RAID controllers allow you to hot-swap a broken controller, plop the cache module back in, and continue working without data-loss. – Jörg W Mittag Oct 09 '19 at 10:12
  • 1
    You probably always want the *option* to clear RAM. Remember the old troubleshooting question "have you tried turning it off and on again?" That fixes a lot of issues with bad state because RAM is cleared. Clearing bad state is also the idea behind process supervision and restarts in Erlang. – Nathan Long Oct 09 '19 at 14:07
  • 1
    There always this: https://en.wikipedia.org/wiki/Uninterruptible_power_supply but it sounds like you mean something smaller or more localized to memory only. (What is your use case here?) – Jeff Y Oct 09 '19 at 17:23
  • @JeffY It was just a question that occurred in my mind. – Vishal Dalwadi Oct 09 '19 at 17:56

7 Answers7

48

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.

rdtsc
  • 15,913
  • 4
  • 30
  • 67
  • 3
    Thank you! This was a great help. – Vishal Dalwadi Oct 07 '19 at 17:01
  • 9
    Before the flash SSD era, haven't there been storage devices that were essentially just boxes of battery-backed DRAM, with dedicated circuitry for refreshing it? – user1686 Oct 08 '19 at 04:56
  • 9
    @grawity There have been (cf. https://images-na.ssl-images-amazon.com/images/I/41CVgprtpsL.jpg). They were sold as literal "RAM Disks". Though I would not label them as "common", more a really specialized exotic piece of hardware – and in that domain virtually *anything* existed if you looked hard enough. – Hermann Oct 08 '19 at 05:19
  • 9
    https://en.wikipedia.org/wiki/Memory_refresh says: "Although in some early systems the microprocessor controlled refresh, with a timer triggering a periodic interrupt that ran a subroutine that performed the refresh, this meant the microprocessor could not be paused, single-stepped, or put into energy-saving hibernation without stopping the refresh process and losing the data in memory. So in modern systems refresh is handled by circuits in the memory controller, or increasingly on the chip itself." – Razvan Socol Oct 08 '19 at 05:56
  • 1
    There also used to be the battery-backed disk cache in professional disk/RAID controllers not so longer ago (called "Battery Backup Unit"). When the power goes away, the data in the disk cache not yet written out to the rotating disk is retained for later. More recent cards just write the data to an internal flash instead. See also: https://serverfault.com/questions/203355/why-do-i-need-a-raid-battery-pack – David Tonhofer Oct 08 '19 at 07:10
  • 1
    @RazvanSocol Only Dynamic RAM (DRAM) needs to be refreshed. Static RAM (SRAM) does not have this requirement, and the CPU can be paused or even switched off if other details like bus buffers are designed correctly. SRAM will keep it's memory intact as long as it has power. – Reversed Engineer Oct 08 '19 at 08:53
  • 5
    @ReversedEngineer: of course. I was commenting to rdtsc, who wrote: „To "refresh" DRAM properly, the CPU (traditionally) has been responsible. There is no dedicated chip just for refreshing it.” That's no longer the case in modern systems. – Razvan Socol Oct 08 '19 at 09:08
  • @RazvanSocol Sorry, I misunderstood you. Useful info as well, since I thought that the CPU, even today, is used to refresh DRAM – Reversed Engineer Oct 08 '19 at 10:47
  • @RazvanSocol what I meant by "There is no dedicated chip just for refreshing it” was that Vishal has essentially zero hope of keeping DRAM contents with DC power alone. – rdtsc Oct 08 '19 at 12:01
  • @rdtsc: According to https://user.eng.umd.edu/~blj/papers/ieeetc65-1.pdf, DDR4 has a self-refresh mechanism. – Razvan Socol Oct 08 '19 at 17:14
  • 3
    nitpick, but "within thousandths of a second" is not really correct. The minimum retention time is 64ms at the maximum rated temperature, which I'd describe as more like "hundredths of a second". Also at more normal temperatures, retention is in the neighborhood of multiple seconds to minutes. It is a well known attack to significantly cool the memory of a target machine before removing it and installing in another machine to read the data off it. (The temperature dependence is exponential, allowing data retention of even hours) – Aaron Oct 09 '19 at 03:25
  • 1
    On many of the early microcomputers DRAM refresh was taken care of by external circuitry, often the video circuitry. The Z80 was the first microprocessor I recall that did DRAM refresh itself, but even there I think it would be more clear to consider the refresh to be done by a "memory controller" integrated into the CPU, since this was (and still is, mostly) invisible to the programmer and works the same way whether the memory controller is integrated or off-board. – cjs Oct 09 '19 at 04:35
  • 1
    The original PC used a timer chip and a dma chip to refresh its memory. @CurtJ.Sampson - on early systems or terminals with built-in video controllers, the video controller's accessing of memory to update the screen was sufficient to meet memory refresh requirements. – rcgldr Oct 09 '19 at 06:53
  • @rcgldr Well, I think for a lot of systems just having video alone wasn't enough. As far as I'm aware, all 8-bit Apples and CBM machines other than those with static RAM needed special additional support for DRAM refresh designed in; the video alone wouldn't do it. (Which makes sense; on many of those systems there were not only DRAM rows that were never accessed by the video generation circuitry but even entire banks of chips that never were.) – cjs Oct 09 '19 at 08:59
  • 2
    I think it might be somewhat pointless to save RAM contents unless the CPU registers and cache memory could also be saved. The hibernate process on a PC must save something more than the exact state of RAM in order to allow the PC to 'restore' the computer state when it wakes up. – B H Oct 09 '19 at 14:27
  • @rcgldr No, I specifically mean the non-Z80, non-CP/M machines that by the very early 80s were by far the most common 8-bit systems. For almost none of these was having a video controller alone sufficient to do refresh; they all needed separate refresh circuitry (often added to or integrated with the video controller) for refresh. – cjs Oct 09 '19 at 15:20
  • @CurtJ.Sampson: On the Apple II, the concatenation of (A0-A4, A7-A9) will cycle through all 256 values with video fetches performed every ~4.4ms. – supercat Oct 09 '19 at 19:51
  • @CurtJ.Sampson - On the 8 bit Atari's, 400, 800, 65XE, 130XE (128KB ram), the ANTIC chip did the DRAM refresh, as well as DMA and part of the video along with the GTIA chip. Unlike the other 6502 based systems (Apple II, Commodore 64), the Atari's ran their 6502 at about 2 MHz (versus 1 MHz). – rcgldr Oct 09 '19 at 19:52
25

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.

Battery-Backed SRAM

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. Embedded systems still have dedicated RTC chips, but for post-2000s PCs, it's integrated into the motherboard chipset, a.k.a the southbridge, or Platform Controller Hub as Intel calls it.

  • 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 integrated the RTC/SRAM circuitry into the motherboard chipset.

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 temper tamper-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. DRAM's power consumption is too high for a small battery. 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?

As the comments have pointed out, technically we do, if you use the Suspend feature on a laptop running on its battery, it's basically equivalent to "connecting a battery to the RAM."

比尔盖子
  • 6,064
  • 2
  • 24
  • 51
  • 4
    @dave_thompson_085 A lot of people do get angry as well in the retro gaming / arcade community, some games are permanently lost when the battery of the encryption-key SRAM is dead ;-( – 比尔盖子 Oct 08 '19 at 06:54
  • 5
    Well, it's largely available on laptops: it is exactly what "suspend to RAM" is, and I use it every day instead of deep hibernation, because it's faster to wake up. I'm probably not the only one, and I can tell you all major operating systems support it since many years. – dim Oct 08 '19 at 09:55
  • "And it's very likely that your desktop computer still has one inside to keep the BIOS settings." Is this what the CMOS battery is? – Cruncher Oct 10 '19 at 15:33
  • 1
    I recently paid good money to replace the memory board holding the configuration data for one of my radios ([ICOM R-71A](https://www.universal-radio.com/catalog/commrxvr/r71a.html) ) after the battery died. I could have simply replaced the battery, but there was no way to re-enter the configuration data (and no source of configuration data I could find to enter). It's impressive that the battery lasted 30 years, just wish I'd known I had to replace it before it was gone. – TMN Oct 10 '19 at 16:13
  • @Cruncher Yes. Although the term "CMOS battery" is kind of a misnomer. CMOS is the name of the semiconductor technology used for manufacturing all kind of chips, calling the battery the "CMOS battery" is as meaningless as naming a computer program the "C program". I suppose that the name came from the fact that CMOS enabled the first generation of ultra-low power SRAM and RTC chips and made battery-backup solutions more practical, so people started to use the name of the technology to refer the SRAM/RTC chip that holds the BIOS settings and time. – 比尔盖子 Oct 10 '19 at 16:49
7

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.

pjc50
  • 46,540
  • 4
  • 64
  • 126
  • In the case of a PC, it may happen that the O.S. does an "hybrid" aproach: write to disk, but keep in RAM as well. This way, it can boot super-quickly and is protected in case of a power failure, since it has all the hybernation data on the disk. This is faster than a cold boot and restores the contents and running programs, just like suspending to RAM would. – Ismael Miguel Oct 09 '19 at 11:55
6

EEPROM - because I guess HDDs and SDDs consume more power.

SSDs are EEPROM by definition.

What you describe is exactly how "suspend to disk" or "hibernate" functions in modern computers do. You pull the plug, the battery or an external battery (UPS) starts to power the computer and when the battery depletes to a certain point, the OS (depending on settings) just writes the whole RAM to a file and shuts down. When you power the thing back on, the OS sees that the "hibernation" file exists and instead of booting normally, it reads the file to RAM and starts from where it stopped before (a lot of simplifications here, of course).

There is also a "suspend to RAM" function - turn off periferals, halt the CPU and power only the RAM and the RAM controller.

Both functions can be chained - you suspend to RAM first, and when the battery depletes some more, the computer wakes briefly in order to transfer the RAM content to the disk and power itself off completely.

fraxinus
  • 8,726
  • 11
  • 34
  • 2
    "Both functions can be chained - you suspend to RAM first, and when the battery depletes some more, the computer wakes briefly in order to transfer the RAM content to the disk and power itself off completely." – It actually makes more sense to write the RAM contents to disk *first*, because then you don't have to do anything other than powering off when the battery depletes. This is called Hybrid Sleep in Windows, Safe Sleep in macOS ("Safe" because you can safely pull out the battery / lose power) and also exists in Linux. – Jörg W Mittag Oct 09 '19 at 07:01
  • 2
    @JörgWMittag I believe that that's not exactly what happens. Don't quote me on this, but I believe that some O.S. can turn off peripherals and most devices (like if it was suspending) and write everything to the disk before going into the lower-power states (aka: suspend). When you power on the PC, it can resume everything. In case of a power failure, everything is already in the disk and it just resumes. I strongly believe the copying is done before halting the CPU, but after turning off all peripherals (display, keyboard, mice, webcam, network cards, gpu, others). – Ismael Miguel Oct 09 '19 at 12:00
3

To add to the other thorough answers, let me point out that simply having power available for the memory is insufficient to guarantee a proper restart where you left off. If you simply allow the power to drift out of specification for the processor it will typically "go nuts" at some point as the internal logic begins to fail and a GHz speed processor can create a lot of data corruption in the milliseconds it takes to drop to the point where it goes silent entirely (and even a single bit might be enough to cause it to crash).

For this reason you want supervisory circuits and usually som software that monitor the power rails and permit an orderly start-up and shut down that does not corrupt non-volatile or battery-backed SDRAM or RAM.

Spehro Pefhany
  • 376,485
  • 21
  • 320
  • 842
1

If your really looking to protect your a device from power loss then a UPS is the way to go. APC and numerous other brands have 1500va UPS that will allow most computers to run 10 minutes or so depending on the power consumption of the device.

The UPS is a battery, but its easy to implement and doesn't require any fiddle about to solder it on or etc.

If you are taking about a computer you can put it in sleep mode, the UPS will last a lot longer.

For the PC you can hibernate the computer, it writes the contents of RAM to the hard drive and reloads it when the computer it turn on again.

cybernard
  • 119
  • 1
  • 5
1

Since @rdtsc has provided an excellent answer let me share an experience which might be what you are thinking:

In a remote part of Canada a building used a heating/cooling system from a very reputed company. But grid power was unreliable. If power was lost it took over 2 hours for the system to reboot (power interruptions happen more than once per day in Winter). Imagine taking 2 hours when it is -50C cold (brrr). What I did was 'battery back' (UPS) the computer board. So, when power came back on, the system resumed immediately. I believe it is still in use...

Mr. de Silva
  • 111
  • 4
  • I was unaware of UPS before I asked this question. This is why I thought that the RAM can be battery-backed so that when the power cuts happen, the data that is in the main memory can be kept on EEPROMs. The point I am trying to make is that I was thinking about data loss, but if UPS is used we don't need to worry about power cuts, they would power the machine. The use of battery-backed RAM would only preserve data but UPS would allow you to use the computer system so that you can perform emergency backups. – Vishal Dalwadi Oct 10 '19 at 07:19
  • My approach to solving the problem of power cuts was narrow. – Vishal Dalwadi Oct 10 '19 at 07:20
  • 1
    Most UPSs have a feature where it can communicate with your computer and shut it down automatically when power is low, etc... As additional info, before EEPROMS, stereos had memory chips which had a special pin for connecting a 9V battery. This allowed the MPU to 'remember' the tuning, etc when the power is off... – Mr. de Silva Oct 10 '19 at 08:37