Memory Leak issue

 

why does my code cause a memory leak. I coded it such that it will run the indicator on all the bars and then stop accepting future ticks.


 i = Bars;
    Print("Year: "+TimeYear(Time[i])+" Month: "+TimeMonth(Time[i])+" Day: "+TimeDay(Time[i])+"Hour: "+TimeMonth(Time[i]));
     while(i > 0)
     {
       string match = List[0];
      
       m = 0;
       for(j = size - 1; j >= 0; j--)
       {
         if( (Close[i+j+1] - Open[i+j+1]) > 0)match = StringSetChar(match, m,'u');
         else match = StringSetChar(match, m,'d');
         //Print("Time of Hour "+TimeHour(Time[i+j]));for debugging
         m++;
       }
      // Print("pattern found is "+match+" at hour " +TimeHour(Time[i]) );//for debugging
      
       
       for(j = 0; j < ArraySize(List); j++)
       {
         if(List[j] == match)
         {
           // Print("The minute and hour of this Hourly is "+TimeMinute(Time[i])+" and "+TimeHour(Time[i]) );
           // Print("The minute and hour of this Minute is "+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60))))+" and "+TimeHour(iTime(Symbol(),PERIOD_M1,i*60+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60)))) );
            
            m = (i+1)*60+TimeMinute(iTime(Symbol(),PERIOD_M1,(i+1)*60));
           // Print("Time of m is hour: "+TimeHour(iTime(Symbol(),PERIOD_M1,m+1))+" min: "+TimeMinute(iTime(Symbol(),PERIOD_M1,m+1)));
           // Print("closing price is "+iClose(Symbol(),PERIOD_M1,m+1));
            k=m+1;
            while(m>0)
            {
               if(iClose(Symbol(),PERIOD_M1,m) <= iOpen(Symbol(),PERIOD_M1,m) )
               {
                  if((iHigh(Symbol(),PERIOD_M1,m)>= (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3))&&(iLow(Symbol(),PERIOD_M1,m)> (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3)))
                  {
                     //Print("broke up at "+iHigh(Symbol(),PERIOD_M1,m));
                     Frequency[j]++;
                     ListData[j][1]++;
                     break;
                  }
                  else if((iLow(Symbol(),PERIOD_M1,m) <= (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3))&&(iHigh(Symbol(),PERIOD_M1,m)< (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3)))
                  {
                   //Print("broke dn at "+iLow(Symbol(),PERIOD_M1,m));
                   Frequency[j]++;
                   ListData[j][0]++;
                   break;
                  }
               }
               else if(iClose(Symbol(),PERIOD_M1,m) >= iOpen(Symbol(),PERIOD_M1,m))
               {
                   if((iLow(Symbol(),PERIOD_M1,m) <= (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3))&&(iHigh(Symbol(),PERIOD_M1,m)< (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3)))
                  {
                   //Print("broke dn at "+iLow(Symbol(),PERIOD_M1,m));
                   Frequency[j]++;
                   ListData[j][0]++;
                   break;
                  }
                  else if((iHigh(Symbol(),PERIOD_M1,m)>= (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3))&&(iLow(Symbol(),PERIOD_M1,m)> (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3)))
                  {
                     //Print("broke up at "+iHigh(Symbol(),PERIOD_M1,m));
                     Frequency[j]++;
                     ListData[j][1]++;
                     break;
                  }
               
               }
             m--;
            }
         }
       }
       
        
     i--;
     Print("i is: "+i);
     }
 

can u explain please what is the value of size? & where is this var initialize

