简体   繁体   English

Java ArrayList内存问题

[英]Java ArrayList Memory Issue

I have the following code: 我有以下代码:

  result = binding.downloadData(sourceURLString.replace("{CAT_ID}", catId), Data.class);

  ArrayList<Data> mAllProducts = result.getProducts();
  cloneList(mAllProducts);
  System.gc();

And here is the deep copy of the mAllProducts ArrayList 这是mAllProducts ArrayList的深层副本

  static List<Data> clone;
  public static void cloneList(ArrayList<Data> list) {
    clone = new ArrayList<Data>();
    for(Data item: list){ 
        clone.add(new Data(item));
         }
 }

Data Constructor: 数据构造器:

 public Data(Data item2) {
    this.imageUrl = item2.imageUrl;
                   *
                   *
  }

My questions are: 我的问题是:

  1. Will the mAllProducts arraylist collected by the garbage collector? 垃圾收集器会收集mAllProducts数组列表吗?
  2. Is the clone list a passed by value ArrayList? 克隆列表是按值ArrayList传递的吗?
  3. If the answer at the 2nd question is yes, that means that the clone arraylist doesn't have a reference to the memory? 如果第二个问题的答案是肯定的,那意味着克隆arraylist没有对内存的引用吗?
  4. And finally, if the answer at the second question is yes, that means that will stay at the memory only for the time is being used by the system and then will be garbage collected? 最后,如果第二个问题的答案是“是”,那么这意味着将仅在系统正在使用的时间停留在内存中,然后才会进行垃圾回收吗?

1) No way to know, your gc call is merely a suggestion that the JVM try to perform a collection. 1)没有办法知道,您的gc调用仅是JVM尝试执行收集的建议。

2) Everything in Java is pass by value. 2)Java中的所有内容都是按值传递的。

3) I don't know what you mean. 3)我不知道你的意思。 But your clone, assuming it creates new items for the list, and the items don't share references to any objects, is completely separate from the original list. 但是您的克隆(假设它为该列表创建了新项目,并且这些项目不共享对任何对象的引用)与原始列表完全不同。 Primitive values like ints are immutable, it's only object instances you have to worry about. 像int这样的原始值是不可变的,它只是您需要担心的对象实例。 It seems you are using a copy constructor, so be extra careful you copy any objects each item contains, as well as any items those children might contain; 似乎您正在使用复制构造函数,因此请格外小心,复制每个项目包含的任何对象以及子项可能包含的任何对象; your copy needs to be deep. 您的副本需要很深。

4) I don't know what you mean. 4)我不知道你的意思。 If you don't have any references to the original it will be eligible for collection the next time the GC runs. 如果您没有对原始文件的任何引用,则下次GC运行时将有资格进行收集。

Will the mAllProducts arraylist collected by the garbage collector? 垃圾收集器会收集mAllProducts数组列表吗?

Only when 1) The garbage collector decides to do so and 2) When it falls out of scope 仅当1)垃圾收集器决定这样做和2)当它超出范围时

Is the clone list a passed by value ArrayList? 克隆列表是按值ArrayList传递的吗?

Yes

If the answer at the 2nd question is yes, that means that the clone arraylist doesn't have a reference to the memory? 如果第二个问题的答案是肯定的,那意味着克隆arraylist没有对内存的引用吗?

Definitely needs a reference to some point in memory, else it can't exist in a logical system ie a computer. 绝对需要引用内存中的某个点,否则就不能存在于逻辑系统(例如计算机)中。

And finally, if the answer at the second question is yes, that means that will stay at the memory only for the time is being used by the system and then will be garbage collected? 最后,如果第二个问题的答案是“是”,那么这意味着将仅在系统正在使用的时间停留在内存中,然后才会进行垃圾回收吗?

Again the garbage collector will collect it when it is deemed fit to do so. 同样,垃圾收集器将在认为合适的情况下进行收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM