maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   MeeGo / Harmattan (https://talk.maemo.org/forumdisplay.php?f=45)
-   -   How to free swap memory without rebooting? (https://talk.maemo.org/showthread.php?t=89538)

Hacker 2013-03-21 09:31

How to free swap memory without rebooting?
 
My N9 is fastest in every way when swap memory usage is lowest. Emails swipe faster, homescreens swipe faster, and I think that swap-related slowdown is one of the primary reasons N9 owners are rebooting all of the time--because rebooting resets the swap memory.

I have seen other threads that discuss complex (and admittedly risky) ways to add swap capacity to our N9s, but I have a much more simple question:

How can we reset the swap memory as if we had rebooted without actually rebooting? After looking and searching I have yet to find an answer. I imagine that there must be a command that could be put in Profilematic or simply entered from time to time that would do the job, but what is it?

This could be a game-changer for many users, since clean swap positively impacts my N9's speed performance as much as FasterN9 and light overclocking.

Thanks.

juiceme 2013-03-21 10:00

Re: How to free swap memory without rebooting?
 
Well, I don't use OC, I don't use FasterN9, and still I find the performace good all the time regardless of uptimes...
(well it's only 7 days now because I did some boot stuff back then... but generally I only boot my device when I need to change to different kernel/OS...)

I do not think you need to boot your devices regularily to have good performance. If that's required, then you have misbehaving applications or you have yourself done some tuning of the device that has turned sour. :)

pichlo 2013-03-21 10:23

Re: How to free swap memory without rebooting?
 
Quote:

Originally Posted by Hacker (Post 1330449)
My N9 is fastest in every way when swap memory usage is lowest. Emails swipe faster, homescreens swipe faster, and I think that swap-related slowdown is one of the primary reasons N9 owners are rebooting all of the time--because rebooting resets the swap memory.

I thought the whole idea of swapping is to put the less used memory blocks out of the way but have them ready when needed. Resetting the swap would mean that the next time the memory block is needed, it will have to be reinitialized again which is slower than fetching it from the swap. It is true that the swap space may become fragmented in which case a reset may help a little but you will get a better performance boost by closing applications to reduce the absolute memory usage.

Quote:

How can we reset the swap memory as if we had rebooted without actually rebooting? After looking and searching I have yet to find an answer.
Have you found this?

http://talk.maemo.org/showthread.php?t=84007

You can reset the swap by running (as root)
Code:

swapoff -a
swapon -a

I won't recommend it though. I don't have an N9, but on my N900 this takes a LONG time (minutes) and results in some background tasks forcibly closed as they can't allocate memory while the swap is off.

A better solution would be having TWO swap spaces and from time to time swap (no pun intended) between them. This has been discussed a number of times. See e.g.

http://talk.maemo.org/showthread.php?t=42357
http://talk.maemo.org/showthread.php?t=84296
http://talk.maemo.org/showthread.php?t=86752

...and many others. Some of them discuss the N900 but the solution is actually the same for the N9.

Mikkosssss 2013-03-21 10:50

Re: How to free swap memory without rebooting?
 
I am not sure but is this what you are looking for?

http://store.ovi.com/content/258450?...e=search&pos=1

Lirion 2013-03-21 14:21

Re: How to free swap memory without rebooting?
 
Maybe Dropcache http://store.ovi.com/content/258450 help you out:
(dropcache and inception required)

Code:

terminal command:
aegis-exec /opt/dropcache-mdn/bin/dropcache.sh --3

The linux kernel has it's own algorithm to do this ... so i think it isn't a good idea to periodically force a swap flush either

Hacker 2013-03-21 16:15

Re: How to free swap memory without rebooting?
 
Thanks for the responses! I have read and researched what everyone has said and here's some feedback.

Quote:

Originally Posted by juiceme (Post 1330451)
Well, I don't use OC, I don't use FasterN9, and still I find the performace good all the time regardless of uptimes...
(well it's only 7 days now because I did some boot stuff back then... but generally I only boot my device when I need to change to different kernel/OS...)

I do not think you need to boot your devices regularily to have good performance. If that's required, then you have misbehaving applications or you have yourself done some tuning of the device that has turned sour. :)

Hmmm, given the number of users who use FasterN9, overclocking, etc., slow performance is an issue for many of us! Maybe you have an exceptional N9 or you have different expectations. :-) The problem is not misbehaving apps (I used top command and Conky to check); but rather it seems to be that swap memory is not being freed up even after the related apps are closed, so you eventually use more and more of the available swap memory, causing the N9 to slow down more and more.

The swap-related slowdown issue is discussed in this thread: http://talk.maemo.org/showthread.php?t=84007

I started this thread in hopes that an answer could be found to the swap-reset question. Once we have an answer, It can be merged in a solution thread.

Quote:

Originally Posted by pichlo (Post 1330460)
I thought the whole idea of swapping is to put the less used memory blocks out of the way but have them ready when needed. Resetting the swap would mean that the next time the memory block is needed, it will have to be reinitialized again which is slower than fetching it from the swap. It is true that the swap space may become fragmented in which case a reset may help a little but you will get a better performance boost by closing applications to reduce the absolute memory usage.

Have you found this?

http://talk.maemo.org/showthread.php?t=84007

Yes, I have found that thread, and while your theory seems plausible, it clearly is not working out in real life because several users, including users in the thread that you quote, are having the same problem with swap-related issues! ;-) If the memory were behaving as expected and not causing slowdown, I suppose most of these types of threads would not exist. The problem, I think, is as I stated above: that even closing programs that caused the swap memory to be used does not free swap mem., so it is inevitably exhausted and leads to slower and slower performance.

Quote:

Originally Posted by pichlo (Post 1330460)
You can reset the swap by running (as root)
Code:

swapoff -a
swapon -a

I won't recommend it though. I don't have an N9, but on my N900 this takes a LONG time (minutes) and results in some background tasks forcibly closed as they can't allocate memory while the swap is off.

This approach was discussed in the thread you quote and did not work. :-( Has anyone with an N9 used this approach successfully? I will run a confirmation study when I have a chance.

And thanks for the two swap spaces ideas and research threads. I'll check them out, but am still looking for a workable, easy swap reset approach that avoids reboot.

Mikkosssss and Lirion, I have tried Dropcache, but as discussed in the thread discussed above, it does not seem to solve the problem of freeing swap memory (though it is very useful in tracking the problem as it shows the amount of free swap memory).

Thanks again, everyone.

TMavica 2013-03-21 16:17

Re: How to free swap memory without rebooting?
 
Swap memory cant be free up except booting, only way is to add more swap memory from 255 to 412

Hacker 2013-03-21 16:29

Quote:

Originally Posted by TMavica (Post 1330551)
Swap memory cant be free up except booting, only way is to add more swap memory from 255 to 412

Why? This seems so strange . . . Please help me understand. Is it a bug?

I saw your earlier post, so any insight would be much appreciated.

TMavica 2013-03-21 16:35

Re: How to free swap memory without rebooting?
 
Sorry I dont know why, just know many developer said CANT..I dont know the reason and the meaning, just I know is cant

pichlo 2013-03-21 19:36

Re: How to free swap memory without rebooting?
 
Quote:

Originally Posted by Hacker (Post 1330554)
Quote:

Originally Posted by TMavica (Post 1330551)
Swap memory cant be free up except booting, only way is to add more swap memory from 255 to 412

Why? This seems so strange . . . Please help me understand. Is it a bug?

Look at these figures:
Code:

N900:~# free
            total        used        free      shared      buffers
Mem:        235252      232012        3240            0        1784
-/+ buffers:            230228        5024
Swap:      786424        71540      714884

Of course they will be different on your N9 or even on the next guy's N900, but the idea is the same. The total used memory (main + swap) is higher than the amount of available main memory. Which means some pages have to be swapped. The only way to get around this is by increasing the amount of main memory, which is clearly not possible.

One way of reducing the impact of swapping is spreading the bandwidth, such as one swap partition in the device and one on the SD card, as discussed in those threads that I linked in my first reply.

EDIT: I am actually a bit surprised how much main memory is used before swapping occurs (or how little main memory is left free). My gut feeling would be to strive for at least 20% slack, which would mean even more swap usage. Obviously the Hildon programmers knew better.

Hacker 2013-03-21 20:18

Re: How to free swap memory without rebooting?
 
I appreciate the effort, but I don't think that I am being clear enough, so please let me try to restate the problem.

(1) We all agree that when you reboot the N9, there is 255 MB of swap available out of a total swap size of 255 MB. In short, 100% of the swap memory is available immediately after reboot.

(2) I think we all agree that after using the device, the swap memory begins to be consumed, so after a bunch of multitasking, use of heavy apps, etc., the swap memory will drop to something like 200MB out of 255MB total. After more use, it may drop to 130 MB out of 255MB total. And ultimately can reach 0MB out of 255MB total.

(3) On my device, shutting down the heavy apps, multitasking windows, etc. does NOT restore the swap capacity, so the available swap memory tends only to go down.

(4) I have noticed that slowdown both within apps like Fenix and while switching between the three homescreens, and in general other usage, is noticeable and irritating. The slowdown I experience is directly proportional to the degree that swap memory is utilized. Performance slows early on, though it is hardly noticeable, but then gets progressively worse until it absolutely cripples the device.

(5) MY QUESTION. Yes, we all know that rebooting the device will get us back to a snappy 255MB out of 255MB of swap available. BUT, how can this exact result be obtained without rebooting the device??? Something like a swap memory refresh script is what I'm looking for. Can you dig it? :)

kumary 2013-03-21 20:58

Re: How to free swap memory without rebooting?
 
Don't have any other answer to your query except what all other said i.e. No. Till now haven't heard of any such possible solution or script but that doesn't mean its impossible. That being said you can definitely increase your swap memory without making any changes to your main memory and it is well explained here

http://talk.maemo.org/showpost.php?p...02&postcount=4

which means It will free the 256MB of RAM reserved as Swap and instead use new mtd partition and create new swap memory of 420+MB

Currently 1GB RAM is used as 768 MB RAM and 256MB as Swap
later it will be used as
1GB RAM + 420MB of SWAP (additional) which means more RAM and more SWAP for your mobile :)

Steps are given on this post
http://talk.maemo.org/showpost.php?p...3&postcount=51

If you find any issue just PM me. I can explain in detail ;)

Cheers

pichlo 2013-03-21 21:18

Re: How to free swap memory without rebooting?
 
Quote:

(1) We all agree that when you reboot the N9, there is 255 MB of swap available out of a total swap size of 255 MB. In short, 100% of the swap memory is available immediately after reboot.
Actually, there is 100% out of the total virtual memory (RAM + swap) available. I am not sure on exact figures; is it 1 GB RAM + 256 MB swap or 1 GB altogether?

Quote:

(2) I think we all agree that after using the device, the swap memory begins to be consumed, so after a bunch of multitasking, use of heavy apps, etc., the swap memory will drop to something like 200MB out of 255MB total. After more use, it may drop to 130 MB out of 255MB total. And ultimately can reach 0MB out of 255MB total.
Again, we are talking about the total virtual memory (RAM + swap) that gets consumed. Swap is just an extension of the RAM, nothing more, nothing less. Memory gets allocated by the OS and applications. When there is not enough memory in the RAM, some pages are swapped out (moved to swap). When the application frees the memory (which happens naturally during its operation and always upon terminating it), it does not matter where the memory block actually is. Swap or RAM, it is made available again.

Quote:

(3) On my device, shutting down the heavy apps, multitasking windows, etc. does NOT restore the swap capacity, so the available swap memory tends only to go down.
That is very strange. Applications and most OS components have no way of telling which memory to use. They just go, "give me some memory" and "here is your memory back, I no longer need it". Only the kernel decides whether a certain memory page should be in RAM or in swap, using a sophisticated algorithm, based mostly the frequency of the access to a particular page. More used pages stay in the RAM, the less used ones go to the slower part of the virtual memory, a.k.a. swap.

If what you say is this indeed the case then I would expect the available RAM to go drastically down as well. Yes, it is theoretically possible that the swap apace is completely full while the RAM is not. I would expect that to happen if a memory hungry background task has not been used for a long time. Let's call it task X. Other tasks have started, allocated some memory, the kernel had to resort to pushing some memory blocks already in RAM to swap. If task X has not used its memory for a while (and forgot to free it), then after a while it might find all its memory swapped out. When other tasks terminate, they release their memory but task X's memory is not moved from swap back to RAM as it is not needed (yet). This is however a very hypothetical situation that is very easily verified.

To give us a better picture, could you please post the result of:
Code:

free
followed by
Code:

top
Ideally when you experience your swap problems as described.
(After starting top, press 'm' to sort the processes by memory usage.)

Hacker 2013-03-21 22:12

In the interest of science, here you are:

BusyBox v1.20.0.git (MeeGo 3:1.20-0.2+0m8) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ $ free
total used free shared buffers
Mem: 1008648 967212 41436 0 27132
-/+ buffers: 940080 68568
Swap: 262136 54776 207360
~ $

And here is the top output:

Mem: 967620K used, 41028K free, 0K shrd, 27724K buff, 248328K cached

Thanks for your interest. Just to make sure, though, I'm really looking for an answer to paragraph (5) in the post above, which should be the same for all N9s, without regard for any specific memory data. :)

pichlo 2013-03-21 22:39

Re: How to free swap memory without rebooting?
 
Your 'free' output looks OK. Swap is barely used. I assume you are not experiencing any problems now, are you?

A more interesting of the 'top' output would be the following few lines, like this:
Code:

Mem: 229072K used, 6180K free, 0K shrd, 43144K buff, 46060K cached
CPU:  2.5% usr  5.3% sys  0.0% nic 89.4% idle  2.5% io  0.0% irq  0.0% sirq
Load average: 0.09 0.08 0.07 1/205 4629
  PID  PPID USER    STAT  RSS %MEM %CPU COMMAND
4280  1191 user    S    16624  7.0  0.4 /usr/bin/modest
 4193  1461 user    S    14756  6.2  0.0 /usr/sbin/browserd -s 4193 -n browseru
 1029  703 root    S <  11356  4.8  0.2 /usr/bin/Xorg -logfile /tmp/Xorg.0.log

 1303  1191 user    S    10564  4.4  0.7 /usr/bin/hildon-home
 1805  1191 user    S    9128  3.8  0.0 /usr/bin/browser
 4434  1191 user    S    9128  3.8  0.0 /usr/bin/osso-xterm

The answer to your question 5 is, "it depends". If the real problem is that all your virtual memory is used up, then resetting swap is not going to help. That's where 'free' comes in. If, OTOH, some runaway background task allocated a lot of memory that got swapped out in a hypothetical situation such as what I described, then we need to find the culprit. Hence 'top'.

Hacker 2013-03-22 00:36

Quote:

Originally Posted by pichlo (Post 1330628)
Your 'free' output looks OK. Swap is barely used. I assume you are not experiencing any problems now, are you?

A more interesting of the 'top' output would be the following few lines, like this:
Code:

Mem: 229072K used, 6180K free, 0K shrd, 43144K buff, 46060K cached CPU: 2.5% usr 5.3% sys 0.0% nic 89.4% idle 2.5% io 0.0% irq 0.0% sirq Load average: 0.09 0.08 0.07 1/205 4629 PID PPID USER STAT RSS %MEM %CPU COMMAND 4280 1191 user S 16624 7.0 0.4 /usr/bin/modest 4193 1461 user S 14756 6.2 0.0 /usr/sbin/browserd -s 4193 -n browseru 1029 703 root S < 11356 4.8 0.2 /usr/bin/Xorg -logfile /tmp/Xorg.0.log 1303 1191 user S 10564 4.4 0.7 /usr/bin/hildon-home 1805 1191 user S 9128 3.8 0.0 /usr/bin/browser 4434 1191 user S 9128 3.8 0.0 /usr/bin/osso-xterm
The answer to your question 5 is, "it depends". If the real problem is that all your virtual memory is used up, then resetting swap is not going to help. That's where 'free' comes in. If, OTOH, some runaway background task allocated a lot of memory that got swapped out in a hypothetical situation such as what I described, then we need to find the culprit. Hence 'top'.

Yeah, I had rebooted shortly before I took those screenshots, so I'll try to let it run a while and then take more screenshots when I have less swap memory available.

Thanks! I like a good mystery/investigation . . .

reinob 2013-03-22 08:41

Re: How to free swap memory without rebooting?
 
Quote:

(1) We all agree that when you reboot the N9, there is 255 MB of swap available out of a total swap size of 255 MB. In short, 100% of the swap memory is available immediately after reboot.
This is not necessarily true. If you've understood what @pichlo is trying to say from the very beginning of this thread, you'd know that swap is the same as ram, only that it uses disk :)

