1

这段代码会导致内存泄漏吗?

垃圾收集器什么时候启动?是计时器结束的时候吗?或者即使计时器仍在运行,也会调用 GC?

public static SwingWorker sw;

t2 = new Timer (300,this);
     t2.start();

@Override
public void actionPerformed(ActionEvent arg0) {
try {
    sw = new TextAreaMainPanelWorker();
    sw.execute();
} catch (Throwable e) {
    e.printStackTrace();
}   
}

TextAreaMainPanelWorker 类:

public class TextAreaMainPanelWorker extends SwingWorker<Integer, Integer>
{

protected Integer doInBackground() throws Exception
{
    ConnectMysql.fetchMessage(MainPanel.jtep,MainPanel.sd,MainPanel.count);
    return 1;
}

protected void done()
{
    try
    {
        ConnectMysql.rodolfol(MainPanel.jtep, MainPanel.sd);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

查询数据库的方法:

public static void fetchMessage(JTextPane jtep,StyledDocument sd,int count  )
{
    try{

    String query = "SELECT members.username, message,color FROM chat JOIN members ON chat.user_id = members.id WHERE message_id > "+count+" AND user_id != 1";
    ps = con.prepareStatement(query);
    rs = ps.executeQuery();
    }catch(Exception e){}
}

public static void rodolfol(JTextPane jtep,StyledDocument sd){
    try {
        while(rs.next())
        {
            try {
                final JLabel jp = new JLabel(rs.getString("username")+ "\n");
                jp.setAlignmentY(0.75f);
                final String usernameChat = rs.getString("username");
                jp.addMouseListener(new MouseListener(){

                    @Override
                    public void mouseClicked(MouseEvent e) {}

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                        jp.setCursor(c);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        if(SwingUtilities.isRightMouseButton(e)){System.out.print("lawl");}
                        if(e.getClickCount() == 2)new OneToOneChat(usernameChat);

                        jp.setForeground(Color.BLUE);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                    jp.setForeground(Color.BLACK);
                    }
                });
                jp.setFont(new Font("arial",Font.BOLD,16));
                jtep.insertComponent(jp);
                StyleConstants.setForeground(MainPanel.sas2, Color.BLACK);
                MainPanel.sd.insertString(MainPanel.sd.getLength(), ": ", MainPanel.sas2);
                StyleConstants.setForeground(MainPanel.sas,new Color(Integer.parseInt(rs.getString("color"))));
                sd.insertString(sd.getLength(),rs.getString("message")+ "\n", MainPanel.sas);

            } catch (BadLocationException e1) {
            }finally{
            }
            MainPanel.count++;}
    } catch (SQLException e) {
    }finally{
    if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqlEx) { } 
            rs = null;
        }

    if (ps != null) {
            try {
                ps.close();
            } catch (SQLException sqlEx) { } 
            ps = null;
        }
    }
}
4

2 回答 2

1

每当 JVM 认为有必要这样做时,就会调用 GC。

于 2013-05-14T00:58:17.517 回答
1

垃圾收集器什么时候启动?是计时器结束的时候吗?或者即使计时器仍在运行,也会调用 GC?

垃圾收集随时都会发生,感觉就像。通常,当应用程序内存不足时。GC 可能会在计时器仍在运行时运行,从而使您的应用程序暂停(使用并行/吞吐量收集器)。

所以基本上你无法控制 GC 何时启动。它可能随时发生,无论你在代码中做什么。

于 2013-05-14T01:02:07.387 回答