code: void updateGCcounters(){
      long gccount = 0;
      long gctime = 0;
          
      for(GarbageCollectorMXBean gc :
        ManagementFactory.getGarbageCollectorMXBeans()) {
      
        long count = gc.getCollectionCount();
        if(count >= 0) {
          gccount += count;
        }
        
        long time = gc.getCollectionTime();
        if(time >= 0) {
          gctime += time;
        }
      }
      
      Iterator beans = ManagementFactory.getMemoryPoolMXBeans().iterator();
      long aftergc = 0;
      long maxaftergc = 0;
      
      while (beans.hasNext()){
        MemoryPoolMXBean bean = (MemoryPoolMXBean) beans.next();
        String beanname = bean.getName();
        
        if(!beanname.toUpperCase().contains("OLD GEN")) continue;
        
        MemoryUsage mu = bean.getCollectionUsage();
        
        if(mu == null) continue;
        
        aftergc = mu.getUsed();
        
        if(aftergc > maxaftergc) {
          maxaftergc = aftergc;
        }
        
      }
      
      counters.findCounter(GC_COUNTER_GROUP,"Total number of GC")
        .setValue(gccount);
      counters.findCounter(GC_COUNTER_GROUP,"Total time of GC in milliseconds")
        .setValue(gctime);
      counters.findCounter(GC_COUNTER_GROUP,"Heap size after last GC in bytes")
        .setValue(maxaftergc);
      
      long currentMax =
        counters.findCounter(GC_COUNTER_GROUP,"Max heap size after GC in bytes")
          .getValue();
      
      if(maxaftergc>currentMax){
        counters.findCounter(GC_COUNTER_GROUP,"Max heap size after GC in bytes")
          .setValue(maxaftergc);
      }
      
    }
docs: Update counters about Garbage Collection