Java - Converting bytes to megabytes
12,982
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.
So when you are calculating memoryUsedOnStartup
it's in bytes. If you want to get the result in Megabytes then you need to divide it by (1024 * 1024)
Author by
Logan
Updated on June 04, 2022Comments
-
Logan almost 2 years
I have the following code here:
long memoryUsedOnStartup = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
and I'm using it for some statistical stuff in my program, but this returns it in kilobytes. Is there any reliable way to convert this to megabytes?
EDIT: It returns it in bytes, but I still need it in megabytes...
-
Marged over 6 yearsThat is not the correct way. Check this with some real values. Besides that you should not uppercase the function name and KB