How to release memory after massive CopyRates()? - page 2

 

Of course cache is a great and absolutely correct idea for CopyRates() process. No doubt.

All what I'm searching for is a little bit more flexability working with cache. Like CacheFree() or something. May be I'm missing something in MQL5 world?

Regarding particulary my case, as I already said I need to test about 1000 CFD symbols on different timeframes. CopyRates() for each kills my memory very fast. Particulary in my case I would be happy to clear that cache after each symbol test.

 
terminalstat:

  I have about 1000 CFD symbols to scan. In various timeframes for each. A lot of memory required) 

If you have trouble with this causing too heavy a load i recommend you to declare the variables in a designated function itself so that it get's destroyed after you exit the function call and you only have to store the outcome of the calculation. 

There is no need to call and keep it all since you are only interested in the outcome of the calculation (usually).

This way you can keep it feather-light and blazing-fast.

Otherwise it can get bulky and sluggish real fast.

 
Marco vd Heijden:

If you have trouble with this causing too heavy a load i recommend you to declare the variables in a designated function itself so that it get's destroyed after you exit the function call and you only have to store the outcome of the calculation. 

There is no need to call and keep it all since you are only interested in the outcome of the calculation (usually).

This way you can keep it feather-light and blazing-fast.

Otherwise it can get bulky and sluggish real fast.

In theory, it should not change anything compared to the posted code, as arrays are already declared locally in the for loop scope which is equivalent to a function call in matter of scope.

And anyway, if the underlying issue is a cache issue, it will not change anything, as it's not related to variables declaration.

 

Exactly! May be I was not clear enough. The problem that CopyRates() create cache in memory (what is good), but it's not getting destroyed if we don't need it anymore. And this is a problem for me.

Here is a new version of script, more informative:

const int testMaxDepth = 100000;
const int symbolsMax=15;

//+------------------------------------------------------------------+
void OnStart(){

//----check MaxBars limit:
   if(TerminalInfoInteger(TERMINAL_MAXBARS)<testMaxDepth+1000){
      Print("insufficient MaxBars in terminal settings, adjust to ",testMaxDepth+1000,", restart terminal exiting...");
      return;
   } 
     
   ENUM_TIMEFRAMES period=PERIOD_M1;
   
   //-save ammount of FREE memory available: 
   int mem_start=TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE),mem=0;
   
//----going through SYMBOLS:
   for(int s=0;s<SymbolsTotal(false);s++){ 

      const string symbol=SymbolName(s,false);

      //-one iteration memory consumption:
      mem=TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE);

      MqlRates rates[];
      ArraySetAsSeries(rates,true);
      ArrayResize(rates,testMaxDepth);
      if(CopyRates(symbol,period,0,testMaxDepth,rates)<testMaxDepth){Print(GetLastError(),": CopyRates failed, check TERMINAL_MAXBARS var");return;}
      
      Print("copied ",testMaxDepth," ",symbol," ",EnumToString(period)," bars, took ",mem-TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)," Mb of memory");
 
      
      //-some calculations goes here-//


      ArrayFree(rates);
      
      if(IsStopped()||s>=symbolsMax){break;}
   }
   
   Print("Program exited, total memory occupied by last run: ", mem_start-TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)," Mb");
   Print("Memory stay freezed whatever you do. Only terminal exit can release it.");   
}

Possible output for FIRST run after terminal launch (all the rest would return zero because of already cached data):

2019.12.01 22:57:39.977 test-CopyRatesThroughSynbols (EURUSD,H1)        Program exited, total memory occupied by last run: 111 Mb
2019.12.01 22:57:39.977 test-CopyRatesThroughSynbols (EURUSD,H1)        Memory stay freezed whatever you do. Only terminal exit can release it.
 
anyone?
 
Documentation on MQL5: Common Functions / ZeroMemory
Documentation on MQL5: Common Functions / ZeroMemory
  • www.mql5.com
Common Functions / ZeroMemory - Reference on algorithmic/automated trading language for MetaTrader 5
 
delete CArrayObj? FreeMode?
Documentation on MQL5: Standard Library / Data Collections / CArrayObj / FreeMode
Documentation on MQL5: Standard Library / Data Collections / CArrayObj / FreeMode
  • www.mql5.com
Setting the memory management flag is an important part in the CArrayObj class use. Since the array elements are pointers to dynamic objects, it is important to determine what to do with them when removing from the array. If the flag is set, removing an element from the array, the element is automatically deleted by the delete operator. If the...
 
If you need some calculations, adding them into a buffer variable and dividing for a counter could optimize and save a lot of memory... Something like reading row by row in SQL...
 

This caching mechanism is normal for EA testing.

The MT5 terminal will release the memory occupied by the cache after 5 minutes if the tester agent does not receive a new test task.

It has nothing to do with the use of the CopyRates() function.

But I don't know how a script works, or whether is it related at all...

 
tickfenix:

This caching mechanism is normal for EA testing.

The MT5 terminal will release the memory occupied by the cache after 5 minutes if the tester agent does not receive a new test task.

It has nothing to do with the use of the CopyRates() function.

But I don't know how a script works, or whether is it related at all...

Please read the topic, it's not about the Strategy Tester.
Reason: