So, I'm trying to write a function to scale an image and use it as the background, but it allocates a lot of memory and after a few seconds the out of memory exception is raised. Here's the function that is inside an infinite loop.
public static void scale() {
JLabel background = new JLabel();
Image imgscale = img.getScaledInstance(back.getWidth(), back.getHeight(), Image.SCALE_SMOOTH);
ImageIcon backgroundImage = new ImageIcon(imgscale);
background.setIcon(backgroundImage);
background.setSize(back.getWidth(), back.getHeight());
jlayeredpane1.add(background, Integer.valueOf(0));
jlayeredpane1.remove(background);
frame.setVisible(true);
}