Download MetaTrader 5
To add comments, please log in or register
Publish programs in Code Base. Improve your rating!
gr101
196
gr101 2016.04.27 17:57 
Hi,

I've got an Expert Advisor that has been setup to cycle through symbols and it chews alot of memory.

I've noticed this is when the symbols are loaded into and out of the Market Watch that is slowly creeps up.  So it's not the chart cycling through existing items in the Market Watch but more when new symbols are loaded and removed.

It's setup to do the following:

  1. OnInit sets timer to fire every 3 seconds
  2. OnTimer
    1. Get the next symbol in the list using SymbolTotal() SymbolName()
    2. Sets the chart to the next symbol using SymbolSelect() to load it into the Market Watch and ChartSetSymbolPeriod() to set the current chart to the next symbol
    3. Uses SymbolSelect() to remove any unused symbols in the Market Watch

When I remove as much custom code as possible and just use the steps above to cycle a chart through the available symbols I notice the memory usage on my machine keeps increasing.

The CPU stays and a steady rate but the memory shoots right up.

Questions:

  1. Why is this happening?
  2. Are there any work arounds or suggestions that anyone has to stop the memory shooting up?
    (i.e. rather open a new chart with ChartOpen() instead of using the existing chart with ChartSetSymbolPeriod())
  3. Is there a work around?
    (i.e. is this just the way it works)
  4. Any other suggestions?


Thanks much appreciated

Daniela Bluemel
539
Daniela Bluemel 2016.04.27 18:32  
Maybe an endless loop? Difficult to guess without any code.
gr101
196
gr101 2016.04.27 18:40  

Yes it is an endless loop but that's the idea.

To keep cycling through all the symbols.

The code below fires every 3 seconds.

Here's code snippet (it's all in one method for demo purposes):

void OnTimer()
  {
      // Find next symbol
      long symbolsTotal = SymbolsTotal(false);
      long count = GlobalVariableGet("testmem");
      if(count >= symbolsTotal)
      {
         count = 0;         
      }                  
      string newSymbol = SymbolName(count, false);
      SymbolSelect(newSymbol, true);
      count++;
      GlobalVariableSet("testmem", count);
      
      // Change chart symbol
      ChartSetSymbolPeriod(0, newSymbol, Period());         
      
      // Remove other symbols from Market Watch
      CArrayString *symbols = new CArrayString();
      for(int i = 0; i < SymbolsTotal(true); i++)
      {
         string symbol = SymbolName(i, true); // Get name from local market watch
         symbols.Add(symbol);
      }           
            
      int total = symbols.Total();
      for(int i = 0; i < total; i++)
      {
         string symbolToRemove = symbols.At(i);
         
         // Is the symbol not the current chart or about to  
         // be added?
         if(newSymbol != symbolToRemove)
         {            
            // Remove from Market Watch
            SymbolSelect(symbolToRemove, false);   
         }         
      }
      
      delete symbols;
   
  }
Daniela Bluemel
539
Daniela Bluemel 2016.04.27 19:23  
where is symbols initiated?
gr101
196
gr101 2016.04.27 19:42  

Not sure what you mean by "symbols" but the code above is in a new Expert Advisor with the ExecuteTimer() set to 3 seconds.

Here's the code (this is in an EA):

int OnInit()
  {
//--- create timer
   EventSetTimer(3);
     
//---
   return(INIT_SUCCEEDED);
  }

gr101
196
gr101 2016.04.27 20:26  
Hi,

I've added the full script see attached file "ForumQuestion1.mq5".

Steps to recreate the error:

  1. Open the attached script in Metaeditor (it's safe) for Metatrader 5
  2. Open Task Manager in Windows
    1. Look for "Metatrader 5 Client Terminal" and note the amount of memory
  3. Run the script and watch the memory column in Task Manager

The memory slowly increases as the script cycles through the symbols.

Files:
Alain Verleyen
Moderator
30323
Alain Verleyen 2016.04.27 20:49  
gr101:
Hi,

I've added the full script see attached file "ForumQuestion1.mq5".

Steps to recreate the error:

  1. Open the attached script in Metaeditor (it's safe) for Metatrader 5
  2. Open Task Manager in Windows
    1. Look for "Metatrader 5 Client Terminal" and note the amount of memory
  3. Run the script and watch the memory column in Task Manager

The memory slowly increases as the script cycles through the symbols.

I tried your code and don't have memory issue.

MT5 Build 1325.

gr101
196
gr101 2016.04.27 22:13  

Hi Alain,

Thanks for the response.

I'm on the same build. The memory increase is slow. It takes a few minutes. Basically it increases gradually.

See the attached file "Memory increase.docx.png", I've got some screenshots in there showing it build up over time.

I'm on the same build "MT5 Build 1325"

Machine info:

  • Windows 8.1 Enterprise
  • 64bit OS
  • 16Gb RAM
  • i7 processor

Thanks

Files:
Alain Verleyen
Moderator
30323
Alain Verleyen 2016.04.27 22:20  
gr101:

Hi Alain,

Thanks for the response.

I'm on the same build. The memory increase is slow. It takes a few minutes. Basically it increases gradually.

See the attached file "Memory increase.docx.png", I've got some screenshots in there showing it build up over time.

I'm on the same build "MT5 Build 1325"

Machine info:

  • Windows 8.1 Enterprise
  • 64bit OS
  • 16Gb RAM
  • i7 processor

Thanks

I am on Windows 10 if that makes any difference.

Your script is running since some minutes before my previous post. Currently the memory usage is lower than when I started it (280 Mb against 300 Mb).

EDIT: I just checked againt and it's now 189 Mb !!! still decreasing :D

EDIT2: Running on Metaquotes Demo-Server.

gr101
196
gr101 2016.04.27 22:22  

Thanks for the feedback.

Think I will upgrade to Windows 10 and see if that makes a difference.

gr101
196
gr101 2016.04.27 22:24  

I put it on Windows Server 12 R2 and that also pushes the memory up.

Let me monitor this for a while and give feedback on this thread.

/ /1234
To add comments, please log in or register