How to shut off swap and prevent thrashing?

My many pardons as this should be a common question, but my Google-fu is failing me entirely today.

I have OpenSUSE 13.1 64-bit installed on a machine with 8GB of RAM and no swap file or partition. When I run out of memory, I want the next memory request to fail and the program that requested it to segfault. (Yes, I know this can lead to data loss.)

Currently, when the memory usage gets to 100%, the computer starts thrashing (HDD light is solid-on and the computer is non-responsive, except for a jerky mouse pointer). This forces me to use the reset button and I lose everything I was working on. It appears that there is still some sort of swap memory that the system is accessing.

How can I prevent any sort of swap/thrashing behavior?

My /proc/meminfo:

MemTotal:        8135200 kB
MemFree:         4167204 kB
Buffers:           73528 kB
Cached:           562188 kB
SwapCached:            0 kB
Active:          1144316 kB
Inactive:         491956 kB
Active(anon):    1002268 kB
Inactive(anon):    30856 kB
Active(file):     142048 kB
Inactive(file):   461100 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:               172 kB
Writeback:             0 kB
AnonPages:       1000712 kB
Mapped:          2346612 kB
Shmem:             32576 kB
Slab:              85388 kB
SReclaimable:      33192 kB
SUnreclaim:        52196 kB
KernelStack:        2960 kB
PageTables:        27084 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     4067600 kB
Committed_AS:    5622968 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      329656 kB
VmallocChunk:   34359405976 kB
HardwareCorrupted:     0 kB
AnonHugePages:    104448 kB                                                                                                                                                                                     
HugePages_Total:       0                                                                                                                                                                                        
HugePages_Free:        0                                                                                                                                                                                        
HugePages_Rsvd:        0                                                                                                                                                                                        
HugePages_Surp:        0                                                                                                                                                                                        
Hugepagesize:       2048 kB                                                                                                                                                                                     
DirectMap4k:      117412 kB
DirectMap2M:     2971648 kB
DirectMap1G:     5242880 kB

Hi
Install more RAM…?

You can tune swap, I run mine as follows on my machines, all have 8GB of RAM, but do have swap as well;


/etc/sysctl.conf
# See https://wiki.archlinux.org/index.php/Solid_State_Drives#Swap_Space_on_SSDs
vm.swappiness=1
vm.vfs_cache_pressure=50

What about monitoring the ram usage and create a tmp swap file via a script and maybe a notification pop up?

There is zram as well, I have a systemd service for this on OBS;
http://software.opensuse.org/package/systemd-zram-service

On 2014-07-10 15:36, quantamm wrote:
>
> My many pardons as this should be a common question, but my Google-fu is
> failing me entirely today.
>
> I have OpenSUSE 13.1 64-bit installed on a machine with 8GB of RAM and
> no swap file or partition. When I run out of memory, I want the next
> memory request to fail and the program that requested it to segfault.
> (Yes, I know this can lead to data loss.)

I don’t know if that is possible.

> Currently, when the memory usage gets to 100%, the computer starts
> thrashing (HDD light is solid-on and the computer is non-responsive,
> except for a jerky mouse pointer). This forces me to use the reset
> button and I lose everything I was working on. It appears that there is
> still some sort of swap memory that the system is accessing.
>
> How can I prevent any sort of swap/thrashing behavior?

Ad suitable swap space, or add more RAM, or use fewer programs.
You can limit how much ram a program is allowed to use, or it is killed.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

On 07/10/2014 07:36 AM, quantamm wrote:
>
> How can I prevent any sort of swap/thrashing behavior?

Simple enough: Delete any swap partitions or swap files from the hard
drive. With no swap area, nothing will ever swap.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

Monitoring, running fewer programs, and installing more RAM really aren’t options. Sometimes LibreOffice or Firefox will just start eating up RAM with little warning. Monitoring or more RAM would just give me extra time to identify the problem, and running fewer programs won’t help if the only thing I’m running is FF and it decides it’s time to go nuts.

There is no swap space on the system (see the /proc/meminfo I include in my OP). zRAM is an interesting idea, but that just prevents thrashing, it doesn’t stop it from happening.

If I had to guess what’s happening, I’d say that even without a swap, the kernel still reserves some disk space for paging. This seems like the sort of thing that there should be a kernel option to change the behavior of.

What is the rational for no swap??

> If I had to guess what’s happening, I’d say that even without a swap,
> the kernel still reserves some disk space for paging. This seems like
> the sort of thing that there should be a kernel option to change the
> behavior of.

