标签:java memory-leaks swing settext
我注意到我的程序中有内存泄漏.
我已将问题追溯到该线.
Clock.setText("" + h + ":" + df.format(m) + ":" + df.format(s));
我用Google搜索了这个似乎是一个常见的问题,但我还没有找到答案.
有谁知道修复?
这是整个代码:
package CodeBits;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Countdown {
private JFrame Timer;
private JTextField Clock;
private JLabel label;
private JLabel label_1;
static Calendar calendar = new GregorianCalendar();
int minutes = 90;
int count = minutes * 60;
int h;
int m;
int s;
javax.swing.Timer refreshTimer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Countdown window = new Countdown();
window.Timer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Countdown() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Timer = new JFrame();
Timer.getContentPane().setBackground(new Color(173, 216, 230));
Timer.setBounds(0, 0, 135, 100);
Timer.setTitle("Aero Software");
Timer.setIconImage(Toolkit.getDefaultToolkit().getImage("Files/Icon.jpg"));
Timer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
Timer.getContentPane().setLayout(gridBagLayout);
label = new JLabel(" ");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.insets = new Insets(0, 0, 5, 5);
gbc_label.gridx = 0;
gbc_label.gridy = 1;
Timer.getContentPane().add(label, gbc_label);
JLabel lblNewLabel = new JLabel("Countdown Timer");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridwidth = 4;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
Timer.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
Clock = new JTextField();
Clock.setFont(new Font("Arial", Font.BOLD, 22));
Clock.setHorizontalAlignment(JLabel.CENTER);
GridBagConstraints gbc_Clock = new GridBagConstraints();
gbc_Clock.fill = GridBagConstraints.HORIZONTAL;
gbc_Clock.gridwidth = 3;
gbc_Clock.insets = new Insets(0, 0, 5, 5);
gbc_Clock.gridx = 1;
gbc_Clock.gridy = 2;
Timer.getContentPane().add(Clock, gbc_Clock);
Clock.setColumns(10);
label_1 = new JLabel(" ");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.insets = new Insets(0, 0, 5, 0);
gbc_label_1.gridx = 4;
gbc_label_1.gridy = 2;
Timer.getContentPane().add(label_1, gbc_label_1);
// Create countdown timer
ActionListener refreshListener = new ActionListener() {
// Ensure time is fotmatted as 7:04 not 7:4
DecimalFormat df = new DecimalFormat("00");
Calendar countdown = calendar;
{
// Zero the time and add the number of minutes to countdown from
countdown.set(Calendar.HOUR, 0);
countdown.set(Calendar.MINUTE, 0);
countdown.set(Calendar.SECOND, 0);
countdown.set(Calendar.MINUTE, 0);
countdown.add(Calendar.MINUTE, minutes);
}
// Start the timer
public void actionPerformed(ActionEvent e) {
h = countdown.get(Calendar.HOUR_OF_DAY);
m = countdown.get(Calendar.MINUTE);
s = countdown.get(Calendar.SECOND);
calendar.add(Calendar.SECOND, -1);
Clock.setText("" + h + ":" + df.format(m) + ":" + df.format(s));
count--;
if (count == 0) {
System.out.println("Time is up!");
refreshTimer.stop();
}
}
};
refreshTimer = new javax.swing.Timer(1000, refreshListener);
refreshTimer.setInitialDelay(0);
refreshTimer.start();
}
}
解决方法:
你的程序看起来不错.请注意周期性GC如何返回基线.相比之下,这个example泄露了主机资源.最多,您可以考虑在setText()调用中使用StringBuilder
或MessageFormat
.
标签:java,memory-leaks,swing,settext 来源: https://codeday.me/bug/20190610/1208965.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。