title: What is the reason for this method to take so much memory?
body: <p>im trying to debug this method but it seems that nothing works...<br>
  I suspect that in the conditional statement pathName.add() is cousing trouble. After this method is executed it takes up 50 MB executed one more time it takes up 150mb till it reaches 800mb. But all is allocated space. Why does the gc does not clean this mess ??? </p>
  
  <p>P.s this method created directories based on given path that have been constructen with conditional statements </p>
  
  <p>P.s P.s The method writeDir(...) is called from within actionListener (When button on gui ic clicked). The button can be clicked offten</p>
  
  <p>P.s P.s P.s I have tried Andreas suggestion and it worked partially. After invoking pathName.clean() eden space droped but allocated space was still growing maximum is reached.</p>
  
  <p><a href="https://i.stack.imgur.com/iP0bs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iP0bs.png" alt="memory snapshot"></a></p>
  
  <p>Would be interested in your opinion :) Thanks </p>
  
  <blockquote>
    <p>Calling writeDir(...)</p>
  </blockquote>
  
  <pre><code>startButton.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
              try {
                          progress(0);
                          XMLSaxLogic stax = new XMLSaxLogic(xmlPath);
  
                          DetectionFilter detectionFilter = new DetectionFilter(stax.getObjets());
                          try {
                              WriteFile writeFile = new WriteFile(detectionFilter.getDetectionList());
                              writeFile.writeDir(savedDirPath, detectionFilter.getHardwareList(), stax.getSiteName());
                              progress(100);
                          } catch (Exception a) {
                              System.out.println(a.getLocalizedMessage());
                          }
                          GetFileCount getFileCount = new GetFileCount(savedDirPath, detectionFilter.getDetectionList(), combo.getSelectedIndex());
  
                          getFileCount.getFile(savedDirPath.getAbsoluteFile().toString());
                      } catch (Exception a) {
                          System.out.println(a.getLocalizedMessage());
                      }
  }
  }
  </code></pre>
  
  <blockquote>
    <p>writeDir(...)</p>
  </blockquote>
  
  <pre><code>   private ArrayList&lt;String&gt; detectList;
     private String detection = null;
     private String build = null;
     private Set&lt;String&gt; pathName = new LinkedHashSet&lt;&gt;();
     public WriteFile(ArrayList&lt;String&gt; detectList) {
         this.detectList = detectList;
     }
  public void writeDir(File root, ArrayList&lt;String&gt; sevenElementList, ArrayList&lt;String&gt; oneElementList) {
  
      for (String site : oneElementList) {
          for (String s : sevenElementList) {
              int indexx = s.indexOf("_");
              int id = Character.getNumericValue(s.charAt(indexHardware - 1));
  
              for (String detectionList : detectList) {
                  int index = detectionList.indexOf("_");
                  int sId = Character.getNumericValue(detectionList.charAt(index + 1));
  
                  if (detectionList.contains("Apple") &amp;&amp; sId == id) {
                      detection = site.trim() + "/" + s + "/" + detectionList.trim();
                      pathName.add(format(detection));
                  } else if (detectionList.contains("Banana") &amp;&amp; sId == id) {
                      build = detection.trim() + "/" + detectionList.trim();
                      pathName.add(format(build.trim()));
                  } else if (detectionList.contains("nananana") &amp;&amp; sId == id) {
                      pathName.add(format(build.trim() + "/" + detectionList.trim()));
                  } else if (detectionList.contains("Watermelone") &amp;&amp; sId == id) {
                      pathName.add(format(build.trim() + "/" + detectionList));
                  } else if (detectionList.contains("Orange") &amp;&amp; sId == id) {
                      pathName.add(format(site.trim() + "/" + s.trim() + "/" + detectionList.trim()));
                  }
              }
          }
      }
      createDirTest(pathName, root);
  }
  private void createDirTest(Set&lt;String&gt; pathArray, File root) {
      for (String s : pathArray) {
          File subdir = new File(root, s);
          subdir.mkdirs();
      }
  }
  
  private String format(String toBeFormated) {
      String toBeTrimmed = trimLastChar(toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim());
      return toBeTrimmed;
  }
  </code></pre>