• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

swap - memory

fernandel

Well-Known Member

Thanks: 66
Messages: 451

#1
Hi!

I have FreeBSD 11.1 - Release (amd64).
I am using portmaster for building ports. The "problem" is why it use for building swap if is a lot of memory available still.
I am using GNOME 3 at this momemnt and rebuilding www/iridium and
there are 3.2 GM of 8.0 GB used and 25.7 MB of Swap. Size of the swap partition is 3.6 GB.
I am running System Monitor and used of RAM is between 3.1 GB to 3.7 GB.

Do I need to do some settings, please?
Thank you.
 

ralphbsz

Aspiring Daemon

Thanks: 316
Messages: 750

#2
You did notice that the amount of swap used is microscopically small? You have 8G of RAM, and 0.025G of swap were used. At the typical speed of a hard disk (about 100-200 MByte/s), writing that swap took less than 1/4 second.

For some reason that I don't understand, some operating systems use swap, even when they are never out of memory. OK, that' a fact of life. As long as the swap usage is tiny, it should not be a reason to worry.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 5,485
Messages: 25,656

#3
Modern operating systems will always use a bit of swap. This is a tiny amount however. Swap usage in and of itself is never a problem, it's excessive swapping that will cause performance issues.
 

Snurg

Aspiring Daemon

Thanks: 238
Messages: 684

#4
To find how much swap my pc uses atm, I did man swapctl and then tried swapctl -h according to the info there.
It only puts out the usage information. However, swapinfo -h works.
Is it me, my computer, or an incorrect man page?

Semi-OT: Regarding SirDice's point of excessive swapping, I made an interesting observation.
On Windows computers, I regularly notice much swap-in when I switch between different apps.
Even with lots of spare memory, Windows appears to swap out gigabytes, bogging down computer's performance.
This is the reason why I configure Windows usually that way that it only gets the absolute minimum possible swap partition size to avoid performance loss.
I wonder what's the reasoning behind Windows' swap strategy...
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 5,485
Messages: 25,656

#5
I did man swapctl and then tried swapctl -h according to the info there.
It only puts out the usage information.
Because you need to tell it to list something, swapctl -l -h
 

Crivens

Moderator
Staff member
Moderator

Thanks: 341
Messages: 1,191

#6
The reason the system uses swap when memory is "free" simply is that that memory is not really free. The free memory is used to cache file data, so there is some benefit from pushing unused content to the swap partition to make more cache memory available. Windowa, IIRC, does swap coloring and prefetching on a diferent level, which means it will swap out cached data from Word when Photoshop is in front to make more room for that one. So you always profit from giving the system some swap space.
 

achix

Well-Known Member

Thanks: 33
Messages: 269

#7
it's excessive swapping that will cause performance issues.
Which is exactly what is happening with FreeBSD 11.1. After I upgraded to 11.1 I noticed significant drop in performance. So I told the admin ppl to just order another 16GB for me. After I installed the extra mem, the system went OK for about 1-2 days, then it started swapping again. Basically we spent the money on the extra mem to still have a slow system.
Disabled the swap and lived happily thereafter (even if having a system not set up "officially" correct).
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 5,485
Messages: 25,656

#8
So I told the admin ppl to just order another 16GB for me. After I installed the extra mem, the system went OK for about 1-2 days, then it started swapping again. Basically we spent the money on the extra mem to still have a slow system.
I have several 11.1 machines with only 8GB. They rarely touch the swap, even after running non-stop for a month or so. So I suspect you have an application that's using up the memory. Might even have a memory leak.
 

Snurg

Aspiring Daemon

Thanks: 238
Messages: 684

#9
achix' post about turning off swap motivated me to turn off swap.

And the change in responsiveness was prompt and very noticeable.
And at this moment the workstation session was very fresh. Booted up just a few minutes ago. Free memory was 43GB at that moment!

Another interesting observation about memory usage is this:

Recently I backed up my laptop's HDD, I plugged it into the running computer, turned the drive's bay power on, imported the pool and copied the data (>200GB) onto the workstations' drives.
After that I noticed that Firefox had become extremely sluggish, had to reload every tab that was previously loaded. Like if it was started freshly. But it was a session several days old.

