Advertisement
Guest User

Untitled

a guest
Nov 26th, 2024
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.lang.management.GarbageCollectorMXBean;
  2. import java.lang.management.ManagementFactory;
  3.  
  4. public class Main {
  5.     public static class Test {
  6.         long t;
  7.         long[] tA = new long[1024*1024];
  8.         public Test(long t) {
  9.             this.t = t;
  10.             this.tA[(int)t] = t;
  11.         }
  12.         public long getTa() {
  13.             return this.tA[(int)this.t];
  14.         }
  15.     }
  16.     public static class Res {
  17.         long times, ms;
  18.         public Res(long times, long ms) {
  19.             this.times = times;
  20.             this.ms = ms;
  21.         }
  22.     }
  23.     public static Res getGCStats() {
  24.         long totalGarbageCollections = 0;
  25.         long garbageCollectionTime = 0;
  26.  
  27.         for(GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
  28.             long count = gc.getCollectionCount();
  29.             if(count >= 0) {
  30.                 totalGarbageCollections += count;
  31.             }
  32.             long time = gc.getCollectionTime();
  33.             if(time >= 0) {
  34.                 garbageCollectionTime += time;
  35.             }
  36.         }
  37.  
  38.         return new Res(totalGarbageCollections, garbageCollectionTime);
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.  
  43.         Test t = new Test(1);
  44.         System.out.println("Start");
  45.         while (t.t < 10000L) {
  46.             t = new Test(t.getTa()+1);
  47.         }
  48.         var res = getGCStats();
  49.         System.out.println("Total collections: "+res.times);
  50.         System.out.println("Total time in ms:  "+res.ms);
  51.         System.out.println("MS per run:        "+((double)res.ms/(double)res.times));
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement