我正在了解有关内存泄漏的更多信息,并尝试使用以下代码:
我有下面的代码抛出 Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
import com.learn.general.memoryleaks.LeakFactory.Leak;
public class InnerClassMemoryLeakTest {
static Leak[] leaks = new Leak[100000];
public static void main(String[] args) {
while(true) {
for (int i = 0; i < leaks.length; i++) {
leaks[i] = new LeakFactory().getLeak();
}
}
}
private static void createLeak() {
while(true) {
for (int i = 0; i < leaks.length; i++) {
leaks[i] = new LeakFactory().getLeak();
}
}
}
}
import java.util.Calendar;
public class LeakFactory {
double d = Math.random();
Calendar calendar = Calendar.getInstance();
public Leak getLeak(){
return new Leak();
}
public class Leak{
double d_i = Math.random();
Calendar calendar_i = Calendar.getInstance();
}
}
然后我有下面的代码抛出 Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class ArrayListMemoryLeak {
public static void main(String[] args) {
List<Object> calendars = new ArrayList<>();
int count = 0;
while(true){
calendars.add(Calendar.getInstance());
}
}
}
问题:第一种情况的错误是否表示存在内存泄漏,而第二种情况则没有?
慕后森
HUX布斯
相关分类