Java memory management

I am a C ++ programmer, and I communicate with java after discovering a JPA, which for some of my current applications is sending God. I haven't touched java since university, and I have problems with a lack of heap. I use the code below as the bulk of the not-so-serious jdbc / jpa / lucene test, but I keep 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 will not publish the whole createDocument file, but all that it does is create an instance of the new org.apache.lucene.document.Document and add the fields via add (new field ...) etc. Only about 50 fields and most of them are short lines (<32 characters) in length.

Is there something completely stupid in my newby-ness that I am doing (or not) why things will not be GC'd?

Java GC?

+5
6

. , , -Xmx n JVM. - , , .

- ? , . , , , , , - .

+3

...

Java ( post postgresSQL mysql oracle difference > ) , JDBC, , .

, 24/7, - JVM - . , , , , JVM , , . PAIN, , JDBC, ... , !

... , , .

, , , , .

, , ResultSets , ( ) , . JDBC , ! !

, , ResultSet , ResultSet.

. RTIII

+2

, . , - java.lang.OutOfMemoryError: PermGen

jvm: -XX: MaxPermSize = 128m

. , JVM.

+2

? , , , addDocument , .

0

Java , OutOfMermoryException. OOM.

You should see a detailed trace of the stack — or perhaps an error dump file in the application directory — which may provide additional information about the problem.

If you use a decent profiler - JVisualVM, which comes with the latest Sun Java 6 JDKs, is probably sufficient - you can monitor all pools and see which ones are exhausted.

0
source

All Articles