我正在制作一个时间表调度程序作为最后一年的项目。在过去的两天里,我收到了 OutOfMemoryException。我已经阅读了很多关于异常的内容,并尝试增加通过 -Xms 和 -Xmx 选项分配的内存。这些似乎都不适合我。
我分析了这个项目,发现最大空间被 hashmap 对象和 MySQL 连接所消耗。我使用了如下静态连接
public final class Connector
{
private static Connector connector;
Connection con;
String driverName;
String dbname;
String username;
String password;
String connString;
private Connector(){
driverName = "com.mysql.jdbc.Driver";
dbname = "timegen";
username = "root";
password = "root";
connString = "jdbc:mysql://localhost:3306/" + dbname;
openConnection();
}
public void openConnection(){
try{
Class.forName(driverName);
con = DriverManager.getConnection(connString, username, password);
} catch(Exception e){
System.out.println(e);
}
}
public void terminateConnection(){
try{
con.close();
} catch(Exception e){
System.out.println(e);
}
}
public static Connector createConnection() {
if (connector == null){
connector = new Connector();
}
return connector;
}
public Connection getCon() {
return con;
}
public String getConnString() {
return connString;
}
public void setConnString(String connString) {
this.connString = connString;
}
}
这是一个名为 MasterData 的类的代码,它由访问数据库的所有其他类扩展
public class MasterData{
static Connector con;
static Statement st;
MasterData(){
try {
con = Connector.createConnection();
st = con.getCon().createStatement();
} catch (SQLException ex) {
Logger.getLogger(MasterData.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Statement createStatement() throws SQLException{
Statement st = con.getCon().createStatement();
return st;
}
public void closeConnection(){
con.terminateConnection();
}
}
一个使用这个的类的例子
public class Teacher extends MasterData{
int teacherid;
String teachername;
String subject;
String post;
@Override
public String toString() {
return "Teacher{" + "teacherid=" + teacherid + ", teachername=" + teachername + ",
post=" + post + ", subject=" + subject + '}';
}
public Teacher(int teacherid, String teachername,String subject, String post) {
this.teacherid = teacherid;
this.teachername = teachername;
this.subject = subject;
this.post = post;
}
public Teacher(String teachername) {
this.teachername = teachername;
}
public Teacher(){}
public String display(){
String s ="\nTeacher name = " + teachername
+ "\nSubject = " + subject
+ "\nPost = "+post;
return s;
}
public ArrayList<String> getSubjectTeachers(String s){
ArrayList<String> teachers = new ArrayList<String>();
try{
ResultSet rs = st.executeQuery("select teachername from teacher where
subject='"+s+"';");
while(rs.next()){
teachers.add(rs.getString(1));
}
}catch(Exception e){e.printStackTrace();}
return teachers;
}
public List<Teacher> getFree()
{
List<Teacher> lst = new ArrayList<Teacher>();
try{
ResultSet rs = st.executeQuery("select * from teacher where teacherid not
in(select classteacher from division where classteacher!=null)");
while(rs.next())
{
lst.add(new
Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)));
}
}catch(Exception e ){e.printStackTrace();}
return lst;
}
public int getTeacherid() {
return teacherid;
}
public void setTeacherid(int teacherid) {
this.teacherid = teacherid;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public boolean checkDuplicate(){
try{
ResultSet rs = st.executeQuery("select * from teacher where
teachername='"+teachername+"';");
if(rs.next())
return true;
}catch(Exception e){e.printStackTrace();}
return false;
}
public boolean insert(){
int t;
try{
t = st.executeUpdate("insert into teacher(teachername,subject,post)
values('"+teachername+"','"+subject+"','"+post+"');");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
public boolean delete(){
int t;
try{
new AssignedTeacher().deleteTeacher(teacherid);
t = st.executeUpdate("delete from teacher where teacherid="+teacherid+";");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
public boolean update(){
int t;
try{
t = st.executeUpdate("update teacher set teachername = '"+teachername+"',
subject='"+subject+"', post='"+post+"' where teacherid="+teacherid+";");
if(t!=0) return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
}
我的目的是为整个程序创建一个静态连接。它似乎运作良好。但这是问题的可能原因吗?