内存消耗稳步增长,处理器100%使用Java应用程序

内存消耗稳步增长,处理器100%使用Java应用程序,java,memory,processor,consumption,Java,Memory,Processor,Consumption,我为TeamSpeak服务器构建了一个应用程序(我通过一个专用框架编写的代码),该应用程序包含3个计时器和一些指令,这些指令每秒更新数据库中的记录。应用程序在最初几分钟内正常运行,内存消耗从3.0%逐渐增加,但在29-30%时停止,逐渐增加而不减少!在大约5-6分钟的操作后,应用程序开始消耗100%的处理器,其功能不再工作,或者无法正常工作 此错误在处理器100%运行后发生: Exception in thread "Timer-0" java.lang.OutOfMemoryError: Ja

我为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的类连接:

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文件或屏幕上不会出现错误。

在这种情况下,什么是
FSQL
?你能提供你正在使用的框架的名称吗?吹毛求疵:它是Java,不是Java。它不是首字母缩写。@Dominikowsiński我发布了FSQL包,框架名称是TeamSpeak 3 API:。谢谢大家的兴趣。所以,我看不出应该在哪里调用
Myconnect
静态方法来初始化SQL连接。我也无法在TeamSpeak Api中找到
Load
类。@Dominikowsiński
Load
类它是调用
函数和设置
查询的主要应用类。我已经发布了
加载
类在这种情况下什么是
FSQL
?你能提供你正在使用的框架的名称吗?吹毛求疵:它是Java,不是Java。它不是首字母缩写。@Dominikowsiński我发布了FSQL包,框架名称是TeamSpeak 3 API:。谢谢大家的兴趣。所以,我看不出应该在哪里调用
Myconnect
静态方法来初始化SQL连接。我也无法在TeamSpeak Api中找到
Load
类。@Dominikowsiński
Load
类它是调用
函数和设置
查询的主要应用类。我已经发布了
加载