How do you use Runtime in Java?

2017-12-14 5:15 pm
I'm trying to use Runtime.totalMemory() and Runtime.freeMemory() to see how much memory my program is using, but how would you use it contextually? I get that it's an instance method so I should use Runtime.getRuntime() but how do I use it after that? I've been on a long hiatus from coding so I'm a little out of practice.

回答 (1)

2017-12-14 9:50 pm
✔ 最佳答案
There just one Runtime object for a running Java application, and you can get a reference to that object with Runtime.getRuntime().

You can use this each time you want to refer to that object, with something like:
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();

Another option is to call getRuntime() once and save the reference:
Runtime rt = Runtime.getRuntime();

...and then later use that reference:
long total = rt.totalMemory();
long free = rt.freeMemory();


收錄日期: 2021-05-01 22:14:33
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20171214091517AACm1gH

檢視 Wayback Machine 備份