Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
即使进一步引用了超出范围的引用,Java垃圾收集器也会进行收集吗? 请考虑以下代码: import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static List<String> arraylist=new ArrayList<String>(); //add hello n times to the list public static void add_to_list(int n) { if(n==0) { return; } else { String b=new String("hello"); arraylist.add(b); add_to_list(n-1); } } public static void main(String args[]) throws IOException { add_to_list(5); for(String s:arraylist) { System.out.println(s); } } }_Java - Fatal编程技术网

即使进一步引用了超出范围的引用,Java垃圾收集器也会进行收集吗? 请考虑以下代码: import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static List<String> arraylist=new ArrayList<String>(); //add hello n times to the list public static void add_to_list(int n) { if(n==0) { return; } else { String b=new String("hello"); arraylist.add(b); add_to_list(n-1); } } public static void main(String args[]) throws IOException { add_to_list(5); for(String s:arraylist) { System.out.println(s); } } }

即使进一步引用了超出范围的引用,Java垃圾收集器也会进行收集吗? 请考虑以下代码: import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static List<String> arraylist=new ArrayList<String>(); //add hello n times to the list public static void add_to_list(int n) { if(n==0) { return; } else { String b=new String("hello"); arraylist.add(b); add_to_list(n-1); } } public static void main(String args[]) throws IOException { add_to_list(5); for(String s:arraylist) { System.out.println(s); } } },java,Java,我的假设: 一旦执行了add_to_list方法,字符串b就超出范围 我引用的arraylist引用索引超出了其范围 Arraylist包含在方法中创建的字符串的引用。 因此,我的问题是: 在我打印值之前,java收集器是否有可能清除引用? 我是不是很幸运,在我读取这些值之前Java收集器没有运行? 变量arraylist是静态的,因此不会超出范围。因此,在程序结束之前,它将保持对其元素的引用 它们不能被垃圾收集,所以你不仅仅是幸运的。当你使用new操作符创建字符串时,它总是在堆内存中创建一个新

我的假设:

一旦执行了add_to_list方法,字符串b就超出范围 我引用的arraylist引用索引超出了其范围 Arraylist包含在方法中创建的字符串的引用。 因此,我的问题是:

在我打印值之前,java收集器是否有可能清除引用? 我是不是很幸运,在我读取这些值之前Java收集器没有运行? 变量arraylist是静态的,因此不会超出范围。因此,在程序结束之前,它将保持对其元素的引用


它们不能被垃圾收集,所以你不仅仅是幸运的。

当你使用new操作符创建字符串时,它总是在堆内存中创建一个新对象。在上面的代码中,每次调用add_to_list时,它都会在堆内存中创建一个新的字符串对象,其引用存储在本地变量“b”中,该变量位于add_to_list的堆栈内存中。每次调用此方法时,都会分配新的堆栈内存,并且在完成该特定调用的执行后,会清除其分配的堆栈内存

但您也可以将这个字符串对象引用,即“b”添加到静态ArrayList中。静态变量作为与该类关联的类对象的一部分存储,并驻留在堆内存中的PermGen中,只要该类在内存中,静态变量就会一直存在


垃圾收集总是通过清除没有任何引用的任何对象来释放内存。这些是不再使用的对象。但在上面的代码中,所有字符串引用都存储在静态列表中,因此只有在类终止时才会对它们进行垃圾收集。

垃圾收集器将只收集对它们没有强引用的对象。@Slaw您能解释一下什么是强引用吗?变量b超出范围,但是该对象仍然存在,并且不符合GC的条件,因为该列表包含对它的引用。如果从列表中删除该项,则该项将符合GC条件。@SouravKanta Correct。垃圾收集器是专门设计的,因此您永远无法看到它在工作。@SouravKanta区别在于,在Java中,除了原语和引用之外,堆栈上实际上不存在任何东西。所有对象都在堆栈外分配。为了补偿堆积在堆上的所有内容,有一个内存池用于存储新创建的对象—young gen—从中积极地剔除对象。
hello
hello
hello
hello
hello