Can i delete this file ?

Hi,

On my linux system i find the below two files which occupy good space.

# du -sh /root/myswapfile /swap2
1.1G    /root/myswapfile
1001M   /swap2

# ls -ltr /root/myswapfile /swap2
-rw-------. 1 root root 1073741824 Jan 27  2017 /root/myswapfile
-rw-------. 1 root root 1048576000 Jan 31  2017 /swap2

Current output of top command:

# free
              total        used        free      shared  buff/cache   available
Mem:        1883724      962668       79056       11064      842000      645476
Swap:       2072568     2008324       64244
# uname -a
Linux mymac1 3.10.0-514.10.2.el7.x86_64 #1 SMP Fri Mar 3 00:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Considering my current memory usage is it safe to remove (rm) any of the two files ?

Hello mohtashims,

I am little bit confuse here, not sure why you are looking for deleting the files? When we hit top command it will show us(you could sort it by memory or cpu consumed order) which process is responsible for high cpu or memory before simply pointing few files(since they are of huge size) to delete them.

Please paste output of top command and make sure they have high memory or cpu related processes details in it, then we could try to move further at this point it is NOT clear.

Thanks,
R. Singh

Those look like something that might have been created by the mkfile utility for use as NFS mounted swap areas. (Although I would usually expect to see them with a set-uid bit set for the owner in this case.)

In any case, I would be very wary of removing two gigabytes of someone's data without knowing why it is there and what it is being used for first.

1 Like

@Don, why NFS??
Check if they are used as (local) swap files, with

cat /proc/swaps

or

swapon -s

and (to be mounted at boot)

cat /etc/fstab
1 Like

Hi MadeInGermany,
I was just quoting from the MacOS mkfile (8) man page:

NAME
       mkfile - create a file

SYNOPSIS
       mkfile [ -nv ] size[b|k|m|g] filename ...

DESCRIPTION
       mkfile  creates  one  or  more  files that are suitable for use as NFS-
       mounted swap areas.  The sticky bit is set, and the file is padded with
       zeroes  by  default.   Non-root  users  must  set  the sticky bit using
       chmod(1).  The default size unit is bytes, but the  following  suffixes
       may  be  used  to  multiply  by  the given factor: b (512), k (1024), m
       (1048576), and g (1073741824).
... ... ...

I didn't remember it being for NFS only either, but I haven't personally used mkfile since I left Sun over a decade ago.

Can you tell me if it is used and required (should not be deleted) inlight with the below output:

cat /proc/swaps
Filename                                Type            Size    Used    Priority
/root/myswapfile                        file            1048572 1041852 -1
/swap2                                  file            1023996 538916  -2

# swapon -s
Filename                                Type            Size    Used    Priority
/root/myswapfile                        file    1048572 1041852 -1
/swap2                                  file    1023996 538916  -2

# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Dec 15 22:52:03 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4402acfe-bd46-475b-b78c-cc0aef5b78f7 /                       ext4    defaults        1 1
/root/myswapfile        swap                    swap    defaults        0 0
/swap2                  swap                    swap    defaults        0 0

In my opinion, the you should change your config settings to not to mount the swap space when the system boots, then do an orderly shutdown and reboot.

Then you will be OK.

Sorry, I did not look at your system and think about how to configure it to not mount and / or use swap when it boots, but if you configure your system to not use swap when it boots, then you reboot in an orderly way, you should be OK.

I don't recommend just "deleting" that space without an orderly plan / method to first boot without the swap, and then after you are comfortable all is working as you expect, you can reclaim the disk space as you wish.

1 Like

Update, mohtashims, I thought I would Google this for you and found a great little tutorial for you on this topic::

How to Permanently Disable Swap in Linux

1 Like

So the answer is No.
You have these two swapfiles only, and they are fully used, and there is not enough RAM available to delete either of them.

Maybe you can increase your disk space?
Check with

lsblk
pvs

If a user boots a system without swap, the system will still boot and Linux will dynamically allocate memory, and is very good at it.

Yes, this could cause the system to run slower (not using swap) and thrash, but it can still be done.

So the "absolute" answer "NO" is not really accurate.

It can be done and the system will work if swap is turned off, but your performance could dive and the system thrash.

Is it a good idea? Well, that depends on the constraints the user is working under, but if it is working well now, why do it?

I have ran many Linux servers over the years with zero swap and they work fine, but I like to have a lot of memory in servers.

There is nothing wrong with experimenting; but you need to be careful of course if the system is a production system with critical apps in real or near-real time.

It does not hurt (if the system is not critical to production) to turn swap off if you really need to reclaim the disk space and for some reason don't want to add more disk space.

Having said that, both memory and disk space is pretty cheap these days, so why torture yourself about all this?

Is this a mission critical production server that you do not want to bring down to add disk space or memory?

Why not add more memory and / or disk space?

Thanks.

1 Like

The system currently needs about 3.5 GB virtual memory. Disabling swap in /etcfstab and rebooting with 2GB will work, but will not allow the applications to run like before. It depends on the applications what exactly happens.
Of course adding 2GB RAM is the best solution! Then you won't need the swapfiles. (While with zero swap I don't trust the kernel to handle all memory pressure situations well.)

Also, memory is so cheap these days, why not use add a lot of memory, 16GB total for example, and have plenty of headroom.

I don't know the criticality of the application, but for the unix.com server, I like to have double the memory the system typically requires, for those days when things go south on the net or a process hangs, etc.

top - 02:18:33 up 68 days, 54 min,  1 user,  load average: 0.76, 0.52, 0.48
Tasks: 226 total,   2 running, 160 sleeping,   0 stopped,   1 zombie
%Cpu(s):  3.3 us,  0.4 sy,  0.0 ni, 96.2 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem : 65842320 total, 27976264 free,  3082788 used, 34783268 buff/cache
KiB Swap:  7995388 total,  7995388 free,        0 used. 61988500 avail Mem 

But of course unix.com is critical infrastructure for many people on the net :slight_smile:

So, I don't like any bits to swap, if I can help it, since faster is generally better for users.