The kernel may well decide to use swap during boot, but this is not likely.

Quote:

(2) I think we all agree that after using the device, the swap memory begins to be consumed, so after a bunch of multitasking, use of heavy apps, etc., the swap memory will drop to something like 200MB out of 255MB total. After more use, it may drop to 130 MB out of 255MB total. And ultimately can reach 0MB out of 255MB total.
*MEMORY* is used. Whether it's DRAM or swap is only the kernel's business. Obviously, the more memory is used the less memory is available. If your free swap reaches zero there is a very high probability that your physical ram is also zero, meaning you can't do anything.

Quote:

(3) On my device, shutting down the heavy apps, multitasking windows, etc. does NOT restore the swap capacity, so the available swap memory tends only to go down.
When a program exits its memory (both ram and swap) is freed. Now most programs are not self contained but use libraries and other "middleware" which is basically constantly allocating memory. Most programs written after 2000 basically don't care about memory and except that someone else will take care of deallocating it. The problem is that that someone else doesn't exist.

Imagine (just hypothetically) a music player tells pulseaudio to allocate a chunk of 4MB to buffer music (this is clearly false, but only a made-up example) and then exists without telling pulseaudio that it doesn't need the buffer anymore. The memory used by the player is freed, but those 4MB are still there, allocated (reserved) and basically orphaned forever.

