Update ArrayList with new object+deleting old object [duplicate]

问题: This question already has an answer here: What is the garbage collector in Java? 16 answers I'm still learning basics of Java and try to understand...

问题:

This question already has an answer here:

I'm still learning basics of Java and try to understand that language on different levels. I have a question regarding to creating/deleting old objects in Java.

I have got an example:

Let say that I have got laptopList = new ArrayList<Laptop>; which contains objects of class Laptop. We have got 20 created objects, all added to the ArrayList. Now we want to update fourth index with a new Laptop object. In this case we can create new Laptop object and use laptopList.set(4, new Laptop("Dell",1024));.

In this point I would like to ask you -> We will not use that old instance anymore, but it still exists in memory. Should we set old instance to null value (or somehow else delete it)? I know for PC's it might be no problem but what impact has it to mobile devices (memory use => faster discharging of battery).

I know that in Java is implemented tool called Garbage Collector. Does it work in this case? Does it delete non use instances immediately? How and when it knows that particular instance will not be used anymore?

Thank you in advance for your response and information.


回答1:

Java's garbage collector will handle freeing up the memory of the object that was previously at index 4 assuming that it can be collected. It should be collectable if it isn't referenced by another part of your code. Java will run the java collector periodically so there is no reason to run it yourself.

Java supports numerous implementations of the garbage collector which are optimized for different circumstances. The default garbage collector typically works well for most general applications.

In a mobile application, the JVM running on the mobile device should use a garbage collector by default that is optimized for a mobile environment. You can learn more about the java's garbage collection here: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html


回答2:

Well, you don't have any chance to directly delete an object in Java.

In the case you draw in your posting, the laptops are only referenced by the ArrayList. As soon as you store a new object on 4th index, there is no remaining reference to the object, you have overwritten. So that object can be deleted.

And, as you already guessed, the garbage collector will take care of this unused object. But to response to your answer: you will not know, when this happens. Maybe the garbage collector will immediately start to run and remove the object from memory. Most probably, the garbage collector will not run immediately, so the object remains in memory. The garbage collector may run, when the application is idle, but it will definitely run, as soon as memory is needed. You may ask the JVM to run the garbage collector (System.gc();), but again, you will not have absolute control over the garbage collector. It may also run a few milliseconds later.

The topic of memory usage based on undeleted objects is a bit different from compiled languages. When you start the JVM (start an application using the JVM), the JVM requests a certain amount of memory from the underlying OS (you may control this amount while starting the JVM with certain flags), so the JVM controls the memory on its own. This means, the memory is totally under control of the JVM and thus the fewer battery usage will not hold here.

  • 发表于 2019-03-06 03:44
  • 阅读 ( 203 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除