When I examined memory usage, I found that ZFS ARC had taken over all memory.
ARC cache had grown to 43GB, leaving only 5GB for kernel+programs.
Swap usage was little, about 80MB of 4GB swap area.

Firefox apparently had to drop all memory usage on demand of ZFS ARC, leaving it with only 57 MB!!!

Computer had become very very sluggish. I had to reboot to fix that.
(Just now, this session is very fresh, only 2 hrs. And so Firefox just now uses only 5GB for 10 windows with maybe 30, 40 tabs of >100 loaded. On long sessions it takes >10GB).

After that experience I restricted ZFS ARC usage to 9GB to avoid having to reboot after backing up things.

And... until I discovered that, I always believed Firefox were the culprit... I then restarted it. But I hate to restart it, because all tabs then need to be reloaded when I access them.
 

achix

Well-Known Member

Thanks: 33
Messages: 269

#10
I have several 11.1 machines with only 8GB. They rarely touch the swap, even after running non-stop for a month or so. So I suspect you have an application that's using up the memory. Might even have a memory leak.
Will have to let the system run for a while and let you know. For the moment it is running better than ever, with everything up (both bhyve guests, jboss, eclipse, postgresql , KDE, thunderbird, lots of konsoles, firefox windows, etc) and having swapoff'ed the swap disk. I have no ZFS.

Currently top shows :

last pid: 10099; load averages: 0.52, 0.51, 0.49 up 29+23:13:44 10:51:50
268 processes: 1 running, 267 sleeping
CPU: % user, % nice, % system, % interrupt, % idle
Mem: 2703M Active, 25G Inact, 3186M Wired, 1436M Buf, 558M Free
Swap:


When swap was on, it went to a point of using it all, while still having lots of Inactive memory. Would love to enable back swap , let it run and report back, but this would mean killing all services to free mem and then disable it again, and today is not a good day to do so.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 5,485
Messages: 25,656

#11
but this would mean killing all services to free mem and then disable it again, and today is not a good day to do so.
That's quite understandable.

Machines haven't been running long, a few days now, had to reboot for updates. But this is my firewall, has 4GB, but not much running on it:
Code:
last pid: 54876;  load averages:  0.01,  0.00,  0.00                                                       up 10+07:09:28  10:40:45
67 processes:  2 running, 64 sleeping, 1 waiting
CPU 0:  0.0% user,  0.0% nice,  0.4% system,  0.8% interrupt, 98.8% idle
CPU 1:  0.0% user,  0.0% nice,  1.6% system,  0.4% interrupt, 98.0% idle
Mem: 21M Active, 109M Inact, 2304M Wired, 1217M Free
ARC: 1043M Total, 481M MFU, 274M MRU, 32K Anon, 14M Header, 273M Other
     518M Compressed, 873M Uncompressed, 1.69:1 Ratio
Swap: 8192M Total, 8192M Free
Another machine, 16GB (recently upgraded from 8GB):
Code:
last pid: 38497;  load averages:  0.17,  0.24,  0.23                                                        up 4+19:42:10  10:42:15
93 processes:  2 running, 90 sleeping, 1 waiting
CPU 0:  2.4% user,  0.0% nice,  0.8% system,  0.4% interrupt, 96.5% idle
CPU 1:  3.1% user,  0.0% nice,  1.2% system,  0.0% interrupt, 95.7% idle
CPU 2:  2.7% user,  0.0% nice,  2.0% system,  0.0% interrupt, 95.3% idle
CPU 3:  1.9% user,  0.0% nice,  1.6% system,  0.0% interrupt, 96.5% idle
Mem: 2115M Active, 700M Inact, 29M Laundry, 12G Wired, 730M Free
ARC: 8182M Total, 2955M MFU, 4567M MRU, 256K Anon, 101M Header, 558M Other
     7103M Compressed, 8067M Uncompressed, 1.14:1 Ratio
