Matlab - 一个程序的内存使用情况 [英] Matlab - Memory usage of a program

查看:567
本文介绍了Matlab - 一个程序的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在MATLAB中实现不同的信号处理算法,以后用C ++实现其中的一种。要在这些之间进行选择,我将执行一些测试,其中一个是内存使用检查。也就是说,我想看看不同算法使用多少内存。由于实现被划分为子函数,所以我在收集有关实际内存使用情况的信息时遇到了问题。



这是我迄今为止所尝试的:


  1. 我使用分析器来检查每个函数的内存使用情况。
    问题:它只显示分配的内存使用情况。它没有显示例如每个函数中变量的内存使用情况。

  2. 我在每个函数的末尾都使用了whos来收集有关函数工作空间中所有变量的信息。然后我将这些添加到全局变量中。
    问题:即使在执行完成后,全局变量也在不断增加,它似乎永远不会停止。


现在我的问题。我怎样才能以一种相当简单的方式获得关于我程序的内存使用情况的信息,包括所有函数?



最好的祝愿


<我认为你在每个函数结束时调用 whos 的策略(就在它返回之前)是一个好的;但也许你想把结果打印到屏幕上而不是全局。如果它持续增长,那么你可能有一个被你称为不知道的回调函数,它包括你的 whos 调用之一。通过打印到屏幕上(也许在它之前包括一个 disp(在<函数名称> ***'结尾的'****内存使用情况)),你会发现为什么它继续前进。



使用 memory 的替代方法有点帮助,但它提供了关于可用内存的信息,以及Matlab使用的所有内存(不仅仅是变量)。



当然,内存使用的任何快照都不一定会抓住高峰 - 这可能是一个像

  x = sum(repmat(A,[1000 1])); 

需要相当大的峰值内存使用量(当您复制矩阵 A 1000次),但是之前或之后的内存快照(或运行 whos )不会告诉你发生了什么......


I'm currently implementing different signal processing algorithms in MATLAB, to later implement one of these in C++. To choose between these I'm going to perform a number of tests, one being a memory usage check. That is, I want to see how much memory the different algorithms use. Since the implementations are divided in to sub-function, I'm having problems collecting information about the actual memory usage.

This is what I've tried so far:

  1. I've used the profiler to check memory usage of every function. Problem: It only shows allocated memory usage. It doesn't show e.g. memory usage of variables in every function.

  2. I've used whos at the end of every function to collect information about all the variables in the workspace of the functions. I then added these to a global variable. Problem: The global variable keeps increasing even after the execution is done and it seems to never stop.

Now to my question. How can I, in a rather simple way, get information about the memory usage of my program, all functions included?

Best regards

解决方案

I think your strategy to call whos at the end of every function (just before it returns) is a good one; but maybe you want to print the result to the screen rather than a global. If it "keeps increasing", then maybe you have a callback function that is being called unbeknownst to you, and that includes one of your whos calls. By printing to screen (and maybe including a disp('**** memory usage at the end of <function name> ***') just before it, you will find out why it "keeps going".

The alternative of using memory is somewhat helpful, but it gives information about "available" memory, as well as all the memory used by Matlab (not just the variables).

Of course any snapshot of memory usage doesn't necessarily grab the peak - it's possible that a statement like

x = sum(repmat(A, [1000 1]));

would require quite a large peak memory usage (as you replicate the matrix A 1000 times), yet a snapshot of memory (or running whos) right before or after won't tell you what just happened...

这篇关于Matlab - 一个程序的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