Free kernel memory
James R. Van Zandt
jrvz at comcast.net
Fri Jun 2 21:30:00 EDT 2006
klussier at comcast.net wrote:
> I'm looking for a way to flush the cache and buffers so that memory
> that is not currently in use shows in the "free" pool. Any ideas?
This may not be quite what you had in mind, but might give you some
ideas.
- Jim Van Zandt
#!/usr/bin/perl -w
#
# cleanup-memory
#
# by Linux Weekly News subscriber zblaxell <http://lwn.net/Articles/153203/>
#
# his explanation:
#
# I generally like to run a small application before suspending, which
# allocates memory until a few hundred pages are swapped (it is a loop
# of malloc() and reading paging statistics out of /proc), then
# exits. This dumps out some of the more useless 400MB or so of caches
# on my system, and cuts resume time in half (it does add a second or
# two to suspend), without the extreme pain of having to swap
# _everything_ back in on resume.
#
# My application forces all the clean pages (600MB as I write this) to
# go away, without losing active program text pages or forcing dirty
# pages to swap. It stops as soon as there are more than 100 pages
# written to swap since the program started running, so it does not
# significantly extend the suspend time (a few hundred pages are swapped
# before the application notices and exits, which does take a second or
# so).
#
# This approach doesn't need prior configuration--it automatically
# discovers just how much RAM can be cheaply freed by allocating as much
# as the system can spare without swapping, then it exits and leaves
# thousands of free pages.
#
# Without all the extra pages, the suspend image is much smaller, so
# suspend and resume are faster. Since only a few dirty or active pages
# were actually swapped, it doesn't noticeably slow down the machine
# after resume (there is more overhead when xscreensaver wakes up after
# noticing the wall clock time jumping well past the inactivity
# threshold, than there is from post-resume swapping ;-).
use strict;
use Time::HiRes qw(time);
sub swapfree {
open(PROC, "/proc/meminfo") or die "open: /proc/meminfo: $!";
my ($swapfree) = grep(/^SwapFree:/, <PROC>);
close(PROC);
$swapfree =~ s/\D+//gos;
print STDERR "swapfree=$swapfree\n";
return $swapfree;
}
my $last_swapfree = swapfree;
my @blobs;
my $count = 0;
my $total = 0;
my $start_time = time;
while ($last_swapfree <= (my $new_swapfree = swapfree)) {
++$count;
push(@blobs, ('.' x (1048576 * $count)));
$total += $count;
print STDERR "${total}M allocated \t";
$last_swapfree = $new_swapfree;
}
system("ps -F $$");
printf STDERR ("%5.2f seconds\n", time - $start_time);
More information about the gnhlug-discuss
mailing list