Is there a comprehensive list of MT5 memory usage reduction ideas / strategies?

 

After turning off news, reducing bars in charts and reducing symbols in the market watch, what other things can be done?

Appreciate any new ideas, thanks.

Jeff

 
  1. Your title and question implies you have a memory leak.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

    I have (MT4) 60+ charts with nine (9) indicators (including one MTF) open on my (4 core) laptop, averaging 15% CPU, 109 MB RAM, 10 MB/s disk. 2020.06.19

  2. Hard to give suggestions when we can't see your code. 

  3. Get your handle(s) only in OnInit.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  4. Move your Copy…() arrays out of global space into the function so its memory can be released on return.

  5. Remove unnecessary extra buffer(s). If you don't refer to past values (only previous one) you don't need the buffer, only a temporary and a static. The static contains the previous run's value. The temporary (initialized at start of the loop from the static) updates to the current bar's value and you update the static at end of loop, except on bar zero. If you update on bar zero, you break the precondition (previous value) for next iteration.

 
William Roeder:
  1. Your title and question implies you have a memory leak.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

    I have (MT4) 60+ charts with nine (9) indicators (including one MTF) open on my (4 core) laptop, averaging 15% CPU, 109 MB RAM, 10 MB/s disk. 2020.06.19

  2. Hard to give suggestions when we can't see your code. 

  3. Get your handle(s) only in OnInit.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  4. Move your Copy…() arrays out of global space into the function so its memory can be released on return.

  5. Remove unnecessary extra buffer(s). If you don't refer to past values (only previous one) you don't need the buffer, only a temporary and a static. The static contains the previous run's value. The temporary (initialized at start of the loop from the static) updates to the current bar's value and you update the static at end of loop, except on bar zero. If you update on bar zero, you break the precondition (previous value) for next iteration.

ok, this is helpful, William, thanks.

I've done the same with MT4 in the past (48 charts x 16 instances running on 1 VPS) but I moved to MT5 after 9 years for a lot of reasons. 

I think you have pinpointed (one of) the problem(s)...with the indicators.  I instantiate 100's of them and get the handle (iBands mostly) from a function called from OnInit() so I can rule that out according to your post.  However, your admonition to look at how I am handling the buffer intrigues me.  I only want the latest value from the upper and lower band so I use the simple function below to return them to me.  Is there a better way to do this if I only want the latest value? (I'm already using a static, local array as the documentation suggests)  This routine may get called 1000's of times in a second.

double GetBBValue(int handle, Indicator part)
{
   double BB[1];

   if(CopyBuffer(handle,(int)part,0,1,BB)<0)
   {
     Print("GetBBValue() error part ",(int)part," error code ",GetLastError());
     return(-1);
   }
   
   return(BB[0]);
}

I believe your advice in point 4 is referring to custom indicators and I'm not using those in this EA so I have no control there.

The rest of my memory usage seems to be related to a couple of GB of global, statically allocated memory.  (I prefer sparse arrays for quick hashing as processing speed is most important to me.  I don't do a lot of dynamic memory management any more...)  At this point, I mostly want to jettison anything in MT5 that I can and is not needed...


btw, I have always had memory leaks (30 years of coding will do that to a person ;-)

Thanks William.

Jeff

 

algodevtrader:

(I'm already using a static, local array as the documentation suggests)  This routine may get called 1000's of times in a second.

btw, I have always had memory leaks (30 years of coding will do that to a person ;-)

  1. Your posted code is not using a static array; and shouldn't be. The documentation say “statically allocated” array, which means fixed length, not dynamic. Two completely different things.
  2. Why are you calling it more than once per tick? You are definitely not getting thousands of ticks per second from your broker.
  3. I've been coding for 47 years, and never have had memory leaks (in debugged SW).
 
William Roeder:
  1. Your posted code is not using a static array; and shouldn't be. The documentation say “statically allocated” array, which means fixed length, not dynamic. Two completely different things.
  2. Why are you calling it more than once per tick? You are definitely not getting thousands of ticks per second from your broker.
  3. I've been coding for 47 years, and never have had memory leaks (in debugged SW).

Off-topic, but have you ever created a personal standard library from scratch, or is extending the built-in library enough? I plan to go down that road. 

 
William Roeder:
  1. Your posted code is not using a static array; and shouldn't be. The documentation say “statically allocated” array, which means fixed length, not dynamic. Two completely different things.
  2. Why are you calling it more than once per tick? You are definitely not getting thousands of ticks per second from your broker.
  3. I've been coding for 47 years, and never have had memory leaks (in debugged SW).

I call the function multiple times per tick because I am using it on many custom symbols with different sets of time series.  The profiler shows me that CopyBuffer is one of the largest time-sinks I have and I am trying to get processing time per tick down as much as possible.  Any suggestions appreciated.  You're right about the array declarations of course.

The crack about memory leakage was meant to be a (bad) joke...I bow to your Senior Coder status!!  ;->  I haven't really worried about memory leaks since I stopped using C in the 80's.

Thanks and happy trading.

Jeff

 
algodevtrader: I call the function multiple times per tick because I am using it on many custom symbols

Stop calling it on symbols that haven't changed; only run when something has. React to a button clicked inside another chart - Indices - MQL4 programming forum

Reason: