为什么 ArrayList.clear() 方法不释放内存? [英] Why ArrayList.clear() method doesn't free memory?

查看:106
本文介绍了为什么 ArrayList.clear() 方法不释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 ArrayList.clear() &发现它没有释放内存而是使该列表为空.为什么要等GC来&当您可以手动告诉编译器不再需要此特定 ArrayList 时,释放该内存(它可以在 clear 方法本身中完成).!!

I was looking into ArrayList.clear() & found that it doesn't free the memory instead made that list null. Why do you want to wait for GC to come & to free that memory (It could have done in clear method itself) when you can tell compiler manually that this particular ArrayList is no longer required.!!

你为什么要不必要地给 GC 带来额外的麻烦.

Why do you want to give extra headache to GC Unnecessarily.

我知道 System.gc() 会影响应用程序的性能,因为它会为整个应用程序运行垃圾收集.

I know that System.gc() impacts the application performance since it runs garbage collection for the whole application.

有没有办法收集这个特定的 ArrayList 垃圾,而不是等待 GC 来救援,以防我的服务器内存有限&列表尺寸太大.

Is there any way out to collect this particular ArrayList garbage instead of waiting GC to come to rescue in case of my server is having limited memory & list sizes are too too big.

-出现这个问题是因为我已经初始化了一个 ArrayList(500000) &让我们假设由于一些预先初始化的变量,我的对象大小约为 1000 字节,但由于形成第一个对象本身的一些异常,我需要从该函数返回并释放我分配给该 ArrayList 的内存.我希望我的应用程序消耗尽可能低的内存.GC 是我最后的选择.

- This problem arises because I have initialized a ArrayList(500000) & lets assume my object size is around 1000bytes due to some pre-initalized variables but due to some exception in forming first object itself I need to return from that function with freeing that memory which I have allocated to that ArrayList.I want my application to consume as low as memory it can. GC is the last resort for me.

推荐答案

为什么要等GC来&释放那块内存(它可以用 clear 方法本身完成)

Why do you want to wait for GC to come & to free that memory (It could have done in clear method itself)

不,它不能.ArrayList 确实不会再引用对象了,但是 clear() 方法不知道它们是否还在其他地方被引用,所以这就是为什么垃圾收集员的工作.

No, it could not. The ArrayList indeed won't be referencing the objects anymore, but the clear() method doesn't know if they are still being referenced somewhere else, so that's why this is a job for the Garbage Collector.

这篇关于为什么 ArrayList.clear() 方法不释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