title: Java - why doesn't jstack work in a tight loop
body: <p>I've noticed that jstack doesn't work when code is in a tight loop.</p>
  
  <p>For example in this code:</p>
  
  <pre><code>private static void testAllocationNotOnHeap(){
      long time = System.currentTimeMillis();
      long result =0;
  
      for (int j = 0; j &lt; 20_000; j++) {
          for (int i = 0; i &lt; 100_000_000; i++) {
              result += new TestObject(5,5).getResult();
          }
      }
      System.out.println("Allocation not on heap took "
              + (System.currentTimeMillis()-time)
              + " with result " + result);
  }
  </code></pre>
  
  <p>If you run jstack you get this message:</p>
  
  <blockquote>
    <p>Unable to open socket file: target process not responding or HotSpot VM not loaded
    The -F option can be used when the target process is not responding</p>
  </blockquote>
  
  <p>As soon as you allow the code to 'breathe' e.g. by add a System.out every few thousand iterations it all works fine.</p>
  
  <p>Note this is the same if you want to run jconsole or flight recorder from jmc.</p>
  
  <p>I'd like to understand why this is and if there's anything I can do about it as I'd like to profile a tight loop.</p>