title: (Android) OutOfMemoryError thrown while trying to throw OutOfMemoryError
body: <p>So I'm creating an Android app and this code is what throws <em>"Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available"</em> error:</p>
  
  <pre><code>public ArrayList&lt;Double&gt; statModa(ArrayList&lt;Double&gt; statRed, ArrayList&lt;Double&gt; uniqueRed) {
      ArrayList&lt;Double&gt; result = new ArrayList&lt;&gt;();
      int maxFreqIndex = 0;
      int maxFreq = Collections.frequency(statRed, uniqueRed.get(0));
      for (int i=1; i &lt; statRed.size(); i++){
          int currentFreq = Collections.frequency(statRed, statRed.get(i));
          if ((currentFreq &gt;  maxFreq) || (currentFreq == maxFreq &amp;&amp; statRed.get(i) == statRed.get(maxFreqIndex))) {
              maxFreqIndex = i;
              maxFreq = currentFreq;
          }
      }
  
      if (maxFreq == statRed.size() / uniqueRed.size())
          return result;
  
      if (maxFreqIndex == statRed.size()-1){
          result.add(statRed.get(maxFreqIndex));
          return result;
      }
  
      for (int i=maxFreqIndex+1; i &lt; statRed.size(); i++){
          int currentFreq = Collections.frequency(statRed, statRed.get(i));
          if (currentFreq != maxFreq) {
              result.add(statRed.get(maxFreqIndex));
              int tempIndex = maxFreqIndexFinder(statRed, i, maxFreq);
              if (tempIndex == maxFreqIndex) {
                  break;
              } else {
                  i = tempIndex;
                  maxFreqIndex = tempIndex;
              }
          } else {
              double temp = statRed.get(maxFreqIndex);
              int tempTimes = 1;
              for (int j=1; i + currentFreq*j - 1 &lt; statRed.size(); j++){
                  if (currentFreq == Collections.frequency(statRed, statRed.get(i + currentFreq*j - 1))) {
                      temp += statRed.get(i + currentFreq * j - 1);
                      tempTimes++;
                      maxFreqIndex = i + currentFreq * j - 1;
                  } else {
                      break;
                  }
              }
              result.add(temp/(double)tempTimes);
              i = maxFreqIndex;
          }
      }
  
      return result;
  }
  
  public int maxFreqIndexFinder(ArrayList&lt;Double&gt; statRed, int startingIndex, int maxFreq){
      int resultIndex = startingIndex - 1;
      int count = 0;
      double lastFreqValue = 0;
      for (int i = startingIndex; i &lt; statRed.size(); i++){
          int currentFreq = Collections.frequency(statRed, statRed.get(i));
          if (currentFreq == maxFreq) {
              count++;
              if (count == 1) {
                  resultIndex = i;
                  lastFreqValue = statRed.get(i);
              } else {
                  if (lastFreqValue == statRed.get(i)) {
                      resultIndex = i;
                      lastFreqValue = statRed.get(i);
                  } else {
                      break;
                  }
              }
          }
      }
      return resultIndex;
  }
  </code></pre>
  
  <p>I'm not quite sure what exactly causes the error. I suspect a infinite loop, but I couldn't find it, so I'm asking here. Also before throwing the error it continuously allocates memory.</p>
  
  <p>Here's the console log (if needed): <a href="http://pastebin.com/rux3r0dg" rel="noreferrer">PasteBin</a></p>
  
  <p><strong>Additional info:</strong></p>
  
  <p>The function <strong>statModa</strong> is ran when a button is clicked. </p>
  
  <p>Red means line.</p>
  
  <p>The values I ran the app with and got an error are as follows:</p>
  
  <p><em>ArrayList</em> <strong>statRed</strong> has the values {0.1, 1, 1, 2, 2, 3, 3, 4, 5 5 5} and</p>
  
  <p><em>ArrayList</em> <strong>uniqueRed</strong> has the values {0.1, 1, 2, 3, 4, 5}. </p>
  
  <p>Also these are <strong>not</strong> the only values that give me this error. If this <strong>(maxFreq == statRed.size() / uniqueRed.size())</strong> is true then there is no error, if not then I get the error.</p>
  
  <p><strong>P.S.</strong> I saw another question with the same error, there was only one Answer which was to enable largeHeap, but it didn't work for me.</p>