简体   繁体   中英

java.lang.OutOfMemoryError: Java heap space

i get this error when calling a mysql Prepared Statement every 30 seconds this is the code which is been called:

public static int getUserConnectedatId(Connection conn, int i) throws SQLException {
    pstmt = conn.prepareStatement("SELECT UserId from connection where ConnectionId ='" + i + "'");
    ResultSet rs = pstmt.executeQuery();
    int id = -1;
    if (rs.next()) {
        id = rs.getInt(1);
    }
    pstmt = null;
    rs = null;
    return id;
}

not sure what the problem is :s

您需要关闭所有创建的资源-准备好的语句,结果集等。

Why not make the query parameterized and simply change the connection ID? That's how prepared statements were intended to be used. That way you only compile the statement once, and re-use the compiled query plan (or whatever your DB compiles your query into).

Because you are running out of memory as your ResultSet is occupyng more memory compared to default memory.

Solution: At the time of starting the application use this argument java or javaw -Xms 25M -Xmx 1024M

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM