1

Sorry for such simple questions but I am new in Java (usualy write in c)

do new in loop delete old instance of an object?

I need something like

   for(;;)
   {

     // work on here pixels[] 

     source = new MemoryImageSource(200, 200, pixels, 0, 50);
     image  = createImage(source;

    // then use image here

   }

I just need to get here above, new wraping of source and image objects in every frame, and do not want to bother of deleting it, But also I do not want to store large amounts of them as a memory leak,
Will it be deleted automatically? Is it heavy operation?

grunge fightr
  • 1,360
  • 2
  • 19
  • 38

5 Answers5

5

Yes, objects that are no longer being referenced will be garbage collected automatically (at an unspecified point in the future).

I would recommend moving the declaration of source and image into the loop:

for(;;) {
  ...
  Source source = new MemoryImageSource(200, 200, pixels, 0, 50);
  Image image  = createImage(source);
  ...
}

Not only this is preferable stylistically, it also avoids keeping the last iteration's objects around for longer than necessary.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • it’s bad style to declare and initialize objects inside a loop, you consume more resources this way....it’s better to declare them inside methods and when the method is out of scope the objects get cleaned up – firephil Nov 28 '12 at 19:18
  • @firephil: The whole point of the question is that there is a new pair of objects on every iteration. – NPE Nov 28 '12 at 19:20
  • my point is that you can reuse one reference instead say 1000 references if the loop runs 1000 times. Your way every time you loop you alocate new references Source,Image and new Objects Source,Image but you dont have to alocate new references for every iteration just new objects. – firephil Nov 29 '12 at 09:03
2

No, creating and assigning a new instance to a variable in a loop does not 'delete' the old instance it was pointing too. However, as long as there are no other references to the instance it becomes eligible to be garbage collected.

Perception
  • 79,279
  • 19
  • 185
  • 195
1

GC is the basic difference between JAVA and C++. Java developers don't need to care about deleting instances any more and GC mechnism will delete it automatically once the instance is not referenced any more. for example, for (int i =0;i < 2;i ++){ source = new MemoryImageSource(200, 200, pixels, 0, 50); } in the loop,actually, 2 instances are created. But only the second instance is referenced by source after the loop. So the first instance is eligible to be garbage collected.

Wang Wei
  • 573
  • 1
  • 5
  • 10
1

Java behavior inside loop may be rather wierd. See Marko Topolnik's explanation of Java OutOfMemoryError strange behaviour

Community
  • 1
  • 1
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

I would say the most efficient is the following :

Image image;

for(;;) {

    image  = createImage(new MemoryImageSource(200, 200, pixels, 0, 50));
  ...
}

You remove one unnecerary reference which builds up if you use a lot of them and you reuse the Image image reference instead of allocating memory in every iteration for the reference.

firephil
  • 830
  • 10
  • 18