Linux use all ram

How to fill 90% of the free memory?

I want to do some low-resources testing and for that I need to have 90% of the free memory full. How can I do this on a *nix system?

Instead of jut filling memory, could you instead create a VM (using docker, or vagrant, or something similar) that has a limited amount of memory?

@abendigo For a QA many of the solutions presented here are useful: for a general purpose OS without a specific platform the VM or kernel boot parameters could be useful, but for a embedded system where you know the memory specification of the targeted system I would go for the filling of the free memory.

In case anyone else is a little shocked by the scoring here: meta.unix.stackexchange.com/questions/1513/…?

15 Answers 15

stress-ng is a workload generator that simulates cpu/mem/io/hdd stress on POSIX systems. This call should do the trick on Linux < 3.14:

stress-ng --vm-bytes $(awk '/MemFree/' < /proc/meminfo)k --vm-keep -m 1 

For Linux >= 3.14, you may use MemAvailable instead to estimate available memory for new processes without swapping:

stress-ng --vm-bytes $(awk '/MemAvailable/' < /proc/meminfo)k --vm-keep -m 1 

Adapt the /proc/meminfo call with free(1) / vm_stat(1) /etc. if you need it portable. See also the reference wiki for stress-ng for further usage examples.

stress --vm-bytes $(awk '/MemFree/' < /proc/meminfo)k --vm-keep -m 10

Most of MemFree is kept by OS, so I used MemAvailable instead. This gave me 92% usage on Cent OS 7. stress --vm-bytes $(awk '/MemAvailable/' < /proc/meminfo)k --vm-keep -m 1

good to know, MemAvailable was added to "estimate of how much memory is available for starting new applications, without swapping", see git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/… and git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/…

Just as an added note, providing both --vm 1 and --vm-keep are very important. Simply --vm-bytes does nothing and you might be misled into think you can allocate as much memory as you need/want. I got bit by this until I tried to sanity check myself by allocation 256G of memory. This is not a flaw in the answer, it provides the correct flags, just an additional caution.

This is why there is -m 1 . According to the stress manpage, -m N is short for --vm N : spawn N workers spinning on malloc()/free()

If you have basic GNU tools ( head and tail ) or BusyBox on Linux, you can do this to fill a certain amount of free memory:

This works because tail needs to keep the current line in memory, in case it turns out to be the last line. The line, read from /dev/zero which outputs only null bytes and no newlines, will be infinitely long, but is limited by head to BYTES bytes, thus tail will use only that much memory. For a more precise amount, you will need to check how much RAM head and tail itself use on your system and subtract that.

To just quickly run out of RAM completely, you can remove the limiting head part:

If you want to also add a duration, this can be done quite easily in bash (will not work in sh ):

The cat command will wait for inputs to complete until exiting, and by keeping one of the pipes open, it will keep tail alive.

Читайте также:  Linux руководство по командам

If you have pv and want to slowly increase RAM use:

The latter will use up to one gigabyte at a rate of ten megabytes per second. As an added bonus, pv will show the current rate of use and the total use so far. Of course this can also be done with previous variants:

Just inserting the | pv | part will show you the current status (throughput and total by default).

Compatibility hints and alternatives
If you do not have a /dev/zero device, the standard yes and tr tools might substitute: yes | tr \\n x | head -c BYTES | tail ( yes outputs an infinite amount of "yes"es, tr substitutes the newline such that everything becomes one huge line and tail needs to keep all that in memory).
Another, simpler alternative is using dd : dd if=/dev/zero bs=1G of=/dev/null uses 1GB of memory on GNU and BusyBox, but also 100% CPU on one core.
Finally, if your head does not accept a suffix, you can calculate an amount of bytes inline, for example 50 megabytes: head -c $((1024*1024*50))

Credits to falstaff for contributing a variant that is even simpler and more broadly compatible (like with BusyBox).

Why another answer? The accepted answer recommends installing a package (I bet there's a release for every chipset without needing a package manager); the top voted answer recommends compiling a C program (I did not have a compiler or toolchain installed to compile for your target platform); the second top voted answer recommends running the application in a VM (yeah let me just dd this phone's internal sdcard over usb or something and create a virtualbox image); the third suggests modifying something in the boot sequence which does not fill the RAM as desired; the fourth only works in so far as the /dev/shm mountpoint (1) exists and (2) is large (remounting needs root); the fifth combines many of the above without sample code; the sixth is a great answer but I did not see this answer before coming up with my own approach, so I thought I'd add my own, also because it's shorter to remember or type over if you don't see that the memblob line is actually the crux of the matter; the seventh again does not answer the question (uses ulimit to limit a process instead); the eighth tries to get you to install python; the ninth thinks we're all very uncreative and finally the tenth wrote his own C++ program which causes the same issue as the top voted answer.

Источник

How to free up RAM in Ubuntu

Free RAM memory in Ubuntu

It has always been said that the RAM is there to use it. Of course, this is valid when we have a computer with enough memory to not be watching what happens with it. When we're a little tighter, it's worth taking care of it a bit, in the sense that we have to watch how busy we are and, whenever possible, make sure our team can get some air.

