2

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.

EnGoPy
  • 333
  • 4
  • 14
  • 1
    The garbage collector will take care of it for you. It will remove the old instance when there are no references to it. – marstran Mar 04 '19 at 18:13
  • 1
    All your questions about the garbage collector are covered in existing descriptions like this one: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html or on Stack Overflow like the question linked above. – Tom Mar 04 '19 at 18:13
  • Topic about garbage collector and your answers completely answered my question. GC topic => general principle of operation for that tool + your answers explained more clear with List use. Thank you – EnGoPy Mar 04 '19 at 18:36

2 Answers2

0

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

Skcussm
  • 658
  • 1
  • 5
  • 20
0

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.

Jochen
  • 193
  • 1
  • 8