for(j = size - 1
 
jeemba2012:
why does my code cause a memory leak. I coded it such that it will run the indicator on all the bars and then stop accepting future ticks.
Why do you think it has a leak? It created several large arrays, filled them up, then started ignoring ticks. It's still running and using the arrays.
 
qjol:

can u explain please what is the value of size? & where is this var initialize


size is initialized as an external int for the size of a string. strings of size n has characters at positions 0 through n-1.

extern int size = 3;//This is above init()
 
WHRoeder:
Why do you think it has a leak? It created several large arrays, filled them up, then started ignoring ticks. It's still running and using the arrays.

I think there is a leak because it freezes my mt4 and when I use ctrl alt delete and look at my terminal process, the CPU usage increases to 100%.

I am technically collecting data so my intentions is not to make it like a real indicator to keep waiting for ticks. I just want to collect data for all the bars on the chart except the current.

it works if I set i = 20; sometime i = 1000; and freezes on numbers greater.

thanks for your assistance

 
jeemba2012:

why does my code cause a memory leak. I coded it such that it will run the indicator on all the bars and then stop accepting future ticks.

The first two lines show an obvious problem ...

 i = Bars;
 Print("Year: "+TimeYear(Time[i])+" Month: "+TimeMonth(Time[i])+" Day: "+TimeDay(Time[i])+"Hour: "+TimeMonth(Time[i]));

The are

i=Bars

bars on the chart. You are only allowed to index arrays up to Bars-1 because the first bar is Time[0] for example.

You are over-running the length of the arrays and this can crash the program.

 
dabbler:

The first two lines show an obvious problem ...

The are

i=Bars

bars on the chart. You are only allowed to index arrays up to Bars-1 because the first bar is Time[0] for example.

You are over-running the length of the arrays and this can crash the program.


Here is the rest of the code from the while loop on down. If I comment the while loop there is no freezing so I assume the problem lies within the while loop.

i = Bars-1;
    Print("Year: "+TimeYear(Time[i])+" Month: "+TimeMonth(Time[i])+" Day: "+TimeDay(Time[i])+"Hour: "+TimeMonth(Time[i]));
     while(i > 0)
     {
       string match = List[0];
      
       m = 0;
       for(j = size - 1; j >= 0; j--)
       {
         if( (Close[i+j+1] - Open[i+j+1]) > 0)match = StringSetChar(match, m,'u');
         else match = StringSetChar(match, m,'d');
         //Print("Time of Hour "+TimeHour(Time[i+j]));for debugging
         m++;
       }
      // Print("pattern found is "+match+" at hour " +TimeHour(Time[i]) );//for debugging
      
       
       for(j = 0; j < ArraySize(List); j++)
       {
         if(List[j] == match)
         {
           // Print("The minute and hour of this Hourly is "+TimeMinute(Time[i])+" and "+TimeHour(Time[i]) );
           // Print("The minute and hour of this Minute is "+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60))))+" and "+TimeHour(iTime(Symbol(),PERIOD_M1,i*60+TimeMinute(iTime(Symbol(),PERIOD_M1,i*60)))) );
            
            m = (i+1)*60+TimeMinute(iTime(Symbol(),PERIOD_M1,(i+1)*60));
           // Print("Time of m is hour: "+TimeHour(iTime(Symbol(),PERIOD_M1,m+1))+" min: "+TimeMinute(iTime(Symbol(),PERIOD_M1,m+1)));
           // Print("closing price is "+iClose(Symbol(),PERIOD_M1,m+1));
            k=m+1;
            while(m>0)
            {
               if(iClose(Symbol(),PERIOD_M1,m) <= iOpen(Symbol(),PERIOD_M1,m) )
               {
                  if((iHigh(Symbol(),PERIOD_M1,m)>= (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3))&&(iLow(Symbol(),PERIOD_M1,m)> (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3)))
                  {
                     //Print("broke up at "+iHigh(Symbol(),PERIOD_M1,m));
                     Frequency[j]++;
                     ListData[j][1]++;
                     break;
                  }
                  else if((iLow(Symbol(),PERIOD_M1,m) <= (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3))&&(iHigh(Symbol(),PERIOD_M1,m)< (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3)))
                  {
                   //Print("broke dn at "+iLow(Symbol(),PERIOD_M1,m));
                   Frequency[j]++;
                   ListData[j][0]++;
                   break;
                  }
               }
               else if(iClose(Symbol(),PERIOD_M1,m) >= iOpen(Symbol(),PERIOD_M1,m))
               {
                   if((iLow(Symbol(),PERIOD_M1,m) <= (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3))&&(iHigh(Symbol(),PERIOD_M1,m)< (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3)))
                  {
                   //Print("broke dn at "+iLow(Symbol(),PERIOD_M1,m));
                   Frequency[j]++;
                   ListData[j][0]++;
                   break;
                  }
                  else if((iHigh(Symbol(),PERIOD_M1,m)>= (iClose(Symbol(),PERIOD_M1,k) + UsePoint*3))&&(iLow(Symbol(),PERIOD_M1,m)> (iClose(Symbol(),PERIOD_M1,k) - UsePoint*3)))
                  {
                     //Print("broke up at "+iHigh(Symbol(),PERIOD_M1,m));
                     Frequency[j]++;
                     ListData[j][1]++;
                     break;
                  }
               
               }
             m--;
            }
         }
       }
       
        
     i--;
     Print("i is: "+i);
     }
     
      /* 
        for(j = 0; j<ArraySize(List); j++)
        {
            if(Frequency[j] > 0)
            {
              Print("Pattern "+List[j]+" happened "+Frequency[j]+"times and bup "+ListData[j][1]+" a bdn "+ListData[j][0]);
            }
        }
        Print("Frequency list");
        for(i = 0; i < ArraySize(Frequency); i++)
        {
          Print("the string / Frequency / bup / bdn: "+List[i]+" and "+Frequency[i]+" / "+ListData[i][1]+" / "+ListData[i][0]);
        }
        */
        for(i = 0; i < ArraySize(Frequency); i++)
        {
            double currentMax = Frequency[i];
            string currentString = List[i];
            int currentBDN = ListData[i][0];
            int currentBUP = ListData[i][1];
            int currentMaxIndex = i;
          
            for(j = i +1; j < ArraySize(Frequency); j++)//Find Max or next max in the list
            {
               if(currentMax < Frequency[j])
               {
                 currentMaxIndex = j;
                 currentMax = Frequency[j];
                 currentString = List[j];
                 
                 currentBDN = ListData[j][0];
                 currentBUP = ListData[j][1];
               }
            }
            
            if(currentMaxIndex != i)
            {
               Frequency[currentMaxIndex] = Frequency[i];
               Frequency[i] = currentMax;
               List[currentMaxIndex] = List[i];
               List[i] = currentString;
               
               ListData[currentMaxIndex][0] = ListData[i][0];
               ListData[currentMaxIndex][1] = ListData[i][1];
               ListData[i][0] = currentBDN;
               ListData[i][1] = currentBUP;
               
            }
        }
     /* 
         Print("descending sorted Frequency list");
         for(i = 0; i < ArraySize(Frequency); i++)
        {
          Print("the string / Frequency / bup / bdn: "+List[i]+" and "+Frequency[i]+" / "+ListData[i][1]+" / "+ListData[i][0]);
        }
      */  
       /* Print("pattern  Frequency  Prob_up  Prob_dn");
        for(i = 0; i <ArraySize(Frequency); i++)
        {
          if(Frequency[i] != 0)
          {
          double bldu = ListData[i][1];
          double bldd = ListData[i][0];
          double  f = Frequency[i];
          double bup = (bldu/f);
          double bdn = (bldd/f);
          Print("Frequency: "+Frequency[i]+" ListData[i][1]: "+ListData[i][1]+" ListData[i][0]"+ListData[i][1]);
          Print(""+List[i]+"  "+Frequency[i]+"  "+bup+"  "+bdn);
          }
        }*/
       // int counted_bars = IndicatorCounted();
       // i=Bars;
       // Print("Bars: "+Bars+" IndicatorCounted(): "+counted_bars);
     // if(counted_bars>i) i=Bars-counted_bars-1;
        
        LogInfo();
 finished = true;
//----
   return(0);
 
jeemba2012:

Here is the rest of the code from the while loop on down. If I comment the while loop there is no freezing so I assume the problem lies within the while loop.

Have you ruled out the fact that it just simply seems to be performing a very large number of calculations?

Let's say that the value of Bars is something like 60,000. The inner loop "while (m > 0)" is going to get executed lots of times, because the statement "m = (i+1)*60+TimeMinute(iTime(Symbol(),PERIOD_M1,(i+1)*60))" is going to assign a value around 3,600,000 to m if Bars is 60,000. On the next i loop, when i is decremented to 59,999, then the m loop is going to get executed another few million times. Therefore, if there is a five-figure number of bars on the chart, then the contents of the "while (m > 0)" loop are going to be executed billions of times in total.
 
jjc:
Have you ruled out the fact that it just simply seems to be performing a very large number of calculations?

Let's say that the value of Bars is something like 60,000. The inner loop "while (m > 0)" is going to get executed lots of times, because the statement "m = (i+1)*60+TimeMinute(iTime(Symbol(),PERIOD_M1,(i+1)*60))" is going to assign a value around 3,600,000 to m if Bars is 60,000. On the next i loop, when i is decremented to 59,999, then the m loop is going to get executed another few million times. Therefore, if there is a five-figure number of bars on the chart, then the contents of the "while (m > 0)" loop are going to be executed billions of times in total.

Honestly I did not think that would be a problem because that big number is actually the index of the exact time for that particular hour. For instance if I am evaluating 10:00 on my server time, I want to see how far price moves by the minute so I find 10:00 for that particular date in the minute timeframe. what is max. It actually did work for i = 30000 but now it does not work for i = 9000.
 
jeemba2012:

Honestly I did not think that would be a problem because that big number is actually the index of the exact time for that particular hour. For instance if I am evaluating 10:00 on my server time, I want to see how far price moves by the minute so I find 10:00 for that particular date in the minute timeframe. what is max. It actually did work for i = 30000 but now it does not work for i = 9000.
You're right. I was overlooking the break statements.