简体   繁体   中英

Program using maximum memory in linux

Let's say , there are 3-4 highly memory intensive applications running in linux - for example,say, any video processing application. Those applications are using different amount of RAM and their memory access patterns are also different. Also, lets say, those applications are running in different cores.

Lets say all of these programs run for 5 seconds.

What I am trying to find -

  1. Which functions among these applications using maximum memory at a particular instant? I need the name of first 2-3 functions which are taking maximum memory resource.

  2. If multiple functions need a high amount of memory ( which is above a threshold limit) at the same instant, what are their names and how long they need that high amount of memory.

I need some help on this – Cant actually understand how to proceed. Will cache miss calculation using perf tool help? I am new in linux, please write with a little explanation. Thank you in advance.

You can use profilers, Heap analysis tools for getting cpu and memory util of each method.

Profilers are tools which will add some loggers/debug statements/custom code to your application code and will tell you time taken by each method,query in your application. This solves your first question ie you want most cpu taken by which method. A process which is running for longest duration is most likely utilizing most cpu. Profiler will show you those processes. After that you can analyze method by yourself by putting debug statements and loggers to drill down.

Programs are using memory for keeping objects, variables. You can find out which method/object is taking high memory by looking only at memory snapshot. You can get heapdump or memory snapshot (I am not sure about c,c++ code but probably there are tools, commands available). Once you get those snapshots analyze them for objects/methods occupying largest memory block.

After finding these culprits, try to improve them using better hw,sw, libraries, perf tunings etc.

I hope you are clear now :)

You could use ps (perhaps with watch ) or top or htop to look at memory consumption of processes. For example, if you have three processes of pid 1234, 2345, and 3456 ( you could find these pids using ps|grep programname or pidof or pgrep etc...) you might run

watch ps -F 1234 2345 3456

Which functions among these applications using maximum memory at a particular instant?

If multiple functions need a high amount of memory

I guess that you are speaking of C (or C++) functions in your code. Then both questions above don't have any sense: memory consumption is a global property of your program. When (and this happens often) a memory zone is allocated in one function (calling malloc ), filled in another one, and released in yet another one (calling free ) it does not have sense to say which function is owning that data or consuming that memory.

You might use valgrind (at least to check that you don't leak memory).

Also notice that if your program calls malloc then free correctly, the freed memory is not always given back to the OS: most malloc implementations would try to "keep" free -d memory to be immediately reusable at future calls to malloc

Read the wikipages on garbage collection , on memory leak and on memory management , in particular on C dynamic memory allocation . RTFM malloc(3) & mmap(2) (since mmap is used by malloc ) & proc(5) (since /proc/ can be used to query the memory map and state, use cat /proc/$(pidof yourprogram)/maps etc...) & setrlimit(2) (which enables you to limit, eg with the ulimit builtin of bash , the available memory) & ps(1) & watch(1) & pgrep(1) & pidof(1) . Read also Advanced Linux Programming

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM