You get Not Enough Memory while running a command? How can you fix it? The obvious solution is increase the amount of memory installed in the machine. There is another approach to tackle the problem. In this tutorial, we wiil see how to extend the memory via swap file.

Reproduce ‘Not enough memory’ Error

To reproduce the error, we will create a simple python script which invoked with the number of MB to allocate. If the script success to allocate the requested amount of memory – it will print success => nMB was allocated', otherwise failure => nMB can not be allocated` will be printed.


create in your favorite editor and enter the following content:

#! /usr/bin/python
import ctypes
import sys
size = int(sys.argv[1])
class MemoryTest(ctypes.Structure):
    _fields_ = [  ('chars' , ctypes.c_char*size * 1024*1024 ) ]
    test = MemoryTest()
    print('success => {0:>4}MB was allocated'.format(size) )
    print('failure => {0:>4}MB can not be allocated'.format(size) )

make the script executable:

main@work:~# chmod 755


Now , let’s use this script until a failure is reported:

main@work:~# ./ 10
success =>   10MB was allocated
main@work:~# ./ 295
success =>  295MB was allocated
main@work:~# ./ 310
failure =>  310MB can not be allocated

To see the reason why the script failed at 310MB we run free -m which displays the amount of free and used memory in the system in MB:

main@work:~# sudo free -m
            total        used        free      shared  buff/cache   available
Mem:          488         148         140           9         199         302

We can see, there is swap memory. Let’s fix it:

Adding swap

Suppose we want to add 750M of memory. Invoke the following commands:

The following command create a new file /var/swap.1 and copying 750 blocks from /dev/zero - Each block has size of 1MB (1M Bytes). Thus, this command creates a 750MB file of zeros.

main@work:~# sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=750
750+0 records in
750+0 records out
786432000 bytes (786 MB, 750 MiB) copied, 1.57251 s, 500 MB/s

As this file will be used as swap, for security reason, we need to change the permissions so only the root user can read and write to it.

main@work:~# sudo chmod 600 /var/swap.1

Now , will set up a Linux swap area on our new device (The newlly created file) and enable it so the system can start using it.

main@work:~# sudo /sbin/mkswap /var/swap.1
Setting up swapspace version 1, size = 750 MiB (786427904 bytes)
no label, UUID=a6de..
main@work:~# sudo /sbin/swapon /var/swap.1

Now , Let’s test that we extend our memory:

main@work:~# sudo free -m
              total        used        free      shared  buff/cache   available
Mem:            488         150           6           9         332         300
Swap:           749           0         749

Now, there is ~300MB free available memory and ~750MB of swap memory. Thus, we can allocate easily ~1000MB.

root@lamp:~# ./ 750
success =>  750MB was allocated
root@lamp:~# ./ 1000
success => 1000MB was allocated


This method can be used to run programs which require more memory than the installed in the machine. However, there is performance penalty as the system has to read some of memory from and write it to the slower disk device.