aaberg/sql2o

Memory leak

crysan opened this issue · 4 comments

Hi!

When using this code

Sql2o sql2o = new Sql2o("jdbc:mysql://127.0.0.1:3306/db?autoReconnect=true&failOverReadOnly=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");

		while (true) {
			List<Map<String, Object>> result_ = null;
			try (Connection con = sql2o.open()) {
				result_ = con.createQuery("SELECT * FROM tbltasks WHERE end_date=:end_date LIMIT 1").addParameter("end_date", "1970-01-01 00:00:00").executeAndFetchTable().asList();
				con.close();
			}

			for (Map<String, Object> a_ : result_) {
				int task_id = (int) a_.get("id");
				String type = (String) a_.get("type");
				String content = (String) a_.get("content");

				...
			}
		}

There is a big memory leak. In 1 hour the loss reaches 4 GB.
What to do?
Thank!

Hi! I suppose that this code is running continuously as you have a endless loop in your code:
while(true)
It also seems like the result_ variable is reused during the loop. Is that ever cleaned up? If not that may be the issue here.. Secondly, there is no need to call con.close() as you already are using a try with resources statement which will close the connection after use.

Without further context it is not easy to see how the SQL2O library may have caused this memory leak

Thanks for the answer!

That's right, the program should work endlessly and check the contents of the database.
Redid the program only for this code

		List<Map<String, Object>> result_ = null;
		while (true) {
			try (Connection con = sql2o.open()) {
				result_ = con.createQuery("SELECT * FROM tbltasks WHERE end_date=:end_date LIMIT 1").addParameter("end_date", "1970-01-01 00:00:00").executeAndFetchTable().asList();
				//con.close();
			}

			for (Map<String, Object> a_ : result_) {
				int task_id = (int) a_.get("id");
				String type = (String) a_.get("type");
				String content = (String) a_.get("content");
			}
			
			result_.clear();
		}

the problem is not solved.
Memory leak remained.