package testhelper;

import com.sun.management.ThreadMXBean;

import java.lang.management.ManagementFactory;

public class PerformanceUtils {

  private static ThreadMXBean THREAD_MX_BEAN;

  static {
    Object bean = ManagementFactory.getThreadMXBean();
    if (bean instanceof ThreadMXBean)
      THREAD_MX_BEAN = (ThreadMXBean) bean;
  }

  /**
   * @return amount of memory (in bytes) allocated by current thread until now
   */
  public static long measureAllocatedMemory() {
    if (THREAD_MX_BEAN == null) {
      throw new IllegalStateException("Couldn't get thread MBean");
    }
    long selfId = Thread.currentThread().getId();

    return THREAD_MX_BEAN.getThreadAllocatedBytes(selfId);
  }
}

@@ -153,16 +153,22 @@
  static 
+final 
 ThreadMX
@@ -190,19 +190,76 @@
 BEAN
-;%0A%0A  static
+ = initThreadBean();%0A%0A  private static ThreadMXBean initThreadBean()
  %7B%0A 
@@ -316,20 +316,23 @@
 ();%0A    
-if (
+return 
 bean ins
@@ -355,32 +355,10 @@
 Bean
-)%0A      THREAD_MX_BEAN =
+ ?
  (Th
@@ -373,16 +373,23 @@
 an) bean
+ : null
 ;%0A  %7D%0A%0A 

Make thread bean field final