Swap: 16G Total, 755M Used, 15G Free, 4% Inuse
There is some swap usage on this machine, I suspect it's because of Java running on it. Swap is used but there's very little going in or out so it's not problem and doesn't affect performance. This machine does most of the work on my home network, torrents, Samba, a Java app, a jail and a bhyve VM. I also use this machine to build packages with poudriere on it.

One last example:
Code:
last pid:   635;  load averages:  0.68,  0.73,  0.61                                                        up 4+19:20:04  10:50:19
107 processes: 2 running, 104 sleeping, 1 waiting
CPU 0:  0.8% user,  0.0% nice,  0.4% system,  0.0% interrupt, 98.9% idle
CPU 1:  0.8% user,  0.0% nice,  1.1% system,  0.0% interrupt, 98.1% idle
CPU 2:  1.1% user,  0.0% nice,  0.0% system,  0.4% interrupt, 98.5% idle
CPU 3:  1.5% user,  0.0% nice,  0.0% system,  0.0% interrupt, 98.5% idle
Mem: 185M Active, 314M Inact, 1632M Wired, 5796M Free
ARC: 1161M Total, 322M MFU, 761M MRU, 23M Anon, 7813K Header, 47M Other
     983M Compressed, 1972M Uncompressed, 2.01:1 Ratio
Swap: 2048M Total, 2048M Free
This is a bhyve VM, 4 cores, 8GB memory. It runs Zabbix 3.4.
 

Minbari

Active Member

Thanks: 70
Messages: 166

#13
My machines are swapless (ram 8GB, fs=ZFS) for years and everything is working ok considering the fact that I'm always building every program from sources. Check to see if u don't have a memory leak.

Code:
last pid: 49408;  load averages:  0.30,  0.49,  1.01                                                                                   up 0+09:34:26  12:48:56
50 processes:  4 running, 46 sleeping
CPU:  5.3% user,  0.0% nice,  1.4% system,  0.0% interrupt, 93.2% idle
Mem: 577M Active, 244M Inact, 86M Laundry, 2176M Wired, 4675M Free
ARC: 1112M Total, 182M MFU, 753M MRU, 16K Anon, 11M Header, 165M Other
     689M Compressed, 1511M Uncompressed, 2.19:1 Ratio
Swap:
 

achix

Well-Known Member

Thanks: 33
Messages: 269

#14
Reverted back to using swap, but turned off kern.ipc.shm_use_phys . Seems ok since I did it 1-2 hours ago. I noticed some minimal swap used at the first minutes, but then went to zero. Will keep an eye to it.
 

Snurg

Aspiring Daemon

Thanks: 238
Messages: 684

#15
Like Minbari reported his experiences, I had for years FreeBSD (older versions) running on an 8GB machine with no swap partitions, never experienced any non-obvious problem.

It was about 3 years ago when I decided to have the system use the default swap allocated by the installer, because (almost) all people said one should not disable swap.

For my part, I will not experiment any more in that direction.
With me, swap is gone forever. Not going to activate it again.
(I don't care about being perceived as heretic.)
 

Nicola Mingotti

Member

Thanks: 44
Messages: 89

#16
Guys, I disabled the swap, my FreeBSD-11.1 running inside a VMWare (1 core) on a 2009 Macbook is now fast and furious !!! :) ... I know there will be drawbacks but I want to enjoy the moment now:)

BTW, I think there are memory problems in Firefox (package version). Memory free does not increas when I close tabs or windows, only when I close all Firefox windows !
 

Snurg

Aspiring Daemon

Thanks: 238
Messages: 684

#17
Memory free does not increas when I close tabs or windows, only when I close all Firefox windows !
My impression is that FF invokes its garbage collection only when free system memory decreases below some threshold.

This is why one can observe FF's memory usage shrinking when free system memory gets lower, even without closing any window/tab, up to the point that it keeps in memory only the topmost active window/tab.
It also would explain its behavior described in my post #9 above, when it released a dozen gigabytes of memory back to the system, as said without closing any window/tab, only to satisfy ZFS ARC's desire to gobble up every byte of RAM it can get hold of.

