Java Code Examples for org.apache.brooklyn.util.text.Strings#makeSizeString()

The following examples show how to use org.apache.brooklyn.util.text.Strings#makeSizeString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryUsageTracker.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** as {@link #forceClearSoftReferences()} but gives control over headroom and max chunk size.
 * it tries to undershoot by headroom as it approaches maximum (and then overshoot)
 * to minimize the chance we take exactly all the memory and starve another thread;
 * and it uses the given max chunk size in cases where the caller wants more precision
 * (the available memory will be fragmented so the smaller the chunks the more it can
 * fill in, but at the expense of time and actual memory provisioned) */
public static String forceClearSoftReferences(long headroom, int maxChunk) {
    final long HEADROOM = 1000*1000;  
    long lastAmount = 0;
    long nextAmount = 0;
    long oldUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    try {
        List<byte[]> dd = MutableList.of();
        while (true) {
            int size = (int)Math.min(Runtime.getRuntime().freeMemory()-HEADROOM, maxChunk);
            if (size<HEADROOM) {
                // do this to minimize the chance another thread gets an OOME
                // due to us leaving just a tiny amount of memory 
                size = (int) Math.min(size + 2*HEADROOM, maxChunk);
            }
            nextAmount += size;
            dd.add(new byte[size]);
            lastAmount = nextAmount;
        }
    } catch (OutOfMemoryError e) { /* expected */ }
    System.gc(); System.gc();
    long newUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    return "allocated " + Strings.makeSizeString((lastAmount+nextAmount)/2) +
            (lastAmount<nextAmount ? " +- "+Strings.makeSizeString((nextAmount-lastAmount)/2) : "")
            +" really free memory in "+Strings.makeSizeString(maxChunk)+" chunks; "
            +"memory used from "+Strings.makeSizeString(oldUsed)+" -> "+Strings.makeSizeString(newUsed)+" / "+
                Strings.makeSizeString(Runtime.getRuntime().totalMemory());
}
 
Example 2
Source File: BrooklynGarbageCollector.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static String makeBasicUsageString() {
    int present = (int)Math.round(100.0*SoftlyPresent.getUsageTracker().getPercentagePresent());
    return Strings.makeSizeString(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())+" / "+
        Strings.makeSizeString(Runtime.getRuntime().maxMemory()) 
        + (Runtime.getRuntime().maxMemory() > Runtime.getRuntime().totalMemory() ? 
            " ("+ Strings.makeSizeString(Runtime.getRuntime().totalMemory()) +" real)"
            : "")
        + " memory"
        + "; " +
        (present>=0 ? present+"% soft-reference maybe retention (of "+SoftlyPresent.getUsageTracker().getTotalEntries()+"); " : "") +
        Thread.activeCount()+" threads";
}
 
Example 3
Source File: BrooklynTaskTags.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return "Stream["+streamType+"/"+Strings.makeSizeString(streamSize.get())+"]";
}
 
Example 4
Source File: ListeningObjectStore.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public String getTotalString() {
    return "totals: out="+Strings.makeSizeString(bytesOut.get())+" in="+Strings.makeSizeString(bytesIn.get());
}