Quote:

(4) I have noticed that slowdown both within apps like Fenix and while switching between the three homescreens, and in general other usage, is noticeable and irritating. The slowdown I experience is directly proportional to the degree that swap memory is utilized. Performance slows early on, though it is hardly noticeable, but then gets progressively worse until it absolutely cripples the device.
This is the usual effect or having almost no memory availblle (if you have a VM at hand try running Windows NT 4.0 with 12MB of RAM and 16MB of swap. You'll see how I lived for about 3 years).

Quote:

(5) MY QUESTION. Yes, we all know that rebooting the device will get us back to a snappy 255MB out of 255MB of swap available. BUT, how can this exact result be obtained without rebooting the device??? Something like a swap memory refresh script is what I'm looking for. Can you dig it? :)
There's no way to achieve what you want. The only way would be if all programs that are running would be closed so as to free the memory they were using and then start again, which is not going to happen (at least in general).

You could try reducing the swappiness, but this will just make your N9 use more RAM. You could also try reducing the block/inode cache. This will get you more free memory at the expense of everything being slower.

Basically, the only way to not leak memory is by using programs that don't leak memory. Given the unfortunately "common wisdom" that you are supposed to reboot every couple of hours/days, most programmers don't feel the need to do things properly, thus reinforcing that common wisdom.

Hacker 2013-03-22 09:12

Reinob, I greatly appreciate your response and am thankful for all of the information and help you and others have contributed to this thread.

I think that the middleware idea is the likely issue, with the occasional misbehaving program also being another possible complication. I suppose that reboot is not the worst thing in the world, and if that's the only way to get things fully cleared, then I'll live. :)

