flush out swap

Place to discuss Fedora and/or Red Hat
Post Reply
User avatar
cdhgold
administrator
administrator
Posts: 382
Joined: Tue Mar 18, 2003 6:11 pm
Location: Texas

flush out swap

Post by cdhgold » Tue Jul 10, 2007 1:25 pm

what is a command that will flush out the swap usage without rebooting?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Jul 10, 2007 1:51 pm

I just disable and re-enable swap when I need to do it (although I can't think off the top of my head why I ever needed to do it, but I know I have):

# swapoff -a; swapon -a

insomnia
programmer
programmer
Posts: 123
Joined: Thu Feb 05, 2004 6:58 pm
Location: Belgium, Antwerp

Post by insomnia » Tue Jul 10, 2007 7:52 pm

Void Main wrote:I just disable and re-enable swap when I need to do it (although I can't think off the top of my head why I ever needed to do it, but I know I have):

# swapoff -a; swapon -a
Isn't that huge stability risk?
I always assumed the main reasons for using swap are:
1. a safety buffer when you run out of physical memory.
2. needed to clean your physical memory to avoid nr.1

EDIT: Oops, it seems I misunderstood the original question ;)
I never knew you could do this like that...

PS: Just wandering, what would happen if run out of memory without having any swap? (Will the kernel handle this?)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Jul 10, 2007 8:14 pm

insomnia wrote:
Void Main wrote:I just disable and re-enable swap when I need to do it (although I can't think off the top of my head why I ever needed to do it, but I know I have):

# swapoff -a; swapon -a
Isn't that huge stability risk?
I always assumed the main reasons for using swap are:
1. a safety buffer when you run out of physical memory.
2. needed to clean your physical memory to avoid nr.1

EDIT: Oops, it seems I misunderstood the original question ;)
I never knew you could do this like that...
When you do a swapoff -a it will first move everything from swap back into real memory (assuming you actually have enough real memory to hold it).
PS: Just wandering, what would happen if run out of memory without having any swap? (Will the kernel handle this?)
The same thing that would happen if you run out of memory when you do have swap, it'll just happen a lot faster. Try it and see:

$ while true; do firefox &; done

:)

Master of Reality
guru
guru
Posts: 562
Joined: Thu Jan 09, 2003 8:25 pm

Post by Master of Reality » Tue Jul 10, 2007 8:46 pm

PS: Just wandering, what would happen if run out of memory without having any swap? (Will the kernel handle this?)
everything slows down (real choppy like), then programs that take the most ram will suddenly be killed. and if you're lucky X will completely freeze, and then if you really didnt have enough physical RAM for the kernel i'm guessing linux itself would freeze?

Of course even 256MB seems to be enough to run X, KDE, Firefox, konqueror, ssh, sambad, and whatever other routine things i have running without swap. But when i tried to open too many browser windows firefox would close, and sometime X would freeze. This was when Kubuntu stopped mounting my swap for some reason.[/quote]

insomnia
programmer
programmer
Posts: 123
Joined: Thu Feb 05, 2004 6:58 pm
Location: Belgium, Antwerp

Post by insomnia » Thu Jul 12, 2007 9:20 pm

Master of Reality wrote: everything slows down (real choppy like), then programs that take the most ram will suddenly be killed. and if you're lucky X will completely freeze, and then if you really didnt have enough physical RAM for the kernel i'm guessing linux itself would freeze?
Do correct me if I'm wrong, but isn't this "slow down, choppy like behavior" simply caused by the swapping in the first place (that's what I meant with "a safety buffer"). At least it shows you're running out of memory.

Thing is, I just tried this on my main system and it seems to be impossible to crash anything with enough swap mounted (or should I try even harder?). The system just becomes unusable till most swapping is done, but it didn't crash(or terminate) anything...

Not using swap does seem to give a performance boost for some things (bloated 3D games like Doom3), but it does do unwanted terminates.

So... I still don't understand what the kernel is actually doing...
What process does it actually kill (that is without using swap)?
...and is it safe to rely on this?
The latest, the biggest, the least needed(as swap does) ...?

Void Main wrote:When you do a swapoff -a it will first move everything from swap back into real memory (assuming you actually have enough real memory to hold it).
Thanks.
I never knew you could do that :)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Thu Jul 12, 2007 9:55 pm

I have actually run into this many times on Linux and UNIX (and on Windows years ago when I actually occasionally used it). When I run into the problem is usually because of a program with a memory leak that would slowly consume more an more memory.

That's not always the case though. In fact I was having an issue with the Void server locking up every couple of weeks recently and it was because of a compounding problem. I am running Cacti on the Void web server to graph some things including weather in my neighborhood. I have a script that scrapes accuweather's web site every 5 minutes when the Cacti poller runs to gather weather data. Well, if my network connection goes down (like it does a lot with Charter) then this script would just hang (bad programming on my part). So, while the network was down another copy of this script would run and hang every 5 minutes.

Over time there would be so many scripts running that it eventually consumed all of the memory. When you don't have any more memory new programs can not be run and programs that are already running can't allocate more memory. This of course means you probably won't be able to log on or ssh in because a new login/ssh process can not be started so you pretty much have to push the reset button at this point.

Of course if you have your "limits.conf" configured properly (/etc/security/limits.conf on RedHat/Fedora and many other systems) a normal user would only be able to allocate a maximum of 'x' amount of memory and run a maximum of 'x' number of processes. If I had this set up then the user that my Cacti processes run under wouldn't have been able to use up all the memory and I would have been able to log into the system and fix it.

The Void server is sort of low on my priority list so I let this go for a while before actually digging into it to see what the problem was and when it happened I would just call home and have one of my kids push the reset button. It's fixed now but you can see exactly when this happened by the following graphs.

Here you can see when the number of processes had built up:

Image

Here you should see a correlation of memory being eaten up until there was no swap left and the system came to a screeching halt:

Image

Here's one of the weather graphs that I was creating with the problem script:

Image

See the man page for limits.conf:

http://voidmain.is-a-geek.net/man?param ... f&mode=man

That default Cacti memory graph pretty much sucks. I use Cacti at work for some things and I have created a custom memory graph that shows in more detail the different types of memory Linux uses and how much is available. Real memory is displayed as positive numbers and swap is displayed as negative numbers so I can see both real memory and swap memory on one graph. Here is an example:

Image

I actually could provide a couple of really good examples where you would see all of the real memory getting eaten up followed by swap but I'm not at work right now so this is the only snapshot I happened to find on my laptop. Basically the black line at the top represents to total amount of real memory installed on the system and the black line on the bottom represents the size of the swap. The white space actually indicates how much memory is in use, all the other colors indicate amount of that type of memory that is available for use.

Here's another:

Image

Post Reply