I’m highly skeptical that this is happening, at least in the way you
describe. The purpose of swap is to give the kernel the ability to use
virtual memory on some kind of non-RAM, so since you have no swap
currently I would bet that applications are causing the thrashing to avoid
dying. Applications can write their temporary data anywhere, and can
choose how often to flush data to disk, so I’d start by checking which
applications are doing tons of I/O when things go bad, and then kill them.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

On 2014-07-10 19:52, ab wrote:

>> If I had to guess what’s happening, I’d say that even without a swap,
>> the kernel still reserves some disk space for paging. This seems like
>> the sort of thing that there should be a kernel option to change the
>> behavior of.
>
> I’m highly skeptical that this is happening, at least in the way you
> describe. The purpose of swap is to give the kernel the ability to use
> virtual memory on some kind of non-RAM, so since you have no swap
> currently I would bet that applications are causing the thrashing to avoid
> dying. Applications can write their temporary data anywhere, and can
> choose how often to flush data to disk, so I’d start by checking which
> applications are doing tons of I/O when things go bad, and then kill them.

Also, the first thing the kernel sacrifices when there is little ram are
buffers and cache; with these out, when programs want to read things
from disk, and these are not cached, they have to actually be read from
disk. Ie, by not having swap to avoid slow disk activity, you actually
cause disk activity, and make your system slower.

Frankly, I do not see the rationale for not having swap.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

On 07/10/2014 12:52 PM, ab wrote:
>> If I had to guess what’s happening, I’d say that even without a swap,
>> the kernel still reserves some disk space for paging. This seems like
>> the sort of thing that there should be a kernel option to change the
>> behavior of.
>
> I’m highly skeptical that this is happening, at least in the way you
> describe. The purpose of swap is to give the kernel the ability to use
> virtual memory on some kind of non-RAM, so since you have no swap
> currently I would bet that applications are causing the thrashing to avoid
> dying. Applications can write their temporary data anywhere, and can
> choose how often to flush data to disk, so I’d start by checking which
> applications are doing tons of I/O when things go bad, and then kill them.

When you run out of physical memory and have no swap space, one of the immediate
consequences is that every system component that tries to allocate memory
will, of course, fail. In particular, you will not be able to allocate space for
a standard kernel buffer (skb). These skb’s are used for nearly every
communication within the kernel, and are used heavily in the networking stack
and drivers.

Each memory allocation failure is logged, and the most probable cause of your
system sluggishness is the time taken to log these failed attempts. I am
reasonably certain that the out-of-memory killer would eventually be able to
recover by killing the processes that use the most memory, but that might take a
long time.

Unless you are running a system where memory usage is tightly controlled, which
eliminates any system running a browser, or you have massive amounts of RAM,
Linux is designed to have swap available. As your system cannot satisfy either
condition, you need to create some swap space. You can create a swap file and
avoid the necessity of creating a new partition.

On 2014-07-10 20:16, Larry Finger wrote:

> I am reasonably certain that the out-of-memory killer would eventually
> be able to recover by killing the processes that use the most memory,
> but that might take a long time.

But often it kills the most inadequate process, one that you are
actively using and has unsaved work. Other times it kills some
absolutely necessary system process, causing such instability that
brings the entire system down, or forces you to reboot to recover.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

On Thu, 10 Jul 2014 18:13:06 GMT, “Carlos E. R.”
<robin_listas@no-mx.forums.opensuse.org> wrote:

>On 2014-07-10 19:52, ab wrote:
>
>>> If I had to guess what’s happening, I’d say that even without a swap,
>>> the kernel still reserves some disk space for paging. This seems like
>>> the sort of thing that there should be a kernel option to change the
>>> behavior of.
>>
>> I’m highly skeptical that this is happening, at least in the way you
>> describe. The purpose of swap is to give the kernel the ability to use
>> virtual memory on some kind of non-RAM, so since you have no swap
>> currently I would bet that applications are causing the thrashing to avoid
>> dying. Applications can write their temporary data anywhere, and can
>> choose how often to flush data to disk, so I’d start by checking which
>> applications are doing tons of I/O when things go bad, and then kill them.
>
>Also, the first thing the kernel sacrifices when there is little ram are
>buffers and cache; with these out, when programs want to read things
>from disk, and these are not cached, they have to actually be read from
>disk. Ie, by not having swap to avoid slow disk activity, you actually
>cause disk activity, and make your system slower.
>
>Frankly, I do not see the rationale for not having swap.

