FileDocCategorySizeDatePackage
MemoryMonitor.javaAPI DocExample6220Sun Jan 16 19:32:56 GMT 2000tuning.profile

MemorySampler

public class MemorySampler extends Object implements Runnable

Fields Summary
long[]
freeMemory
long[]
totalMemory
int
sampleSize
long
max
boolean
keepGoing
Constructors Summary
MemorySampler()


  
  
    //Start the object running in a separate maximum priority thread
    Thread t = new Thread(this);
    t.setDaemon(true);
    t.setPriority(Thread.MAX_PRIORITY);
    t.start();
  
Methods Summary
public voidaddSample(java.lang.Runtime runtime)

    //Takes the actual samples, recording them in the two arrays.
    //We expand the arrays when they get full up.
    if (sampleSize >= freeMemory.length)
    {
      //just expand the arrays if they are now too small
      long[] tmp = new long[2 * freeMemory.length];
      System.arraycopy(freeMemory, 0, tmp, 0, freeMemory.length);
      freeMemory = tmp;
      tmp = new long[2 * totalMemory.length];
      System.arraycopy(totalMemory, 0, tmp, 0, totalMemory.length);
      totalMemory = tmp;
    }

    freeMemory[sampleSize] = runtime.freeMemory();
    totalMemory[sampleSize] = runtime.totalMemory();

    //Keep the maximum value of the total memory for convenience.
    if (max < totalMemory[sampleSize])
      max = totalMemory[sampleSize];
    sampleSize++;
  
public voidrun()

    //Just a loop that continues sampling memory values every 30 milliseconds
    //until the stop() method is called.
    Runtime runtime = Runtime.getRuntime();
    while(keepGoing)
    {
      try{Thread.sleep(30);}catch(InterruptedException e){};
      addSample(runtime);
    }
  
public voidstop()

    //set to stop the thread when someone tells us
    keepGoing = false;