Conclusion:
Probably every desktop user can profit by restricting ZFS memory usage to reasonable limits, and prevent shared memory being swapped out, either by a) completely disabling swap or b) changing kern.ipc.shm_use_phys as achix suggested. Method a) has proven to work reliably, method b) still needs long-term verification.
 

dlegrand

Active Member

Thanks: 42
Messages: 148

#18
Method a) has proven to work reliably.
Not for me: under high memory pressure (like building packages with poudriere), the kernel kills some process to get free memory. So some important process could be suddenly killed. That's not the case with enough swap space. That's why I now have 12G of swap which is used at almost 30-40% when building packages with poudriere while using my desktop computer.
 

Eric A. Borisch

Well-Known Member

Thanks: 158
Messages: 253

#19
Reverted back to using swap, but turned off kern.ipc.shm_use_phys . Seems ok since I did it 1-2 hours ago. I noticed some minimal swap used at the first minutes, but then went to zero. Will keep an eye to it.
To be clear, you returned kern.ipc.shm_use_phys to its default (0) state and things worked better?
 

Snurg

Aspiring Daemon

Thanks: 238
Messages: 684

#20
Not for me: under high memory pressure (like building packages with poudriere), the kernel kills some process to get free memory. So some important process could be suddenly killed. That's not the case with enough swap space. That's why I now have 12G of swap which is used at almost 30-40% when building packages with poudriere while using my desktop computer.
May I ask how much memory your build box has total, and how much was free when starting poudriere?

Edit: I wonder if it might be sensible to allocate the kernel a generous minimum of reserved memory? (vm.kmem_size="...", vm.kmem_size_max="...")
 

topcat

Active Member

Thanks: 23
Messages: 117

#21
I don't have this problem on any of my 11.1 machines, and they have been up for weeks (only reboots for kernel updates). I run Firefox, Chrome with hundreds of tabs open, with 16 GB total RAM.

I wonder if something is touching a lot of pages in RAM quickly. This should result in a big inactive queue, and will also sometimes swap stuff out. It's probably reading and not writing, because you don't seem to have much in the laundry queue. systat -vmstat should be interesting to observe with swap on. Look at laundry list, soft and hard page faults.

This is a perfect opportunity to learn how the memory manager works :)
 

dlegrand

Active Member

Thanks: 42
Messages: 148

#22
May I ask how much memory your build box has total, and how much was free when starting poudriere?

Edit: I wonder if it might be sensible to allocate the kernel a generous minimum of reserved memory? (vm.kmem_size="...", vm.kmem_size_max="...")
It's a 6 cores CPU (FX6300) with 8Gb memory and separate graphic board with its own 1Gb memory. It's my desktop machine so I am running Xorg, Xfce, Firefox, Emacs, before launching poudriere. I am doing parallel build with sometimes 6 big builds like libreoffice, thunderbird, firefox ...
That's why sleeping processes are going to swap space, but my desktop is still responsive with running processes. I am happy like that, FreeBSD works great for me.
Code:
last pid:  1309;  load averages:  0.40,  0.31,  0.26                                                    up 0+01:23:10  09:01:30
87 processes:  1 running, 86 sleeping
CPU:  0.2% user,  0.0% nice,  0.2% system,  0.2% interrupt, 99.4% idle
Mem: 1105M Active, 712M Inact, 69M Laundry, 2542M Wired, 3474M Free
ARC: 1885M Total, 587M MFU, 1224M MRU, 316K Anon, 12M Header, 62M Other
     1534M Compressed, 3204M Uncompressed, 2.09:1 Ratio
Swap: 12G Total, 12G Free
 

achix

Well-Known Member

Thanks: 33
Messages: 269

#23
To be clear, you returned kern.ipc.shm_use_phys to its default (0) state and things worked better?
it seemed so. I just logged in my workstation, remotely, and now it see it is using some 1.7GB swap out of the total 8GB. Not quite promising. All sysctl settings are back to default btw. I'll revert back in Monday, after some real usage.