def memoryToString(kmem, unit=None):
    """Returns an amount of memory in a human-friendly string."""
    k = 1024
    if unit == "K" or not unit and kmem < k:
        return "%dK" % kmem
    if unit == "M" or not unit and kmem < pow(k, 2):
        return "%dM" % (kmem // k)
    return "%.01fG" % (float(kmem) / pow(k, 2))