Well for starters i build my systems with lots of RAM, 8 GB or more.
Second swap activity is disk activity and correspondingly slow. No swap
and keep the number and kind of programs down and viola fast, responsive
systems. I open and close firefox and other browsers at need and they are
normally not kept running. Same for other programs known to have memory
leaks. I am also starting an experiment with swap on an SSD. It may be
worthwhile do use a small SSD for swap exclusively to keep system speed
up.

?-)

observation

memory/swap problems occur if the available unused memory on the root partition,
‘/’, is < 2GB

what’s the output of cmd df?

On 2014-07-13 07:30, josephkk wrote:
> On Thu, 10 Jul 2014 18:13:06 GMT, “Carlos E. R.” <> wrote:

>> Frankly, I do not see the rationale for not having swap.
>
>
> Well for starters i build my systems with lots of RAM, 8 GB or more.
> Second swap activity is disk activity and correspondingly slow. No swap
> and keep the number and kind of programs down and viola fast, responsive
> systems.

No, that’s not really true. It’s a common misconception.

  1. For the same amount of installed RAM, a computer is faster with swap
    space available that without, with the same workload.
  2. If instead of creating that swap space, you add RAM instead, then
    that is faster.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

On 2014-07-13 07:30, josephkk wrote:

> I am also starting an experiment with swap on an SSD. It may be
> worthwhile do use a small SSD for swap exclusively to keep system speed
> up.

I’m not sure that swap on SSD is faster than rotating disks. You need a
media that has fast write speed, sustained write speed. Typically flash
media has slow sustained write speed, so check it up.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

On Sun, 13 Jul 2014 09:46:01 GMT, keellambert
<keellambert@no-mx.forums.opensuse.org> wrote:

>
>observation
>
>memory/swap problems occur if the available unused memory on the root
>partition,
>’/’, is < 2GB
>
>what’s the output of cmd df?

openSUSE 12.3 KDE3 Not much more than a bare install at this time.


-----------------------------
joseph2k@Hopper:~> df
Filesystem     1K-blocks    Used Available Use% Mounted on
rootfs          41283648 4193140  36041884  11% /
devtmpfs         8184096      36   8184060   1% /dev
tmpfs            8223408       0   8223408   0% /dev/shm
tmpfs            8223408     648   8222760   1% /run
/dev/sda1       41283648 4193140  36041884  11% /
tmpfs            8223408       0   8223408   0% /sys/fs/cgroup
tmpfs            8223408       0   8223408   0% /media
tmpfs            8223408     648   8222760   1% /var/lock
tmpfs            8223408     648   8222760   1% /var/run
/dev/sda3       73514232 1389344  68390568   2% /home
joseph2k@Hopper:~>
-----------------------------

and 



joseph2k@Hopper:~> top
top - 17:19:52 up 20 min, 2 users, load average: 0.01, 0.04, 0.05
Tasks: 143 total, 1 running, 142 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.1%sy, 0.0%ni, 99.9%id, 0.0%wa, 0.0%hi, 0.0%si,
0.0%st
Mem: 16061M total, 997M used, 15063M free, 36M buffers
Swap: 8196M total, 0M used, 8196M free, 575M cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6427 joseph2k 20 0 8860 1196 872 R 0 0.0 0:00.23 top
1 root 20 0 37296 4612 2008 S 0 0.0 0:01.12 systemd
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/0
4 root 20 0 0 0 0 S 0 0.0 0:00.38 kworker/0:0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0
7 root -2 19 0 0 0 S 0 0.0 0:00.11 rcuc0
8 root RT 0 0 0 0 S 0 0.0 0:00.00 rcun0
9 root -2 0 0 0 0 S 0 0.0 0:00.00 rcub0
10 root RT 0 0 0 0 S 0 0.0 0:00.00 rcun1
11 root -2 0 0 0 0 S 0 0.0 0:00.00 rcub1
12 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/0
13 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/1
14 root 20 0 0 0 0 S 0 0.0 0:00.06 kworker/1:0
15 root -2 19 0 0 0 S 0 0.0 0:00.11 rcuc1
16 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/1
18 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/1
19 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/2
21 root -2 19 0 0 0 S 0 0.0 0:00.06 rcuc2
22 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/2
23 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/2
24 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/3
26 root -2 19 0 0 0 S 0 0.0 0:00.06 rcuc3
27 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/3
28 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/3
29 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/4
31 root -2 19 0 0 0 S 0 0.0 0:00.05 rcuc4
32 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/4
33 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/4
34 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/5
36 root -2 19 0 0 0 S 0 0.0 0:00.07 rcuc5
37 root 20 0 0 0 0 S 0 0.0 0:00.00 ksoftirqd/5
38 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/5
39 root 0 -20 0 0 0 S 0 0.0 0:00.00 cpuset
40 root 0 -20 0 0 0 S 0 0.0 0:00.00 khelper
41 root 20 0 0 0 0 S 0 0.0 0:00.00 kdevtmpfs
42 root 0 -20 0 0 0 S 0 0.0 0:00.00 netns
43 root 20 0 0 0 0 S 0 0.0 0:00.00 sync_supers
joseph2k@Hopper:~>

