Java变量->代替?内存优化

发布于 2024-08-31 17:58:34 字数 459 浏览 3 评论 0原文

我只是想知道当我声明并初始化一个变量,然后用其他值(例如 ArrayList 或类似的东西)再次初始化它时,程序背后会发生什么。

当我这样说时,我的 RAM 中会发生什么:

ArrayList<String> al = new ArrayList<String>();
...add values, work with it and so on....

al = new ArrayList<String>();

那么我的第一个 ArrayList 是否保存在 RAM 中,或者第二个 ArrayList 是否存储在第一个 ArrayList 之前所在的位置?或者它只会改变“al”的引用?

如果不替换...有没有办法手动释放第一个 arraylist 占用的 RAM? (无需等待垃圾收集器) 先设置它=null会有帮助吗?

很好的问候, 波斯克伦

I just wanted to know what happens behind my program when I declare and initialize a variable and later initialize it again with other values, e.g. an ArrayList or something similar.

What happens in my RAM, when I say e.g. this:

ArrayList<String> al = new ArrayList<String>();
...add values, work with it and so on....

al = new ArrayList<String>();

So is my first ArrayList held in RAM or will the second ArrayList be stored on the same position where the first one has been before? Or will it just change the reference of "al"?

If it is not replaced...is there a way to manually free the RAM which was occupied by the first arraylist? (without waiting for the garbage collector)
Would it help to set it first =null?

Nice greetings,
poeschlorn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明媚如初 2024-09-07 17:58:34

您发布的代码将分配一个新的 ArrayList 实例。如果您想重用同一个实例,可以这样做:

ArrayList<String> al = new ArrayList<String>();
...add values, work with it and so on....

al.clear();
// now you can use a1

但是请谨慎执行此操作 - 如果将 a1 的初始实例传递给将使用它较长时间的其他代码,则清除它会导致问题,您将需要一个单独的实例。

但还要注意,从回收对象数组和 ArrayList 中获得的节省并不是很大。如果您在 ArrayList 中存储 10 x 4096 字节字符串,则数组列表本身仅占用与引用大小成比例的空间,例如大约 4 字节 x 10 = 40 字节。这是一种简化,但原理是正确的。因此,即使您重用相同的数组列表,您也只是节省了用于存储对象引用的内存,而不是对象本身。考虑到这一点,以及无意中修改集合而导致错误的风险,我猜大多数人都不会打扰回收列表。

现代虚拟机中的内存管理确实非常好,只有当您发现有需要时才应该开始引入内存“优化”。事实上,使用对象的时间超过其自然寿命可能会对垃圾收集性能产生负面影响。

我的建议是,首先清楚地编码、分析,并且仅在发现问题并确定原因时才专注于优化内存使用。

祝你好运!

The code you post will allocate a new ArrayList instance. If you want to reuse the same one, you can do this:

ArrayList<String> al = new ArrayList<String>();
...add values, work with it and so on....

al.clear();
// now you can use a1

But do this with caution - if you pass the initial instance of a1 to other code that will be using it for a longer period, then clearing it will cause problems and you will need a separate instance.

But also note that the savings you get from recycling object arrays and ArrayLists aren't that great. If you were storing 10 x 4096 byte strings in the ArrayList, the array list itself only occupies space proportional to the size of the references, e.g. circa 4 bytes x 10 = 40 bytes. This is a simplification, but the principle is correct. So, even if you reuse the same array list, you are only saving yourself the memory used to store the object references, not the objects themselves. With that in mind, and the risks of causing bugs by modifying a collection unintentionally, I would guess most people don't bother recycling lists.

The memory management in a modern VM is really very good, and you should only start introducing memory "optimizations" when you see that there is a need for it. In fact, using objects for longer than their natural lifetime can have a negative effect on garbage collection performance.

My advice is, code it clearly first, profile, and only focus on optimizing memory use when you see there is a problem and have identified the cause.

Good luck!

梦在深巷 2024-09-07 17:58:34

新的 ArrayList 将从内存的不同部分分配,引用更改为指向它,如果旧的 ArrayList 不再被其他东西引用,它将被垃圾收集。 Java 中没有办法手动释放内存。它会自动发生。

将变量设置为 null 是无关紧要的,当它紧随其后设置为其他内容时,或者它是一个无论如何都会很快超出范围的局部变量时(但在像 ArrayList 这样的数据结构中,当元素为 null 时,将所包含数组的元素设置为 null已删除,以避免内存泄漏)。

The new ArrayList will be allocated from some different part of the memory, the reference is changed to point to it, and if the old ArrayList is not anymore referred by something else, it will be garbage collected. There is no way to manually free memory in Java. It happens automatically.

Settings variables to null is irrelevant, when it is set to something else right after that or it's a local variable which anyways gets out of scope soon (but inside data structures like ArrayList, setting elements of the contained array to null, when the element is removed, is required to avoid memory leaks).

夜夜流光相皎洁 2024-09-07 17:58:34

堆中的新空间将为新的 ArrayList 分配。

您不能强制进行垃圾回收,分配 null 只会消耗处理器周期。您可以调用 System.gc() 来建议 JVM 运行垃圾收集器,但不能保证。

New space in the Heap will be allocated for the new ArrayList.

You can not force garbage collection and assigning null will only serve to eat up processor cycles. You can call System.gc() to suggest the JVM run the garbage collector, but there are no guarantees.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文