内存消耗稳步增加,处理器100%Java App

时间:2019-08-03 22:56:44

标签: java memory processor consumption

我为TeamSpeak服务器构建了一个应用程序(通过专用框架编写的代码),该应用程序包含3个计时器和一些指令,这些指令将数据库中的记录从第二秒更新到第二秒。应用程序在开始的前几分钟正常运行,内存消耗从3.0%逐渐增加,但在29-30%处停止,然后逐渐增加而不减少!在大约运行5-6分钟后,该应用程序开始消耗100%的处理器,并且其功能不再起作用或难以工作。

处理器为100%后会发生此错误:

Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space

SQL文件连接器:

public class SQL {

    public static Connection con;

    String host;
    String name;
    String password;
    String database;

    public SQL(String host, String user, String pw, String db) {
        this.host = host;
        this.name = user;
        this.password = pw;
        this.database = db;
    }

    public void connect() {
        if(!isConnected()) {
            try {
                con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true&serverTimezone=UTC", name, password);
                System.out.println("[MySQL] Modulul MySQL a pornit cu succes!");

            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("[MySQL] Eroare modul SQL: §c" + e.getMessage());

            }

        }
    }

    public void close() {
        if(isConnected()) {
            try {
                con.close();
                System.out.println("[MySQL] Modulu SQL a fost inchis!");

                } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("[MySQL] §4Eroare modul SQL: §c" + e.getMessage());

            }
        }
    }

    public boolean isConnected() {
        return con != null;

    }

    public void createTable(String name, String table) {
        try {
            con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS " + name + "(" + table + ")");
        } catch (SQLException e) { 
            e.printStackTrace();
        }
    }

    public void update(String qry) {
        if(isConnected()) {
            try {
                con.createStatement().executeUpdate(qry);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public ResultSet getResult(String qry) {
        if(isConnected()) {
            try {
                return con.createStatement().executeQuery(qry);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}

计时器构造函数:

            Timer timer = new Timer();
            TimerTask task1 = new TimerActivity();
            timer.scheduleAtFixedRate(task1, 1000, 1000);

计时器文件:

public class TimerActivity extends TimerTask {  

    @Override
    public void run() {
        for(Client c : Load.api.getClients()) {
            if(!(c.isOutputMuted()) && c.isRegularClient()) {
                if(FSQL.Client.ClientExist(c.getUniqueIdentifier())) {
                    try {
                    FSQL.Client.uptodate("users", "timeon = timeon + 1", "uniqid = '"+c.getUniqueIdentifier()+"'");
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else {
                    FSQL.Client.CreateClient(c.getUniqueIdentifier());
                }
            }
        }

    }

}

FSQL类客户端:

public class Client extends Connection {

    public static Object get(String whereresult, String where, String select, String database) {

        ResultSet rs = mysql.getResult("SELECT " + select + " FROM " + database + " WHERE " + where + "='" + whereresult + "'");
        try {
            if(rs.next()) {
                Object v = rs.getObject(select);
                return v;
            }
        } catch (SQLException e) {
            return "ERROR";
        }

        return "ERROR";
    }

    public static int getint(String whereresult, String where, String select, String database) {

        ResultSet rs = mysql.getResult("SELECT " + select + " FROM " + database + " WHERE " + where + "='" + whereresult + "'");
        try {
            if(rs.next()) {
                int v = rs.getInt(select);
                return v;
            }
        } catch (SQLException e) {
            return 0;
        }

        return 0;
    }

    public static boolean exist(String tabela, String conditie, String cautat) {
        try {

            ResultSet rs = mysql.getResult("SELECT * FROM "+tabela+" WHERE "+ conditie + "'");
            if (rs.next()) {
                return rs.getString(cautat) != null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void uptodate(String tabela, String setul, String conditia) {
        mysql.update("UPDATE "+tabela+" SET "+setul+" WHERE "+conditia); 
    }

    public static boolean ClientExist(String id)
    {
        try {

                ResultSet rs = mysql.getResult("SELECT * FROM users WHERE uniqid= '" +id + "'");
            if(rs.next()) {
                return rs.getString("uniqid") !=null;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void CreateClient(String id)
    {
        mysql.update("INSERT INTO users(UNIQID, LEVEL, COINS) VALUES ('"+id+"','0','0')");
        mysql.update("INSERT INTO function(UNIQID, LANG) VALUES ('"+id+"','0')");
    }

    public static void CreateChannel(String id, String data, int idcanal)
    {
        mysql.update("INSERT INTO channelsprivat(CLIENTID, DATE, CHID) VALUES ('"+id+"','"+data+"','"+idcanal+"')");
    }

    public static void CreateTag(int id)
    {
        mysql.update("INSERT INTO tags(IDCHANAL) VALUES ('"+id+"')");
    }

     public static Integer CheckMovePrivat(String id)

    {
        int i = 0;
        try {
        ResultSet rs = mysql.getResult("SELECT pmv FROM function WHERE uniqid= '" +id + "'");
        if(!rs.next() || (Integer.valueOf(rs.getInt("pmv")) == null));
            i = rs.getInt("pmv");
            return i;
        }catch (SQLException e) {
            e.printStackTrace();
            }
        return 0;
     }

     public static Integer IDChanal(String id)

        {
            int i = 0;
            try {
            ResultSet rs = mysql.getResult("SELECT chid FROM channelsprivat WHERE clientid= '" +id + "'");
            if(!rs.next() || (Integer.valueOf(rs.getInt("chid")) == null));
                i = rs.getInt("chid");
                return i;
            }catch (SQLException e) {
                e.printStackTrace();
                }
            return 0;
         }

     public static Integer TimeActiv(String id)

        {
            int i = 0;
            try {
            ResultSet rs = mysql.getResult("SELECT timeon FROM users WHERE uniqid= '" +id + "'");
            if(!rs.next() || (Integer.valueOf(rs.getInt("timeon")) == null));
                i = rs.getInt("timeon");
                return i;
            }catch (SQLException e) {
                e.printStackTrace();
                }
            return 0;
         }


}

在FSQL中是conn的Connection类:

package FSQL;

import Main.SQL;

public class Connection {

    public static SQL mysql;

    public static void Myconnect() {

   mysql = new SQL("localhost", "root", "", "popicu");
        mysql.connect();
    }
}

Load.java

package Main;

import com.github.theholywaffle.teamspeak3.TS3Api;
import com.github.theholywaffle.teamspeak3.TS3Config;
import com.github.theholywaffle.teamspeak3.TS3Query;
import java.util.*;

import Exclusiv.TimerActivity;
import Exclusiv.TimerGroup;
import Exclusiv.TimerTopic;
import FSQL.Connection;

public class Load {

            // Initializare query
            public static final TS3Config config = new TS3Config();
            public static final TS3Query query = new TS3Query(config);
            public static final TS3Api api = query.getApi();

            public static void main(String[] args) {

            config.setHost("ipserver");
            query.connect();
            api.login("username", "password");  
            api.selectVirtualServerByPort(9987);    
            api.setNickname("[SRI] Nea-Popicu");
            System.out.println("Botul este conectat pe serverul dvs.");
            Events.LoadEvents();
            Connection.Myconnect();

            //Timere
            Timer timer = new Timer();
            TimerTask task1 = new TimerActivity(); 
            Timer timer2 = new Timer(); 
            TimerTask task2 = new TimerGroup(); 
            Timer timer3 = new Timer(); 
            TimerTask task3 = new TimerTopic(); 

            timer.scheduleAtFixedRate(task1, 1000, 1000);
            timer2.scheduleAtFixedRate(task2, 1000, 1000);
            timer3.scheduleAtFixedRate(task3, 1000*60, 1000*60);

            }
}

nohup文件或屏幕上没有出现错误。

0 个答案:

没有答案