Booted hopper up just give this information.

?-)

On Sun, 13 Jul 2014 11:23:04 GMT, “Carlos E. R.”
<robin_listas@no-mx.forums.opensuse.org> wrote:

>On 2014-07-13 07:30, josephkk wrote:
>
>> I am also starting an experiment with swap on an SSD. It may be
>> worthwhile do use a small SSD for swap exclusively to keep system speed
>> up.
>
>I’m not sure that swap on SSD is faster than rotating disks. You need a
>media that has fast write speed, sustained write speed. Typically flash
>media has slow sustained write speed, so check it up.


-------------------
Learn more about the Crucial CT128M4SSD2
General

Brand
Crucial

Series
M4

Model
CT128M4SSD2

Device Type
Internal Solid State Drive (SSD)

Used For
Consumer


Performance

Sustained Sequential Read
Up to 500MBps (SATA 6Gbs); 265MBps (SATA 3Gbs)

Sustained Sequential Write
Up to 175MBps (SATA 6Gbps)

4KB Random Read
Up to 45,000 IOPS

4KB Random Write
Up to 35,000 IOPS

MTBF
1,200,000 hours

-------------------

Specifcations from manufacturer.

I think that will eat any rotating disk alive.

Remember, microsecond seek times instead of millisecond, and no rotational
latency.

?-)

Hi
For my OCZ Vertex4 on this system (systemd-analyze total 10.168s);

  • Up to 560 MB/s Sequential Read
  • Up to 510 MB/s Sequential Write
  • Up to 95,000 IOPS 4k Random Read
  • Up to 85,000 IOPS 4k Random Write

I have a CT120M500SSD1 in the other HP system (systemd-analyze total 12.475s);

  • Up to 500MB/s Sequential Read
  • Up to 130MB/s Sequential Write
  • Up to 62,000 IOPS 4k Random Read
  • Up to 35,000 IOPS 4k Random Write

On 2014-07-14 04:26, malcolmlewis wrote:
>
> Hi
> For my OCZ Vertex4 on this system (systemd-analyze total 10.168s);
>
> - Up to 560 MB/s Sequential Read
> - Up to 510 MB/s Sequential Write
> - Up to 95,000 IOPS 4k Random Read
> - Up to 85,000 IOPS 4k Random Write
>
> I have a CT120M500SSD1 in the other HP system (systemd-analyze total
> 12.475s);
>
> - Up to 500MB/s Sequential Read
> - Up to 130MB/s Sequential Write
> - Up to 62,000 IOPS 4k Random Read
> - Up to 35,000 IOPS 4k Random Write

You do see that the write speed varies. On both disks it is a bit slower
than read, and on the second unit it is half the value than on the first
unit, so there is important variance between models. Also the
manufacturer phraseology I see, both in your answer and josephkk’s,
about “up to…”, makes me suspicious.

Me, I would do a write test. Something of this kind:


time dd if=/dev/zero of=file_on_ssd_disk bs=100MB count=30

which creates a 3 GB file; at 100MB/s, it should take just 30 seconds.

On my rotating, magnetic disk:


cer@Telcontar:~> time dd if=/dev/zero of=file_on_ssd_disk bs=100MB count=30
30+0 records in
30+0 records out
3000000000 bytes (3,0 GB) copied, 12,9728 s, 231 MB/s

real    0m13.020s
user    0m0.002s
sys     0m3.370s
cer@Telcontar:~> ls -lh file_on_ssd_disk
-rw-r--r-- 1 cer users 2,8G Jul 14 05:56 file_on_ssd_disk
cer@Telcontar:~>

It takes just 13 seconds, doing 231 MB/s sustained real write speed. A
second run, with “conv=fdatasync” to force real hardware write, not
cache, takes 17.8 seconds and reports 173 MB/s.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

To the OP:
You might want to take a look at what I wrote about using the Free tool to understand how your memory is being used
http://en.opensuse.org/User:Tsu2/free_tool

You should understand how much memory really is free and available, how much might be locked up in buffers.
And, you should understand that like any modern OS, Linux (not just openSUSE) will cache plenty of data in buffers in anticipation you will re-open apps you’ve closed.

But, if you intend to do a major change in your workload (different apps) and don’t intend to re-open big apps you’ve closed, then I include in my write-up a command that clears your memory buffers and cache without rebooting, freeing substantial resources for the new apps you want to open. I create a script using the command and place a shortcut on my desktop where clearing my memory is only a quick click away.

TSU

On Mon 14 Jul 2014 04:05:03 AM CDT, Carlos E. R. wrote:

On 2014-07-14 04:26, malcolmlewis wrote:
>
> Hi
> For my OCZ Vertex4 on this system (systemd-analyze total 10.168s);
>
> - Up to 560 MB/s Sequential Read
> - Up to 510 MB/s Sequential Write
> - Up to 95,000 IOPS 4k Random Read
> - Up to 85,000 IOPS 4k Random Write
>
> I have a CT120M500SSD1 in the other HP system (systemd-analyze total
> 12.475s);
>
> - Up to 500MB/s Sequential Read
> - Up to 130MB/s Sequential Write
> - Up to 62,000 IOPS 4k Random Read
> - Up to 35,000 IOPS 4k Random Write

You do see that the write speed varies. On both disks it is a bit slower
than read, and on the second unit it is half the value than on the first
unit, so there is important variance between models. Also the
manufacturer phraseology I see, both in your answer and josephkk’s,
about “up to…”, makes me suspicious.

Me, I would do a write test. Something of this kind:

time dd if=/dev/zero of=file_on_ssd_disk bs=100MB count=30

which creates a 3 GB file; at 100MB/s, it should take just 30 seconds.

On my rotating, magnetic disk:

cer@Telcontar:~> time dd if=/dev/zero of=file_on_ssd_disk bs=100MB
count=30 30+0 records in
30+0 records out
3000000000 bytes (3,0 GB) copied, 12,9728 s, 231 MB/s

real 0m13.020s
user 0m0.002s
sys 0m3.370s
cer@Telcontar:~> ls -lh file_on_ssd_disk
-rw-r–r-- 1 cer users 2,8G Jul 14 05:56 file_on_ssd_disk
cer@Telcontar:~>

It takes just 13 seconds, doing 231 MB/s sustained real write speed. A
second run, with “conv=fdatasync” to force real hardware write, not
cache, takes 17.8 seconds and reports 173 MB/s.

Hi
The OCZ;


time dd if=/dev/zero of=file_on_ssd_disk bs=100MB count=30
30+0 records in
30+0 records out
3000000000 bytes (3.0 GB) copied, 6.4224 s, 467 MB/s

real	0m6.425s
user	0m0.002s
sys	0m1.950s

ls -lh file_on_ssd_disk
-rw-r--r-- 1 malcolml users 2.8G Jul 13 23:47 file_on_ssd_disk

time dd if=/dev/zero of=file_on_ssd_disk conv=fdatasync bs=100MB count=30
30+0 records in
30+0 records out
3000000000 bytes (3.0 GB) copied, 11.6565 s, 257 MB/s

real	0m12.455s
user	0m0.003s
sys	0m2.986s

For the Crucial ssd


time dd if=/dev/zero of=file_on_ssd_disk bs=100MB count=30
30+0 records in
30+0 records out
3000000000 bytes (3.0 GB) copied, 12.7861 s, 235 MB/s

real	0m12.790s
user	0m0.000s
sys	0m1.936s

ls -lh file_on_ssd_disk
-rw-r--r-- 1 malcolml users 2.8G Jul 13 23:50 file_on_ssd_disk

time dd if=/dev/zero of=file_on_ssd_disk conv=fdatasync bs=100MB count=30
30+0 records in
30+0 records out
3000000000 bytes (3.0 GB) copied, 21.3061 s, 141 MB/s

real	0m22.038s
user	0m0.000s
sys	0m3.068s


Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.1 Kernel 3.11.10-17-desktop
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!