Memory Leak???

 

Hello Experts,

I have a strange one. I call several custom indicators at the beginning of a new candle inside of an EA. While running this in backtest I see that one of the indicators keeps getting loaded at each new candle (each time it is encountered in the code). I stop the EA (in Backtest), I see the one indicator was removed for each time it was called. Only one of the indicators show this problem, while the other indicator is only called once and removed only once. I also see my computer slow down, if the back test is run for long period. I check the CPU usage as the backtesting slows and I find the one of the CPU's is at 100% (on a quad processor).

Ok a little more details. I call indicator-1 once while I call indicator-2 two times. The only difference in each call of the indicator-2 is the buffer number (0 and 1).

I have tried to used HideTestIndicator(true) and many places in the code an it has no effect.

Even though with our powerful computers and large memories, this shouldn't be a problem for quite a while, but I don't think this should be a problem in the first place.

I cannot figure out how to overcome this problem. Any suggestions?

 
jimsabast:

Hello Experts,

I have a strange one. I call several custom indicators at the beginning of a new candle inside of an EA. While running this in backtest I see that one of the indicators keeps getting loaded at each new candle (each time it is encountered in the code). I stop the EA (in Backtest), I see the one indicator was removed for each time it was called. Only one of the indicators show this problem, while the other indicator is only called once and removed only once. I also see my computer slow down, if the back test is run for long period. I check the CPU usage as the backtesting slows and I find the one of the CPU's is at 100% (on a quad processor).

The Strategy Tester running at full speed (32 on the slider) will use all of one core ( in effect even if not in reality ) this is normal behaviour, do you want the Strategy Tester to run slower than it could ?

How are you detecting the memory leak ? Performance Monitor ? Process Explorer ? Task Manager ?

 
RaptorUK:

The Strategy Tester running at full speed (32 on the slider) will use all of one core ( in effect even if not in reality ) this is normal behaviour, do you want the Strategy Tester to run slower than it could ?

How are you detecting the memory leak ? Performance Monitor ? Process Explorer ? Task Manager ?

Summary: It seems the memory is being filled up by MT4 adding indicator-2 over-and-over, at each time it's called in the EA code. The number of candles it covers drops by 100x in real time. So, something is different between the start and later in a backtest. Visual performance drop at the same time as disk access increases. I want it to run as fast as possible and don't want the slow down. indicator-1 behaves differently than indicator-2.

Yes you are right about the CPU talking 100% as long as it is run at maximum speed (32 on the slider). What I see however is the number of candles the chart covered slows down (visual performance). That is when I first start the back test, it runs through the candles so fast I can't even see it trading. As the back test continues it slows to a rate of one candle / second. Is that a 100:1 slow down in the number of candles it covers in a second? I don't know the exact ratio of slow down but it is huge and I don't think it should slow down. So, yea the CPU is working as hard as it can, but the actual performance on the chart dropped. Something is different in the beginning of the backtest than later in the backtest.

Also, I look at the disk access during the run. At first its near zero, then the disk access sharply rises and the candles covered/second drops sharply. It acts as if it had ran out of memory and is using the disk as virtual memory. This is not the 'classic' memory leak, but it is acting like its is leaking.

The only connection I can see is that it adds one of the indicators (I am using 2) over and over as shown in the MT4 journal entries. When I stop the backtest, the journal show all of the added indicators being removed. One of the indicators (I call it indicator-2 here) show a huge list of it being removed. Running the back test slowly, I see that the indicator-2 is added each time it is used in the MQ4 code. The long list of removed indicator equals the number of times it was added. So, it seems MT4 adds one of the two indicator repeatedly, filling up my RAM and then uses the disk as virtual memory. -- like a memory leak --

The difference in the MT4 EA code is indicator-2 is called twice while indicator-1 only once for each new candle. The only difference between the two times I call indicator-2 is it return buffer 1 and in the next statement it returns buffer 0.

I also played with HideTestIndicator(true). I placed it right after the calling of the indicators and the journal record shows the indicator being loaded then removed at each new candle. Then when I stop the back test I don't have the huge list of indicator-2 being removed. This process is makes the backtest run slow from the start and doesn't change during the backtest. (That is it covers about 4 candles / second instead of the 100 or so.) I am guessing the indicators are being recalculated for the whole chart each time it is encountered and then is completely removed when it hits the HideTestIndicator(true) statement. At any rate, it is a way to avoid multiple placing of indicator-2 multiple times, but the performance is slowed too much from the start.

I have moved the HideTestIndicator statement around and it does make a difference in performance, how many times indicator-2 is called and removed, and the overall performance, but it seems I get one of the two issue described above; either the memory leak problem or the slow performance from the start.

I can't figure out how to solve both issues.

 

I would do the same test but subsitute indicator 2 with a standard 2 buffer indicator like a bollinger bands and see if it still does the same thing, if it doesn't you will know there is something wrong with the indicator 2.

If it still does that with a standard indicator, I would then try to eliminate the possibility the problem is caused by calling the same indicator twice, I dont recall anyone else reporting an issue but you never know. So I would make a copy of indicator 2 and give it a new name, then call indicator 2 for the first buffer, and call indicator 2 copy for the second buffer and see if that makes a difference.

 
jimsabast:backtest I see that one of the indicators keeps getting loaded at each new candle
Continous reloading of custom indicator in EA - MQL4 forum
 
If it was caused by missing chart data would it not cause all the called indicators to keep reloading and not just one of them ?
 
different pair
Reason: