I have a counter that counts ne the amount of time

I noticed when I opened the task manager that with every second the memory used by the application increases, which is not good

How to use variables so that the memory is recklessly not wasted

When I read the FAQ I found out that you can't just use string variables. You need to use a StringBuffer or StringBuilder to change it frequently.

Since I have such information displayed in the application, I will give a small sample of code without libraries and the rest of the unnecessary code.

public static StringBuffer time_buffer = new StringBuffer(); public void timerStart() { timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { time_buffer.setLength(0); time_buffer.append(Long.toString(System.currentTimeMillis()/1000)); jLabel.setText(time_buffer); ///Здесь условие выхода и остановка таймера timer.stop(); } } } public static void main() { Frame window = new Frame(); window.setVisible(true); timer.start(); window.timerStart(); } 
  • so that the memory is recklessly not wasted - what volumes are we talking about? - Nofate
  • Well, let's say every second a new value is written to StringBuffer within 2 hours and in all this time about 100 or more MB of memory can be spent - MaximPro
  • Why don't you just want to write jLabel.setText(Long.toString(System.currentTimeMillis()/1000)); ? - Nofate
  • This is a slightly simplified example, which is why it is written like this here. And why will it change? - MaximPro

0