2013-09-16 40 views
0

我有這樣的代碼,其顯示所分配的JVM在存儲器(我想)字節:顯示JVM /千兆字節

Runtime.getRuntime().totalMemory() 

當我打印輸出I得到類似12232313131。你能告訴我如何以兆字節和千兆字節來打印嗎?

+3

也許使用數學? :) – araknoid

+0

這篇文章可能會幫助你:http://stackoverflow.com/questions/3571203/what-is-the-exact-meaning-of-runtime-getruntime-totalmemory-and-freememory – Archer

回答

1

一個很好的竅門是使用printf(...);

System.out.printf("%.3fGiB", Runtime.getRuntime().totalMemory()/(1024.0 * 1024.0 * 1024.0)); 
+0

這些數字是GiB不GB,谷歌它 – Vorsprung

+0

我認爲這比OP期待的更迂腐......我期待OP在尋找GiB,但仍然......會編輯帖子。 – rolfl

+0

對不起,不想成爲吊墜,我知道有些程序做GB = 1024 ** 3,但官方標準現在GB是1000 ** 3,不妨正確:) – Vorsprung