I want to collect a bunch of using the program. In particular, from a specific trigger event to a final event and with a specified interval, for example, every 10 ms. I tried to implement something similar to myself, this is the following code. It works at slow intervals, for example, 1 second, but for lower intervals, for example 100 ms, it becomes buggy. Does the System.gc () method use to block all threads until it finishes? I am currently accepting this.
public void startAsync() { running = true; Thread thread = new Thread(new Runnable() { @Override public void run() { while (running) { measure(); try { Thread.sleep(wait); } catch (InterruptedException e) { throw new RuntimeException(); } } } }); thread.start(); } public void measure() { MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); memoryBean.gc(); long currentTime = System.currentTimeMillis(); MemoryUsage memoryUsage = memoryBean.getHeapMemoryUsage(); writer.exec(currentTime - startTime, memoryUsage.getUsed()); }
source share