Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep resource usage

I have been tasked with writing a shell script to grep through hundreds of log files in many directories on Linux and Solaris servers. Some of the logs are compressed in many formats and some are a few GB in size. I am worried about grep using a lot of resources on the server and possibly taking down web servers that are running on the machine through exhausting the memory (If this will likely happen).

Should I uncompress the files, grep them and then compress them again or use zgrep (or equivalent) to search them while compressed? Would there be an advantage resource wise to using one method over the other?

Also, is there a simple way to restrict the memory usage of a command to a percentage of what is currently available?

If someone could explain how memory usage works while running these commands, it would help out a lot.

like image 610
Robben Avatar asked Oct 12 '17 18:10

Robben


People also ask

Is grep CPU intensive?

GREP process (find.exe) consumes ~60% of the CPU.

How do I check memory usage on PID?

The ps command can also be used to monitor memory usage of individual processes. The ps v PID command provides the most comprehensive report on memory-related statistics for an individual process, such as: Page faults. Size of working segment that has been touched.

How do I find the top 10 memory consuming process in Linux?

Use ps Command to Find Top Processes by Memory and CPU Usage ps is a Linux command-line utility with many options that helps you to display output in different formats. You can use the ps command with –sort argument to sort the output by memory and CPU usage.


1 Answers

grep memory usage is constant; it doesn't scale with file size. It doesn't need to keep the whole file in memory, only the area it's searching through.

Decompression is similar. Memory usage is proportional to the dictionary size, not to the total file size. Dictionary size is nothing to worry about: a few megabytes at most.

I would not worry about some simple grep / zgrep / zcat | grep searches taking down other processes. This stuff is Linux's bread and butter.


† Beware of scanning through files with incredibly long lines, though. Its memory usage does scale with line length. You can use grep -I to skip binary files, which is usually sufficient.

like image 123
John Kugelman Avatar answered Oct 07 '22 07:10

John Kugelman