我知道垃圾收集在Java中是自动化的.但我明白,如果你System.gc()在代码中编写,Java VM可能会或可能不会在运行时决定在那时进行垃圾收集.这是如何工作的?基于什么基础/参数,VM在看到GC时决定做(或不做)GC System.gc()?是否有可能的例子在这种情况下将它放在代码中是个好主意?
如果不调用System.gc(),系统将抛出 OutOfMemoryException。我不知道为什么我需要System.gc()明确调用;JVM 应该调用gc()自己,对吗?请指教。
以下是我的测试代码:
public static void main(String[] args) throws InterruptedException {
WeakHashMap<String, int[]> hm = new WeakHashMap<>();
int i = 0;
while(true) {
Thread.sleep(1000);
i++;
String key = new String(new Integer(i).toString());
System.out.println(String.format("add new element %d", i));
hm.put(key, new int[1024 * 10000]);
key = null;
//System.gc();
}
}
Run Code Online (Sandbox Code Playgroud)
如下,添加-XX:+PrintGCDetails打印出GC信息;如您所见,实际上,JVM 尝试执行完整的 GC 运行,但失败了;我仍然不知道原因。很奇怪,如果我取消注释该System.gc();行,结果是肯定的:
add new element 1
add new element 2
add new element 3
add new element 4 …Run Code Online (Sandbox Code Playgroud) java garbage-collection weak-references out-of-memory java-8
首先,我想澄清我对 的理解,WeakReference因为以下问题取决于相同。
static void test() {
Person p = new Person();
WeakReference<Person> person = new WeakReference<>(p);
p = null;
System.gc();
System.out.println(person.get());
System.out.println(person);
}
static class Person {
String name;
}
static class PersonMetadata {
String someData;
public PersonMetadata(String met) {
someData = met;
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
null
java.lang.ref.WeakReference@7852e922
这意味着虽然有一个实际的 person 对象在 GC 运行时被垃圾收集,但是WeakReference<Person>内存中存在一个类的对象,此时它不指向任何东西。
现在考虑到上述理解是正确的,我对它的WeakHashMap<K,V>工作原理感到困惑。在下面的代码中
public static void main(String[] args) {
Person p = new Person();
p.name = "John";
WeakHashMap<Person, PersonMetadata> map = new WeakHashMap<>(); …Run Code Online (Sandbox Code Playgroud) java collections garbage-collection weak-references weakhashmap