I still find it strange that things are this way, but c'est la vie avec N9.

juiceme 2013-03-22 10:17

Re: How to free swap memory without rebooting?
 
But Hacker, please continue investigating this :)

I would like to see what are the programs that are hogging memory on your device, so after few days when it starts swapping a lot, use "top" to see who is the culprit.

I assume then just killing&restarting that program you will get your memory back...

reinob 2013-03-22 12:50

Re: How to free swap memory without rebooting?
 
Quote:

Originally Posted by Hacker (Post 1330697)
I still find it strange that things are this way, but c'est la vie avec N9.

Maybe this is a wild shot, but I just re-read above that the N9 has 768MB *RAM* and 256MB *SWAP*.

If so, I've read somewhere (maybe not even applicable anymore) that anonymous memory (backed by swap, not by file, e.g. malloc()'d memory) cannot be reclaimed unless it can be moved to swap (?).

Meaning if your swap is too small to even hold a copy of the RAM, then you may actually have memory blocks which *could* be freed which are not been free'd because of lack of swap (???).

Besides, it may be that many programs overcommit memory. When a program "reserves" a chunk of memory, that memory is reserved from swap. The memory is only really allocated when it's used.

Meaning, if you have little swap (and the N9 has too little), there may be programs which cannot reserve memory (even if it's available), only because of lack of sufficient swap.

The only advice I can give is:

1. Increase your swap (if partitioning is a problem with the N9, just make a swap file).

2. Decrease your swappiness (e.g. echo 20 > /proc/sys/vm/swappiness).

3. Increase VFS cache pressure (e.g. echo 200 > /proc/sys/vm/vfs_cache_pressure). This way cache is reclaimed when necessary (instead of swapping out programs).

Other things that could help would be e.g. reducing M_MMAP_THRESHOLD and/or M_TRIM_THRESHOLD (at least for long-running programs). But I don't know what the default values are on the N9 (nor on the N900 for that matter).

pichlo 2013-03-22 15:18

Re: How to free swap memory without rebooting?
 
Quote:

Originally Posted by reinob (Post 1330798)
If so, I've read somewhere (maybe not even applicable anymore) that anonymous memory (backed by swap, not by file, e.g. malloc()'d memory) cannot be reclaimed unless it can be moved to swap (?).

Really? Would it not mean that I could not run Linux with no swap at all? That is contrary to my experience..

Perhaps it was about a distinction between pageable and non-pageable memory. The latter means memory that is not allowed to be moved to swap because the kernel needs it permanently in RAM (file system cache is an obvious example). That is not the same as cannot be moved to swap because of a lack of swap space. But I would still find it strange.

kai_en 2013-03-23 00:34

Edit: Removed, something wrong with TMO app, brings me to wrong thread respective to the title


All times are GMT. The time now is 14:25.

vBulletin® Version 3.8.8