How to release memory in multitimeframe indicators

 

I noticed, that the first read attempt from other timeframe allocates a lot of memory, probably all the candles.

I did not notice it unless I deployed the indicator with a broker, that supplies more than 250000 candles in small timeframes.

The problem is, that with every timeframe and every instance of the indicator memory allocation is increasing, and it may hit the memory limit.

For example, a simple multitimeframe indicator with no buffers, expecting very low memory footprint needs 30MB after a series of calls of iBarsor ArrayCopyRates.

And every additional instance of the indicator takes memory again.

I did not find a solution how to lower the memory footprint, or how to release the memory after use.

Has anyone experience with it?

 
Ovo:
I noticed, that the first read attempt from other timeframe allocates a lot of memory, probably all the candles.

I did not notice it unless I deployed the indicator with a broker, that supplies more than 250000 candles in small timeframes.

The problem is, that with every timeframe and every instance of the indicator memory allocation is increasing, and it may hit the memory limit.

For example, a simple multitimeframe indicator with no buffers, expecting very low memory footprint needs 30MB after a series of calls of iBarsor ArrayCopyRates.

And every additional instance of the indicator takes memory again.

I did not find a solution how to lower the memory footprint, or how to release the memory after use.

Has anyone experience with it?

If you are using iCustom() call no way for you to control it (the only way is to change parameters for the basic calling code when it is unloaded as well as the called indicator). If you are using just CopyRates(), then it is obvious one more big BUG since once when the array is not needed any more the garbage collector should clean up the things (there is nothing similar to free(array) in mql) and it does not

The only thing (in case of CopyRates()) that comes in mid would be that you use ArrayResize(array,0) when you do not need the array any more

 
mladen:
If you are using iCustom() call no way for you to control it (the only way is to change parameters for the basic calling code when it is unloaded as well as the called indicator). If you are using just CopyRates(), then it is obvious one more big BUG since once when the array is not needed any more the garbage collector should clean up the things (there is nothing similar to free(array) in mql) and it does not The only thing (in case of CopyRates()) that comes in mid would be that you use ArrayResize(array,0) when you do not need the array any more

Actually there is documented FreeArray() thingy, and it really resizes the array to zero, but it has absolutely no impact on allocated memory.

My expectation is that the first call to other timeframe may allocate memory if no chart for the particular TF is open. But what I do not understand is why the memory is allocated again and again with the second and third indicator and so on (and they still call it virtual copy). Finally I tried to enter a bug report, lets see what their explanation is.

 
Ovo:
Actually there is documented FreeArray() thingy, and it really resizes the array to zero, but it has absolutely no impact on allocated memory. My expectation is that the first call to other timeframe may allocate memory if no chart for the particular TF is open. But what I do not understand is why the memory is allocated again and again with the second and third indicator and so on (and they still call it virtual copy). Finally I tried to enter a bug report, lets see what their explanation is.

Ovo

Would you mind copying the help description for FreeArray()

I do not have it in my terminal (build 670) not a word about it - and a search returns me a "no topics found" when I try to get it in the search tab

 

Disregard

It is ArrayFree() not FreeArray() and that is why I could nor find it

 

As usual :):)

Note

The need for using ArrayFree() function may not appear too often considering that all used memory is freed at once and main work with the arrays comprises the access to the indicator buffers. The sizes of the buffers are automatically managed by the terminal's executive subsystem.

I wonder what do buffers have to do with ArrayFree() when it can not be applied to buffers

PS: metatrader 4 can not have 0 buffers. Even when you do not allocate any buffers at all, it will allocate one buffer

 
mladen:
Disregard It is ArrayFree() not FreeArray() and that is why I could nor find it

Sorry, my bad. The code was like that. No impact on memory, either in debug or attached.

MqlRates checkRate[];

int recentCount = count;

count = ArrayCopyRates(checkRate, ticker, timeframe);

bool result = ((int)MathAbs(count - recentCount) > 2);

if (count > 0) {

startTime = checkRate[count-1].time;

endTime = checkRate[0].time;

} else {

startTime = LONG_MAX;

endTime = LONG_MAX;

}

ArrayFree(checkRate);

 
mladen:
As usual :):)

I wonder what do buffers have to do with ArrayFree() when it can not be applied to buffers

PS: metatrader 4 can not have 0 buffers. Even when you do not allocate any buffers at all, it will allocate one buffer

Thank you, I did not know that information about a default buffer allocation.

I am trying to understand the need of ArrayFree as well, from my point of view the array should be destroyed behind a block boundary in every case. It is probably useful only if declared in global scope.