首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java内存泄漏

Java内存泄漏
EN

Stack Overflow用户
提问于 2022-01-24 19:47:28
回答 1查看 274关注 0票数 0

我面临一个问题,在服务器上运行一个java应用程序,并且它开始在内存中增长,直到最终服务器无法处理它。

这是某种类型的内存泄漏/资源泄漏问题,我认为这在Java中是非常罕见的,因为垃圾回收。我猜有些东西正在被引用,而且从未被使用过,所以垃圾收集器不会收集它。

问题是内存中的大小增长得如此缓慢,以至于我无法正确地调试它(可能需要两周时间才能使服务器无法使用)。我使用的是java +mysql-连接器,而且我确信内存泄漏是由与数据库连接相关的原因造成的。

下面是我如何连接数据库的方法:

代码语言:javascript
复制
 private static Connection connect(){
    try {
        Connection conn = null;
        conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/database","client","password");
        return conn;
    }catch(SQLException ex){
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
        return null;
    }
}
public static Connection getConnection(){
    try {
        if (connection == null || connection.isClosed()) connection = connect();
        return connection;

    }catch (SQLException exception){
        System.out.println("exception trying to connect to the database");
        return null;
    }
}

我在这里找不到任何可能的问题,但谁知道呢!

下面是我如何从数据库中检索信息:

代码语言:javascript
复制
 public void addPoints(long userId,int cantidad){
    try {
        if(DatabaseConnector.getConnection()!=null) {
            PreparedStatement stm = DatabaseConnector.getConnection().prepareStatement("UPDATE users SET points = points + ? WHERE id = ? ");
            stm.setLong(2, userId);
            stm.setInt(1, cantidad);
            if(stm.executeUpdate()==0){ //user doesn't have any point records in the database yet
                PreparedStatement stm2 = DatabaseConnector.getConnection().prepareStatement("INSERT INTO users (id,points) VALUES (?,?)");
                stm2.setLong(1, userId);
                stm2.setInt(2, cantidad);
                stm2.executeUpdate();
            }
        }
    }catch (SQLException exception){
        System.out.println("error recording points");
    }
}

 public ArrayList<CustomCommand> getCommands(long chatId) throws SQLException{
    ArrayList<CustomCommand> commands = new ArrayList<>();
    if(DatabaseConnector.getConnection() != null) {
        PreparedStatement stm = DatabaseConnector.getConnection().prepareStatement("SELECT text,fileID,commandText,type,probability FROM customcommands WHERE chatid = ?");
        stm.setLong(1, chatId);
        ResultSet results = stm.executeQuery();
        if(!results.isBeforeFirst()) return null;
        while (results.next()){
            commands.add(new CustomCommand(results.getString(1),results.getString(2),results.getString(3), CustomCommand.Type.valueOf(results.getString(4)),results.getInt(5)));
        }
        return commands;
    }
    return null;
}

也许问题与异常捕获和语句不正确执行有关?也许是与结果集有关的东西?快把我逼疯了。谢谢你帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-24 19:57:51

在返回之前,您不需要做任何事情来清理ResultSetStatement。馊主意。您应该在finally块中以单独的try/catch块关闭每个块。

ResultSet是表示数据库上的数据库游标的对象。你应该关闭它,这样你就不会用完游标。

我不会有一个静态的Connection。我希望有一个线程安全的、托管的连接池。

我不会还空的。您没有清楚地说明用户应该使用它做什么。最好是抛出一个异常。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70839634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档