Java Sigar巨大的RAM使用率

时间:2018-05-16 06:34:51

标签: java ram

我在linux工作站服务器上使用Sigar库(dist.Mint)。这是我的代码:

    ConnectionUtils connection = new ConnectionUtils();
    Sigar sigar = new Sigar();
    String query = "";
    connection.openConnection();

    Mem mem;
    CpuPerc cpuperc;
    FileSystemUsage filesystemusage;


    long RAM;
    long CPU;
    long HDD;

    while(true) {

        mem = sigar.getMem();
        cpuperc = sigar.getCpuPerc();
        filesystemusage = sigar.getFileSystemUsage("/");

        RAM  = Math.round(mem.getUsedPercent());
        CPU  = Math.round((cpuperc.getCombined()*100));
        HDD = Integer.valueOf((int) (filesystemusage.getUsePercent()*100));

        query = "UPDATE ServerInfo SET RAM="+RAM+",CPU="+CPU+",HDD="+HDD+" WHERE ServerName=\"XgenVPS\"";
        connection.sendQuery(query);
        System.out.println("informacje sostały pomyśnie wysłane do bazy danych"); 
        Thread.sleep(1000);
    }
}

}

并且在几个小时之后增加了RAM的使用量,并且在一天之后崩溃了 - 程序停止了间隔并且没有响应,但是因为致命的错误而没有下降。谁能帮我?我希望这个程序可以工作很多天没有那个错误。

哦。这来自ConnectionUtils函数:

private Connection conn;

private String host = "localhost";
private String port = "3306";
private String database = "xgen-dashboard";
private String user = "root";
private String pass = "asdzxcasdzxc123";

public ConnectionUtils() {

}

public synchronized void openConnection(){
    if(!isConnected()){
        try{
            conn = DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database+"?user="+user+"&password="+pass+"&useSSL=false");
        } catch(SQLException e){
            e.printStackTrace();
        }
    }
}

public synchronized void closeConnection(){
    if(isConnected()){
        try{
            conn.close();
        } catch(SQLException e){
            e.printStackTrace();
        }
    }
}

public boolean isConnected() {
    try{
        if(conn == null) return false;
        if(conn.isClosed()) return false;
    } catch(SQLException e){
        e.printStackTrace();
    }
    return true;
}

public ResultSet sendQuery(String query) {
    try {
        return conn.createStatement().executeQuery(query);
    } catch (SQLException e) {
        try {
             conn.createStatement().executeUpdate(query);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    return null;

}

0 个答案:

没有答案