简体   繁体   English

程序使用Linux中的最大内存

[英]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. 假设有3-4个在Linux中运行的高度内存密集型应用程序-例如,任何视频处理应用程序。 Those applications are using different amount of RAM and their memory access patterns are also different. 这些应用程序使用不同数量的RAM,它们的内存访问模式也不同。 Also, lets say, those applications are running in different cores. 另外,可以说,这些应用程序运行在不同的内核中。

Lets say all of these programs run for 5 seconds. 可以说所有这些程序运行5秒钟。

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-3个函数的名称。

  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. 在这方面,我需要一些帮助-Cant实际上了解如何进行。 Will cache miss calculation using perf tool help? 使用perf工具进行缓存未命中计算会有所帮助吗? I am new in linux, please write with a little explanation. 我是Linux的新手,请写点说明。 Thank you in advance. 先感谢您。

You can use profilers, Heap analysis tools for getting cpu and memory util of each method. 您可以使用分析器,堆分析工具来获取每种方法的cpu和内存使用率。

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. 这解决了您的第一个问题,即您希望大多数CPU采用哪种方法。 A process which is running for longest duration is most likely utilizing most cpu. 运行时间最长的进程最有可能利用最多的CPU。 Profiler will show you those processes. Profiler将向您显示这些过程。 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). 您可以获取heapdump或内存快照 (我不确定c,c ++代码,但可能有可用的工具和命令)。 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. 您可以使用ps (也许与watch )或tophtop来查看进程的内存消耗。 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 例如,如果您具有pid 1234、2345和3456三个进程 (您可以使用ps|grep programnamepidofpgrep等找到这些pid),则可以运行

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. 我猜您在代码中谈论的是C(或C ++)函数。 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. 当(而且这种情况经常发生)在一个函数中分配一个内存区域(调用malloc ),在另一个函数中填充该内存区域,而在另一个函数中释放该内存区域(调用free )时,则没有理由说哪个函数拥有该数据或使用该数据那记忆。

You might use valgrind (at least to check that you don't leak memory). 您可以使用valgrind (至少检查一下是否不会泄漏内存)。

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 还要注意,如果您的程序调用malloc然后free正确释放,则释放的内存并不总是返回给操作系统:大多数malloc实现都会尝试“保留” free -d内存,以便在将来malloc调用中立即重用

Read the wikipages on garbage collection , on memory leak and on memory management , in particular on C dynamic memory allocation . 阅读有关垃圾回收内存泄漏内存管理 (尤其是C动态内存分配)的Wikipage 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) . RTFM malloc(3)mmap(2) (因为malloc使用了mmap )和proc(5) (因为/proc/可用于查询内存映射和状态,请使用cat /proc/$(pidof yourprogram)/maps等...)和setrlimit(2) (这使您可以使用bashulimit内置限制,可用内存)和ps(1)watch(1)pgrep(1)pidof(1) Read also Advanced Linux Programming 另请阅读高级Linux编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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