Bug Excessive Memory Usage - Metatrader 5 Latest Build 2190 - page 2

 

Hello,

since a few days my tester need a lot of memory. I never had a problem with memory since these few days. I deactivated all cores except 1.

The system had run out of memory very quick, after reducing a lot of Prints() in the EA and using a shorter EA-Name (!?) I think I can run an optimization again.

But only 1 core, only 2 years testing 19GB of memory usage is still crazy.


I did never had these problems, I tested periods of 20 years without problems, why now? Where are these problems coming from?


System: ASRock B450M Pro4 / AMD Ryzen 3400G / 64GB 

Problems of the high memory usage: freezing or self-killing of each application, second screen blanks, nothing clickable, sometimes unwanted reboot.


Do somebody have experience with memory consumption like this and get it solved?


yours sincerely

sb

 
Sebastian Braun :

Hello,

since a few days my tester need a lot of memory. I never had a problem with memory since these few days. I deactivated all cores except 1.

The system had run out of memory very quick, after reducing a lot of Prints() in the EA and using a shorter EA-Name (!?) I think I can run an optimization again.

But only 1 core, only 2 years testing 19GB of memory usage is still crazy.


I did never had these problems, I tested periods of 20 years without problems, why now? Where are these problems coming from?


System: ASRock B450M Pro4 / AMD Ryzen 3400G / 64GB 

Problems of the high memory usage: freezing or self-killing of each application, second screen blanks, nothing clickable, sometimes unwanted reboot.


Do somebody have experience with memory consumption like this and get it solved?


yours sincerely

sb

Two options:

  • You need to update the terminal (the current version of MetaTrader 5 x64 build 2776)
  • You need to check your code for memory leaks
 
Vladimir Karputov:

Two options:

  • You need to update the terminal (the current version of MetaTrader 5 x64 build 2776)
  • You need to check your code for memory leaks

Hello,

1. Looking here I have the latest (stable?) release: https://www.metatrader5.com/en/releasenotes

EDIT: Updated build 2776 (beta). Same issue.

2. How I should find a memory leak? My EA is not saving a lot of data. And why the memory usage is increasing over time, it did not stop anywhere, unlesss the Optimization is stopped memory consumption is increasing till the end.

Why MT5 is written like this? I don't know any other application which causes system crashes like this in 2021.

240 of 10'496 tests in optimization, memory usage is still increasing.


Update: 11.02.2021 6:00:
By reducing a lot of prints I reduced the memory consumption for a single tester run from 1-2GB to 400-500MB but running in optimization mode there is no difference. Every host gets killed.

What's new in MetaTrader 5
What's new in MetaTrader 5
  • www.metatrader5.com
Revised Popup Prices window which allows viewing financial symbol prices on any screen size. The window now supports multi-column presentation, enabling the efficient utilization of screen space.
 
Sebastian Braun :

Hello,

1. Looking here I have the latest (stable?) release: https://www.metatrader5.com/en/releasenotes

EDIT: Updated build 2776 (beta). Same issue.

2. How I should find a memory leak? My EA is not saving a lot of data. And why the memory usage is increasing over time, it did not stop anywhere, unlesss the Optimization is stopped memory consumption is increasing till the end.

Why MT5 is written like this? I don't know any other application which causes system crashes like this in 2021.


Update: 11.02.2021 6:00:
By reducing a lot of prints I reduced the memory consumption for a single tester run from 1-2GB to 400-500MB but running in optimization mode there is no difference. Every host gets killed.

Show your MQL5 code.

 
Vladimir Karputov:

Show your MQL5 code.

Hello, thank you for your time.

I found the problematic line:

void OnNewBarM1(datetime &PrevBarsM1)
  {
   if(CloseAllMethod == CloseAllMethod_C)//active M1-Trading
     {
      ////Memory Leak:
      CurrentBarData cb = new CurrentBarData();

changing to

      static CurrentBarData cb = new CurrentBarData();

solves to memory problem.

I am softwareengineer for 15 years, so I wrote the line like I would do in php or java.

Why the object is not completely removed from memory and how I write this line so I REALLY, REALLY get a new clean object ?

 
CurrentBarData cb = new CurrentBarData();
This line creates the CurrentBarData instance cb, then creates an anonymous instance via new, and assigns it by value to cb. After this line, there's no access to that anonymous instance left. Dang, memory leak.

I suggest to add Print statements or use the debugger with a breakpoint in the constructor to see how this happens.

 

When you create class object pointers with new you should always ensure they are deleted when you are finished with them, at the very least in Deinit() to avoid memory leaks.

Also, do not try and delete the pointer if it does not exist as that will give you another set of problems.

Create Your Own Trading Robot in 6 Steps!
Create Your Own Trading Robot in 6 Steps!
  • www.mql5.com
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
 
lippmaje:
This line creates the CurrentBarData instance cb, then creates an anonymous instance via new, and assigns it by value to cb. After this line, there's no access to that anonymous instance left. Dang, memory leak.

I suggest to add Print statements or use the debugger with a breakpoint in the constructor to see how this happens.

Hello,

"anonymous instance via new" - How would someone access it? Isn't it always a memory leak? Why the language allows that?

 
Paul Anscombe:

When you create class object pointers with new you should always ensure they are deleted when you are finished with them, at the very least in Deinit() to avoid memory leaks.

Also, do not try and delete the pointer if it does not exist as that will give you another set of problems.

Hello,

how would I access something without an handle or variable name?

the line is now:

CurrentBarData cb();

I have to say, I really don't like this syntax. cb is a variable name, not a function - why "()" on the variable name and not on the class name or constructor? 

I also find out accessing some methods multiple times take longer than saving it to a variable and access it from there:

datetime time = TimeTradeServer();

Pro of all this searching: The EA got really fast. I reduced the running time of the EA to about 10%.

But anyway, thank you, to all of you!

 
Sebastian Braun: changing to
    static CurrentBarData cb = new CurrentBarData();

solves to memory problem.

I am softwareengineer for 15 years, so I wrote the line like I would do in php or java.

Why the object is not completely removed from memory and how I write this line so I REALLY, REALLY get a new clean object ?

That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Reason: