I developed a web scraper. The Web scraper uses 6 threads, each thread opens a web page, gets the text of an article, than writes (using a driver) each single word of the text in a mysql database.
During the execution of the program I get a java mysql java.lang.OutOfMemoryError: Java heap space. I installed Memory Analyzer on Eclipse and found that the problem is caused by the mysql driver connection: When I run this program, after 5 minutes the ram occupied by the driver is 6 MB, after another 5 minutes 200MB, after other 5 minutes 500Mb and then i get the java error heap space.
I don't understand why this happens.
Here is the code i use for the model (to access mysql DB)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class model {
private Connection connect = null;
public model(){
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/system?user=keyword_tool&password=l0gripp0");
} catch (Exception e) {
System.out.println(e);
}
}
public synchronized void insertCat(String parola, String categoria){
try{
PreparedStatement statement = connect.prepareStatement("insert into sostantivi (nome, categoria) values (?, ?)");
statement.setString(1, parola);
statement.setString(2, categoria);
statement.executeUpdate();
statement.close();
} catch (Exception e){
//System.out.println(e);
}
}
public void closeDBConnection() {
try {
connect.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Each thread simply call the method insertCat and insert a word with a category in the database.
The Memory Analyzer plugin of Eclipse says:


closeDBConnection()is getting called? Put some logging in. Since you don't show that code, most likely is thatmodel()constructor is called repeatedly but due to some flaw in your code connections are leaked or not closed.