Sometimes, the system does not release RAM memory that is no longer being used and although it is released on demand —to call it somehow— when we open a new program, it can also be released manually by entering a simple command in our console. In theory, this behavior is common in almost all operating systems, and it makes sense: to be able to go faster when we want to access something we just used again.

Читайте также:  Linux обновить версию git

Find out how much is in use and then free up RAM

First of all, stop know how much memory is in use, how much is free and how much is saved in the cache we execute the command:

To see the use in real time we use:

The first command will return something similar to what we see in the header capture, while with the second we will see something very similar, but it will move in real time.

As can be seen, there is a large amount of cached memory, about half of which is being used by open applications. For release cached pages, inodes, and directory entries, just run the command:

sudo sysctl -w vm.drop_caches=3

It is important not to forget to run "sudo sync", or else we could lose information present in RAM that has not yet been saved to the hard drive.

Closing unnecessary processes

Ubuntu, and Linux in general, manages RAM pretty well, so you usually don't need to do any of the above. It can be different if the operating system does not know what to do with a heavy workload. That is to say, when we have many programs open, the operating system cannot or is not the one to decide what to do with each of them, and it tries to move them as it can. If there is no RAM, it will probably "decide" on its own to close some program, but this can cause us to lose some information.

Therefore, and as on so many occasions, the best way to manage RAM memory is head. If we have a computer with at least 16GB of RAM, we will rarely run out of memory if we do it in normal use, but with 4 or less, things are different. In these cases, it is best to have open only what is necessary.

If we notice that the computer suffers, we can open the task manager and see what is happening:

Ubuntu System Monitor

In the system monitor, a graphical tool (GUI) that shows us information similar to that of htop, we will see all the processes that are open. We can order them by name, by user, CPU or RAM usage, among others. If we notice that our computer is slow or has a hard time working, we are interested in ordering the processes by processor use (% CPU) or RAM (Memory). If we know what a process that is consuming a lot of resources is doing, we can right-click on it and choose the “Kill” option. Of course, saving before all the important changes.

If you look closely at the information offered by the system monitor or htop, among others, we will learn what consumes the most. From here, a recommendation for the owners of a low-income equipment is that have a good control of the web browser. Although it can be used to do everything and today it is one of the best work tools, it can also have many open processes that can leave our computer speechless. Therefore, it is worth not having many tabs open if they are not needed, and even closing the browser entirely.

Читайте также:  Удаление установленных пакетов linux

RAM is there to use it, but not to waste it or make us suffer.

The content of the article adheres to our principles of editorial ethics. To report an error click here.

Full path to article: ubunlog » Ubuntu » How to free up RAM in Ubuntu

Источник

How can I run a complete application from RAM? [duplicate]

When I do some stuff in background (unpacking, compiling, backup, etc.) my hdd is under load and for example, Firefox and Chromium take very long to start and react very slow. While my hdd is under load I have still plenty of free RAM and free CPU. How can I copy the whole Firefox or Chromium including all dependencies into RAM? I don't care about persistence, I just want to view some websites, videos, etc. There are instructions putting the cache into /dev/shm, but that didn't speed up so much. Getting the profile into the RAM drive might also be easy. But how to get the binaries with all dependencies into RAM?

4 Answers 4

You do not need to copy all dependencies in the RAM, since, Modern application doesn't load all of it's function at the same time. They load them from disk, when necessary.

Yes, The system may use HDD sometimes to make room for sufficient RAM, So, that it can load another application which may require more RAM. So, it uses a feature called "Swapping".

"Swapping" is good, since it enables you to load more applications at the same time. But it uses hard disk, which is a slow process.

So, if you believe that, you have sufficient RAM for not using swapping at all, you can disable swapping altogether.

To temporarily disable swapping: run this sudo swapoff -a in a terminal and hit enter.

If you want to disable swapping permenantly do this:

  • Open a terminal and run sudo gedit /etc/fstab
  • Carefully delete the line which contains word swap . save and exit gedit.
  • Restart and Login again.

Note: You should make a backup of /etc/fstab file, so that if somethings go wrong, you can restore that.

To backup: sudo cp /etc/fstab /etc/fstab.backup

To restore: sudo cp /etc/fstab.backup /etc/fstab

Memory management is handled by the OS it self. It schedule all the process to give same amount of time to execute. That's the basic of an multiprocessing OS.

Also how many data of an process loaded to RAM is decided by the application. If a application require a memory more than available memory than OS handles it by writing some part of it to virtual page (SWAP space in linux system). (Search for Paging technique of OS ). So if you are plenty of memory be sure the OS is using it properly as requested by the application.

So basically you can't copy anything to RAM. (Also what is RAM drive?). Writing to RAM is done by OS.

I doubt in one statement that there are plenty free CPU while the background task is running . Possibly you have misunderstood something.

Last thing you can use to change priority of the process so the CPU gives more time to that process. renice or nice is the command. But the possibility of system freez/crash , unstable behavior is expected when you change priority.

Источник

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