-   Linux - Kernel (
-   -   SWAP partition, is it necessary???? (

webquinty 05-19-2009 07:38 AM

SWAP partition, is it necessary????

I have a embedded device with kernel and compact flash WITHOUT swap partition and 256 Mb of RAM.
Well, when application startup, at the begin it works fine, but a piece of time later, application does not work fine.

After check all my configuration, I see that compact flash have not SWAP partition, then I made a new compact flash with SWAP partition. After do it, I run my application and I notice that it works fine.

So, what is the difference to use or not SWAP partition??

Thank very much.
Best regards.

repo 05-19-2009 07:40 AM

Take a look at

rikijpn 05-19-2009 07:52 AM

not necessary
SWAP partitions are not necessary, but depending on what you do, and your hardware (mostly RAM), you'd better have one.
If you have lots of programs running, you eventually run out of RAM memory(if you don't understand why, you'd better start by stfw for this), when that happens you'd just the data you are not going to use for a while in that SWAP partition, and retrieve it when needed again.
So, most of the time, you'd need one. But of course if you don't have such worries (have lots of RAM memory, etc) you may not need it.
You know you can actually just make a swap-file in your linux partition and mount it as a swap partition too.

titan22 05-19-2009 09:47 PM

YES. Swap effectively prevents a machine from getting into out of memory at certain level. Many programs die or show problems at low memory scenario. The example below should give you a clear picture why. (You won't see this clear example in any text book)

Let's assume you have 300 programs in disk, 2 pages each. Each program mallocs 2-page buffer to process data. Each program needs 4 pages of memory when it is running (2 pages for code, 2 pages for data).

Given 100 pages of RAM the system can run approximately 25 programs concurrently. Anything above that VM needs to reclaim memory by kicking out programs. Let's see what happen when more than 25 programs run.

=== Scenario 1: (no SWAP)
disk: 1000 pages (0 pages for swap, 1000 pages for files and directories)
Memory 100 pages

Without swap the buffers have to stay in memory. Only memory used by code can be freed because the can be refetched from disk. Eventaully your 100 pages of memory will be filled with buffers, only 2 page is left for the running program. Total 49 programs can be running.

memory (2-page buffer * 49 + 2-page code).

=== Scenario 2: (with SWAP, swap space == memory size)
disk: 1000 pages (100 pages for swap, 900 pages for files and directories)
Memory 100 pages

With 100 pages of swap space 50 2-page buffers from 50 programs can be stored there. Total 99 programs can be running concurrently.

memory (2-page buffer * 49 + 2 page code)
swap(2-page buffer * 50)

=== Scenario 3: (with SWAP. swap space = 2*memory size)
disk: 1000 pages (200 pages for swap, 800 pages for files and directories)
Memory 100 pages

memory (2-page buffer * 49 + 2 page code)
swap(2-page buffer * 100)
149 programs can run concurrently.

The system starts to experience out of memory at 50th program without swap, at 100th program with one time memory size swap, 150th with two times memory size swap. That says a system without swap hits out of memory much easier and programs that cannot handle out of memory start to show problems.

webquinty 05-20-2009 07:29 AM

Very clear

Thank very much.

nini09 05-22-2009 04:47 PM

Can the feature, save page to disk, be turned off because a lot of embedded system doesn't have disk on system?

syg00 05-22-2009 07:42 PM

You have the option of not configuring swap in the kernel.
I wouldn't normally recommend it, but can certainly see the applicability for embedded. Mind you, it makes memory sizing and decent (non-leaking) application code even more important.

jschiwal 05-22-2009 08:10 PM

It may not be a good idea having swap on an embedded device. Flash has a limited number of writes. You will shorten the life of your flash.

All times are GMT -5. The time now is 02:52 AM.