Compare commits

...

4 Commits

Author SHA1 Message Date
f80521d4a5 Runtime 2024-11-10 22:12:04 +08:00
a6c121d9a1 垃圾回收 2024-11-10 22:11:57 +08:00
886c549976 增加注释 2024-11-10 21:39:19 +08:00
730e6a8592 System.getProperties 2024-11-10 21:32:32 +08:00
4 changed files with 68 additions and 0 deletions

View File

@ -1,5 +1,9 @@
package chapter5;
/**
* 获取当前时间
*/
public class Example11 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();

View File

@ -0,0 +1,21 @@
package chapter5;
import java.util.Enumeration;
import java.util.Properties;
/**
* 获取系统信息
*/
public class Example12 {
public static void main(String[] args) {
// 获取当前系统信息
Properties prop = System.getProperties();
Enumeration names = prop.propertyNames();
while (names.hasMoreElements()) {
String key = (String) names.nextElement();
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
}

View File

@ -0,0 +1,28 @@
package chapter5;
class Person implements AutoCloseable {
String name;
public Person(String name) {
this.name = name;
}
public void close() {
System.out.println( name + " 要被回收啦!");
}
}
public class Example13 {
public static void main(String[] args) {
Person p1 = new Person("小明");
Person p2 = new Person("大王");
p1 = null;
p2 = null;
System.gc();
for (int i = 0; i < 1000000; i ++) {
// 为了延长程序运行的时间
}
}
}

View File

@ -0,0 +1,15 @@
package chapter5;
/**
* 获取当前虚拟机信息
*/
public class Example14 {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
System.out.println("处理器个数:" + rt.availableProcessors());
System.out.println("空闲内存:" + rt.freeMemory()/1024/1024 + "MB");
System.out.println("虚拟机中的内存总量:" + rt.totalMemory()/1024/1024 + "MB");
System.out.println("最大可用内存:" + rt.maxMemory()/1024/1024 + "MB");
}
}