title: Metaspace of Java VM runs full
body: <p>I am trying to get my Java code to run, but whenever I try the metaspace starts to get full. At first I didn't even notice it since it takes a long time (~1 month) to run full and crash.</p>
  <p>The first thing I did was to look into the code, if there were Objects being created and filled with a lot of data and not deleted by the GC. After determining that this was (most likely) not the case I wrote       a little test programm which results in the same error.</p>
  <p>In my main I call a new TimerTask and assign a Timer to it. Afterwards I just schedule it.</p>
  <pre><code>TestTimer testTimerTask = new TestTimer();
  Timer timer = new Timer(&quot;test&quot;);
  timer.scheduleAtFixedRate(testTimerTask, 0, 100);
  </code></pre>
  <p>And here the TimerTask itself which just creates a bunch of Objects that should be deleted      afterwards.</p>
  <pre><code>public class TestTimer extends TimerTask{
      private long x = 0;
      @Override
      public void run() {
          String str = &quot;&quot;;
          for (int i = 0; i &lt; 10000; i++) {
              if(x &gt;= 2147483647) {
                  x = 0;
              }
              str = new String(&quot;&quot; + x);
              x++;
          }
      }
  }
  
  </code></pre>
  <p>So I tried <em>debugging</em> it. For this I used the 2.0.4 Version of <a href="https://visualvm.github.io/" rel="nofollow noreferrer">VisualVM</a>.
  After letting it run for some time I got these two pictures of the <a href="https://i.stack.imgur.com/lpAkj.png" rel="nofollow noreferrer">Metaspace</a> and the <a href="https://i.stack.imgur.com/f0mik.png" rel="nofollow noreferrer">Heap</a>.</p>
  <p>In addition here's my JVM arguments,</p>
  <pre><code>-Xms64m
  -Xmx128m
  -Dlog4j.configurationFile=./log4j2.xml
  </code></pre>
  <p>my JVM Version <strong>OpenJDK 64-Bit Server VM (11.0.8+10, mixed mode)</strong> and my Java Version <strong>version 11.0.8, vendor Oracle Corporation</strong>.</p>
  <p>Is there anything I can do to troubleshoot this problem some more and to stop my metaspace from filling up?</p>