ArrayList和内存分配 [英] ArrayList and memory Allocation

查看:103
本文介绍了ArrayList和内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我们有一个应用程序,它存储一组自定义类值作为arraylist。使用应用程序时,将修改arraylist中的数据。即,将根据业务功能插入和删除一组数组。

We have an application which stores a set of custom class values as arraylist. Data in arraylist will be modified when application is used. i.e. A set of array will be inserted and removed based on the business functionality.

但在初始阶段,arraylist的内存最小。但是当应用程序运行近12小时时,内存会增加。

But in the initial stage the memory for the arraylist is minimum. But when the application is running for nearly 12 hrs the memory is increased.

即。 arraylist的初始内存为10 MB。 12小时后的内存为70 MB。

i.e. initial memory for the arraylist is 10 MB. Memory after 12 hrs is 70 MB.

arraylist是否会从应用程序中删除不需要的或未使用的内存或将保留内存。

Will arraylist will removed unwanted or unused memory from application or will hold the memory.

谢谢 

Sri

推荐答案

ArrayList不会自动 - 删除项目,因为它不知道你何时完成它们。如果您从ArrayList中删除项目,那么这些项目最终将被垃圾收集。

ArrayList will not auto-remove items because it doesn't know when you're done with them. If you remove items from the ArrayList then those items will eventually be garbage collected.

如果您想暂时存储项目并自动清理它们,那么您应该考虑使用MemoryCache或等效项目代替。可以将缓存配置为在固定或滑动到期后以及内存约束
变紧时自动删除项目。但是,您的代码必须处理您请求的项目不再位于缓存中的事实。

If you want to store items temporarily and have them cleaned up automatically then you should be looking into using MemoryCache or equivalent instead. A cache can be configured to auto-remove items after a fixed or sliding expiration and when memory constraints get tight. However your code has to handle the fact that the item you are requesting is no longer in the cache.

另一种方法是仅保留您实际需要的项目。不知道你在使用ArrayList是什么,很难说该做什么,但如果你只需要跟踪一些项目,那么我会说在内存中创建项目列表,做你的工作然后
然后释放它们。只有你需要它们时才能保持它们。如果你需要它们,那么你无论如何也无法真正自动删除它们。

An alternative is to keep only the items you actually need. Not knowing what you're using the ArrayList for it is hard to say what to do but if you just need to keep track of some items then I would say create the list of items in memory, do your work and then release them. Only keep them around if you need them. And if you need them then you cannot really auto-remove them anyway.

最后我强烈建议从ArrayList转到
List< T>
。您真的需要在一个列表中存储不匹配的项目是非常罕见的,因此ArrayList效率低下。在最糟糕的情况下,你存储的是值类型,因此你可以装箱/拆箱。在最好的情况下,你的代码
充满了类型转换,这会降低代码的速度。使用List< T>将消除这些问题并减少您必须维护的代码量。它并没有改变你发布的问题,只是一个额外的建议。

Finally I'd strongly recommend going away from ArrayList to List<T>. It is exceedingly rare that you really need to store mismatched items in a single list so ArrayList is going to be inefficient. In the worst case you're storing value types and so you're boxing/unboxing everything. In the best case your code is littered with typecasts which slows down the code. Using List<T> would eliminate both these problems and reduce the amount of code you have to maintain. It doesn't change the problem you posted about though, just an extra recommendation.


这篇关于ArrayList和内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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