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?
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
sourceandimageinto the loop:Not only this is preferable stylistically, it also avoids keeping the last iteration’s objects around for longer than necessary.