Ubuntu Memory Allocation Failed

OS: Ubuntu 16.04

I want to install redis-server on my 512mb RAM VPS server from Digitalocean, I rean the command and I got,

Extracting templates from packages: 100%
Preconfiguring packages ...
(Reading database ... 311316 files and directories currently installed.)
Preparing to unpack .../libisc-export160_1%3a9.10.3.dfsg.P4-
8ubuntu1.9_amd64.deb ...
Unpacking libisc-export160 (1:9.10.3.dfsg.P4-8ubuntu1.9) over 
(1:9.10.3.dfsg.P4-8ubuntu1.7) ...
dpkg: unrecoverable fatal error, aborting:
fork failed: Cannot allocate memory
E: Sub-process /usr/bin/dpkg returned an error code (2)

I ran sudo dpkg --configure -a and sudo apt-get -f install, I got

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libisc-export160
The following packages will be upgraded:
 libisc-export160
 1 upgraded, 0 newly installed, 0 to remove and 169 not upgraded.
 1 not fully installed or removed.
 Need to get 0 B/153 kB of archives.
 After this operation, 1,024 B of additional disk space will be used.
 Do you want to continue? [Y/n] y
 (Reading database ... 311316 files and directories currently installed.)
  Preparing to unpack .../libisc-export160_1%3a9.10.3.dfsg.P4-
  8ubuntu1.9_amd64.deb ...
  E: Sub-process /usr/bin/dpkg returned an error code (2)

I checked the space,

man@pay:$ free -t

            total        used        free      shared  buff/cache   available
Mem:         500060      352068       76032        5800       71960      114020
Swap:             0           0           0
Total:       500060      352068       76032

What am I missing to make redis-server package install?

1 answer

  • answered 2018-01-14 04:13 Alex_Krug

    dpkg: unrecoverable fatal error, aborting:
    fork failed: Cannot allocate memory
    

    It means you do not have enough memory to execute the operation.

    Flush file system buffers by executing :

    $ sync
    

    Now try again.

    If you are still facing the same problem, follow this. To free page cache:

    $ echo 1 > /proc/sys/vm/drop_caches
    

    To free dentries and i-nodes :

    $ echo 2 > /proc/sys/vm/drop_caches
    

    To free page cache, dentries and i-nodes :

    $ echo 3 > /proc/sys/vm/drop_caches
    

    Now try again.

    When using the redis-server, there may be problems in the case of using RDB snapshots. For writing, use the BGSAVE command, which forks the current process and in this fork, the data is written to disk. Thus, the main thread is not blocked and the write occurs asynchronously. The problem is that on UNIX systems when fork () is called, the child process also copies the contents of the memory that the parent process uses. Suppose, if Redis currently occupies 2Gb of memory, and there is only 1Gb of free memory left in the system, then the following error may occur when executing the BGSAVE command:

    # Can't save in background: fork: Cannot allocate memory
    

    In modern systems, when copying memory for forks, the Copy on Write method is used. The memory is copied only when recording to the corresponding section occurs. Redis does the fork of the process only in order to save the data asynchronously, this fork does not change them in any way, so we can safely set the vm.overcommit_memory system parameter to 1. This parameter is responsible for the possibility of allocating more memory than is available. Add a line to /etc/sysctl.conf:

    vm.overcommit_memory = 1
    

    And reread the config:

    # sysctl -p
    

    In details:

    http://linuxamination.blogspot.com/2013/05/dpkg-unrecoverable-fatal-error-aborting.html

    https://redis.io/topics/faq

    redis bgsave failed because fork Cannot allocate memory

    https://habrahabr.ru/post/140893/