Freeing memory in linux

How can I free memory on linux

When I use top to see memory usage, I have 65gb ram but only 1.3gb of it free and remaining is shown as used. When I ran my program It gives memory insufficiency error. Although no other program is using the remaining 63.7gb ram it is hold. how can I get free the unused ram?

Please give more details about the program you are trying to run. Linux uses most of the free ram to cache data from the harddisk but clears this caches to get free memory for programs to run. So are you perhaps trying to run a java program?

We need more information about the program you’re running. How much memory does it need? Is it 32 or 64 bit? What language is it in?

4 Answers 4

Too many duplicates to list. But long story short, the memory is in use as cache and buffers, and will be freed as applications consume more real memory.

If you want the memory not to be used, take it out of the computer and sit it on your desk.

There is a common misunderstanding that free RAM is good. Actually, every byte of RAM that is free represents a failure of the system to make effective use of that RAM. Free RAM won’t make your system any faster.

Consider a grossly oversimplified scenario. A program runs, and then finishes. Your operating system has two choices:

1) It could make the memory that held the program free. This requires a specific operation to make it free, and a specific operation to allow it to be used again. And if the program runs again, it will have to be loaded in from disk.

2) It could not make the memory that held the program free. This requires no operation to make it free, but a specific operation might be needed later to move it to another use. And if the program runs again, it will not have to be loaded in from disk.

So, under most conditions, option 2 is a pure win. It saves an operation if the memory needs to be used for something else (it can just switch from one use to another in one step rather than two steps, one to make it free and one to make it not free). And it may save the program from having to be loaded in from disk, which would be a huge win.

So, the short version is, you don’t want free RAM.

Источник

How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

Like any other operating system, GNU/Linux has implemented memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

Читайте также:  Linux allow ssh for user

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear pagecache, dentries, and inodes.

# sync; echo 3 > /proc/sys/vm/drop_caches

Explanation of the above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in the kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

If you have to clear the disk cache, the first command is safest in enterprise and production as “. echo 1 > ….” will clear the PageCache only. It is not recommended to use the third option above “. echo 3 >” in production until you know what you are doing, as it will clear pagecache, dentries, and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

Free Buffer and Cache in Linux

When you are applying various settings and want to check, if it is actually implemented specially on the I/O-extensive benchmark, then you may need to clear the buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into the disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover, it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2 am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

#!/bin/bash # Note, we are using "echo 3", but it is not recommended in production instead use "echo 1" echo "echo 3 > /proc/sys/vm/drop_caches"

Set execute permission on the clearcache.sh file.

Now you may call the script whenever you are required to clear the ram cache.

Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.

Append the below line, save and exit to run it at 2 am daily.

0 2 * * * /path/to/clearcache.sh

For more details on how to cron a job, you may like to check our article on 11 Cron Scheduling Jobs.

Читайте также:  Zabbix agent linux удаление
Is it a good idea to auto clear the RAM cache on the production server?

Clear RAM Cache on Linux Production Server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache every day at 2 am. Every day at 2 am the script is executed and it flushes your RAM cache. One day for whatsoever reason may be more than expected users are online on your website and seeking resources from your server.

At the same time, the scheduled script runs and clears everything in the cache. Now all the users are fetching data from the disk. It will result in a server crash and corrupt the database. So clear ram-cache only when required, and known your footsteps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Also, you may add the above command to a cron script above, after understanding all the associated risks.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared' OR $ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root

After testing both the above commands, we will run the command “free -h” before and after running the script and will check the cache.

That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know, what you think is a good idea to clear ram cache and buffer in production and Enterprise?

Источник

What is the best way to prevent out of memory (OOM) freezes on Linux?

Is there a way to make the OOM killer work and prevent Linux from freezing? I’ve been running Java and C# applications, where any memory allocated is usually used, and (if I’m understanding them right) overcommits are causing the machine to freeze. Right now, as a temporary solution, I added,

vm.overcommit_memory = 2 vm.overcommit_ratio = 10 

to /etc/sysctl.conf. Kudos to anyone who can explain why the existing OOM killer can’t function correctly in a guaranteed manner, killing processes whenever the kernel runs out of «real» memory. EDIT — many responses are along the lines of Michael’s «if you are experiencing OOM killer related problems, then you probably need to fix whatever is causing you to run out of memory». I don’t think this is the correct solution. There will always be apps with bugs, and I’d like to adjust the kernel so my entire system doesn’t freeze. Given my current technical understandings, this doesn’t seem like it should be impossible.

Читайте также:  Linux mint пропадает интернет

The OOM killer on my linux systems seems to work as designed. How sure are you that you are experiencing a OOM killer failure? Why do you think that is the cause? Have you considered the possibility of garbage collector trouble as well?

The problem is that the OOM killer does not activate soon enough. First the kernel drops all caches and that makes your system freeze. This as a kernel design mistake and/or a distro configuration mistake that should be fixed by the kernel developers. Unfortunately the problem has been there for many years already and it is not getting fixed because people insist on non-solutions such as «buy more RAM» which obviously don’t fix the underlying problem. You can recover faster by manually running the OOM killer (SysRq+F) when it freezes but this is a workaround at best.

The best way would be for Linux to fix this bug — perhaps the most annoying Linux issue ever. It was filed back in 2007 — System freeze on high memory usage. Even Windows prevents this by displaying a dialog warning the user about the low memory.

6 Answers 6

Below is a really basic perl script I wrote. With a bit of tweaking it could be useful. You just need to change the paths I have to the paths of any processes that use Java or C#. You could change the kill commands I’ve used to restart commands also. Of course to avoid typing in perl memusage.pl manually, you could put it into your crontab file to run automatically. You could also use perl memusage.pl > log.txt to save its output to a log file. Sorry if it doesn’t really help, but I was bored while drinking a cup of coffee. 😀 Cheers

#!/usr/bin/perl -w # Checks available memory usage and calculates size in MB # If free memory is below your minimum level specified, then # the script will attempt to close the troublesome processes down # that you specify. If it can't, it will issue a -9 KILL signal. # # Uses external commands (cat and pidof) # # Cheers, insertable our $memmin = 50; our @procs = qw(/usr/bin/firefox /usr/local/sbin/apache2); sub killProcs < use vars qw(@procs); my @pids = (); foreach $proc (@procs) < my $filename=substr($proc, rindex($proc,"/")+1,length($proc)-rindex($proc,"/")-1); my $pid = `pidof $filename`; chop($pid); my @pid = split(/ /,$pid); push @pids, $pid[0]; >foreach $pid (@pids) < #try to kill process normall first system("kill -15 " . $pid); print "Killing " . $pid . "\n"; sleep 1; if (-e "/proc/$pid") < print $pid . " is still alive! Issuing a -9 KILL. \n"; system("kill -9 " + $pid); print "Done.\n"; >else < print "Looks like " . $pid . " is dead\n"; >> print "Successfully finished destroying memory-hogging processes!\n"; exit(0); > sub checkMem < use vars qw($memmin); my ($free) = $_[0]; if ($free >$memmin) < print "Memory usage is OK\n"; exit(0); >else < killProcs(); >> sub main < my $meminfo = `cat /proc/meminfo`; chop($meminfo); my @meminfo = split(/\n/,$meminfo); foreach my $line (@meminfo) < if ($line =~ /^MemFree:\s+(.+)\skB$/) < my $free = ($1 / 1024); &checkMem($free); >> > main(); 

Источник

Оцените статью
Adblock
detector