title: saving state for a Java class that uses a recursive method
body: <p>I have a Jave class that calculates all possible combination for a given array of elements and to do this it uses a recursive method.
  It work fine but when the number of input elements raises I found out of memory problem.
  What I'd like to do is calculate combinations in chuncks of a given size.
  My problem is that I don't know how save and than restore the state for the recursive method,
  specially when it the calling depth is high.
  Beolw is the code.
  Thanks a lot.</p>
  
  <pre><code>package uty;
  
  import java.io.FileOutputStream;
  import java.util.ArrayList;
  
  public class ESCalcCombination {
  
      int iMax = 0;
      boolean bEnd = false;
      int iLenInp;
      ArrayList&lt;Integer[]&gt; resultList;
  
      public ESCalcCombination(int[] inElements, int inMaxElem, int inMaxElemLen) {
          if (inMaxElem &gt; 0) {
              iMax = inMaxElem;
          } else {
              iMax = new Double(Math.pow(2d, new Integer(inElements.length).doubleValue())).intValue();
          }
          resultList = new ArrayList(iMax);
          iLenInp = inElements.length;
          for (int i = 1; i &lt;= iLenInp; i++) {
              if (inMaxElemLen &gt; 0) {
                  if (i &gt; inMaxElemLen) {
                      break;
                  }
              }
              for (int j = 0; j &lt; iLenInp; j++) {
                  if ((iLenInp - j) &lt; i) {
                      break;
                  }
                  addNextElement(inElements, j, i, null);
                  if (bEnd) {
                      break;
                  }
              }
              if (bEnd) {
                  break;
              }
          }
      }
  
      private void addNextElement(int[] inElements, int inCurIndex, int inLimitLen, ArrayList&lt;Integer&gt; inCurrentCombination) {
          if (inCurrentCombination != null
                  &amp;&amp; (inCurrentCombination.size() + (iLenInp - inCurIndex)) &lt; inLimitLen) {
              return;
          }
          ArrayList&lt;Integer&gt; alCombinationLoc = new ArrayList();
          if (inCurrentCombination != null) {
              alCombinationLoc.addAll(inCurrentCombination);
          }
          alCombinationLoc.add(inElements[inCurIndex]);
          if (alCombinationLoc.size() == inLimitLen) {
              Integer[] arComb = new Integer[alCombinationLoc.size()];
              arComb = alCombinationLoc.toArray(arComb);
              resultList.add(arComb);
              alCombinationLoc.clear();
              alCombinationLoc = null;
              if (resultList.size() == iMax) {
                  bEnd = true;
              }
              return;
          }
          for (int i = ++inCurIndex; i &lt; iLenInp; i++) {
              addNextElement(inElements, i, inLimitLen, alCombinationLoc);
              if (bEnd) {
                  return;
              }
          }
      }
  
      public void close() {
          ESUty.closeAL(resultList);
      }
  
      public ArrayList&lt;Integer[]&gt; getCombinations() {
          return resultList;
      }
  
      public static void main(String[] args) {
          ESCalcCombination ESCaCo = new ESCalcCombination(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 0, 15);
          FileOutputStream fos = null;
          try {
              fos = new FileOutputStream("c:\\test\\conbinations.txt");
              for (int i = 0; i &lt; ESCaCo.getCombinations().size(); i++) {
                  StringBuilder sb = new StringBuilder();
                  for (int j = 0; j &lt; ESCaCo.getCombinations().get(i).length; j++) {
                      sb.append(ESCaCo.getCombinations().get(i)[j]);
                  }
                  System.out.println("elemento " + i + " = " + sb.toString());
                  fos.write((sb.toString() + System.getProperty("line.separator")).getBytes());
  
              }
          } catch (Exception ex) {
              System.out.println("errore " + ex);
          } finally {
              ESUty.closeFileOutputStream(fos);
  
          }
  
          System.exit(0);
      }
  }
  </code></pre>