Peter Penzov
Peter Penzov

Reputation: 1606

Convert byte in user friendly format

I found this code from this example:

private static final String[] Q = new String[]{"", "K", "M", "G", "T", "P", "E"};

public String getAsString(long bytes)
{
    for (int i = 6; i > 0; i--)
    {
        double step = Math.pow(1024, i);
        if (bytes > step) return String.format("%3.1f %s", bytes / step, Q[i]);
    }
    return Long.toString(bytes);
}

I implement this code but the result looks suspicious. I tried to get from JVM how much memory is available and I get only 80 MB. Could you please confirm that this code correctly converts bytes into Megabytes or Gigabytes.

Upvotes: 0

Views: 202

Answers (1)

Archer
Archer

Reputation: 5147

Your code looks correct. Try changing -Xmx, -Xms JVM value when running your app and see the difference.

For example, try running this:

JAVA_OPTS="-Xmx2G -Xms2G" java your.Class

Upvotes: 1

Related Questions