我这里有以下代码:
long memoryUsedOnStartup = Runtime.getRuntime().totalMemory() -
Runtime.getRuntime().freeMemory();
我在程序中使用它来进行一些统计工作,但这会以千字节为单位返回它。有没有可靠的方法可以将其转换为兆字节?
编辑:它以字节为单位返回它,但我仍然需要它以兆字节为单位......
最佳答案
1 Kilobyte (KB) -> 2^10 = 1,024 Bytes
1 Megabyte (MB) -> 2^20 = 1,048,576 Bytes = 1,024 Kilobytes (KB)
Return: an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.
Returns: the total amount of memory currently available for current and future objects, measured in bytes.
因此,当您计算memoryUsedOnStartup
时,它以字节为单位。如果您想获得以兆字节为单位的结果,则需要将其除以(1024 * 1024)
关于Java - 将字节转换为兆字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48274239/