I'm a c++ programmer and I'm playing around with java after finding JPA which for a few of my current applications is a god send. I haven't touched java since university and I'm having a problem running out of heap space. I'm using the code below as the main part of a not-very-serious test of jdbc/jpa/lucene but I keep on getting random OutOfMemory exceptions.
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select p from Product p" +
" where p.productid = :productid");
Connection con = DriverManager.getConnection("connection string");
Statement st = con.createStatement();
IndexWriter writer = new IndexWriter("c:\\temp\\lucene", new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
ResultSet rs = st.executeQuery("select productid from product order by productid");
while (rs.next()) {
int productid = rs.getInt("PRODUCTID");
q.setParameter("productid", productid);
Product p = (Product)q.getSingleResult();
writer.addDocument(createDocument(p));
}
writer.commit();
writer.optimize();
writer.close();
st.close();
con.close();
I won't post all of createDocument but all it does is instantiate a new org.apache.lucene.document.Document and adds fields via add(new Field...) etc. There are about 50 fields in total and most are short strings (<32 characters) in length.
In my newby-ness is there something completely stupid I'm doing (or not) that would cause things not to be GC'd?
Are there best practices reagrding java memory management and tickling the GC?