import java.lang.Math;
class heap {
  public static void main(String args[]) {
    double mallocfreq = 0.504;
    int small = 8, big = 4096;
    int initialallocs = 100000;
    double bigfreq = 0.010;
    Object[] allocs = new Object[1000];
    int operations = 0;
    int population = 0;
    int i;
    long lasttime = System.currentTimeMillis();
    long starttime = lasttime;
    long minInterval = Long.MAX_VALUE;
    long maxInterval = Long.MIN_VALUE;
    if (args.length == 2) {
      small = Integer.parseInt(args[0]);
      big = Integer.parseInt(args[1]);
    }
    while (true) {
      operations++;
      double r = Math.random();
      if (operations > initialallocs && r >= mallocfreq) { // free something
        if (population > 0) {
          r = Math.floor(population * (r - mallocfreq) / (1 - mallocfreq));
          population--;
          allocs[(int) r] = allocs[population];
          allocs[population] = null;
        }
      } else { // alloc something
        if (population >= allocs.length) {
          Object[] nallocs = new Object[population * 2 + 3];
          for (i = 0; i < population; i++)
            nallocs[i] = allocs[i];
          allocs = nallocs;
        }
        allocs[population++] =
          new byte[(r / mallocfreq >= bigfreq) ? small : big];
      }
      if (operations % 100000 == 0) {
        long thistime = System.currentTimeMillis();
        long thisInterval = thistime - lasttime;
        minInterval = Math.min(minInterval, thisInterval);
        maxInterval = Math.max(maxInterval, thisInterval);
        System.out.println("After " + operations + " ops, " + population +
          " live allocs, interval " + thisInterval + 
          "\tmin " + minInterval + " max " + maxInterval + " total " + (thistime - starttime));
        lasttime = thistime;
        // if (operations % 1000000 == 0) System.gc();
      }
    }
  }
}
