0

I have a text file containing some data. When I tried to put the data in MySQL I get this error "java.lang.OutOfMemoryError: Java heap space".

My code:

        try {
        // create a buffer reader
        java.io.BufferedReader br = new BufferedReader(new FileReader(f));
        // define line
        String line = null;
        // connect to the database
        connect = (Connection) DbConnection.establishConnection();
        while ((line = br.readLine()) != null) {
            // create preparedStatement
            preparedStatement = (PreparedStatement) connect.prepareStatement("insert into  IdentifiedExpertList values (?,?)");

            String[] rowData = line.split("\\s+", 2);
            String firstColumnData = rowData[0];
            String secondColumnData = null;
            if (rowData[1].trim().isEmpty()) {
                secondColumnData = null;
            } else {
                secondColumnData = rowData[1];
            }

            preparedStatement.setString(1, firstColumnData.trim());
            preparedStatement.setString(2, secondColumnData);
            preparedStatement.executeUpdate(); 

        }
        br.close();
        }
4
  • 7
    Why are you preparing a statement on every loop iteration? YOu should only prepare ONCE. that's the whole point of prepared statements. prepare once, use many times. Commented Sep 10, 2012 at 21:28
  • This is not your problem, but -- given that you're splitting line on \s+, you don't need to write firstColumnData.trim(): it's guaranteed that firstColumnData won't contain any whitespace. Commented Sep 10, 2012 at 21:29
  • 2
    Not only do you create PreparedStatements on every iteration, you're not closing them either, hence the OOME. You may want to create a transaction around the loop, too. Commented Sep 10, 2012 at 21:32
  • Yes guys the PreparedStatement was the problem. Putting it out of the loop solve it. Many thanks Commented Sep 10, 2012 at 21:35

1 Answer 1

4
java.lang.OutOfMemoryError: Java heap space

indicates that your allotted memory for JVM is not enough to keep up with the memory required to load the file into memory.

You may use -Xms and -Xmx flags to allocate memory for JVM. If the memory being used is very less, increase it by using those flags and see.

If still you are having memory issues, then another possible solution would be read chunks of the file and upload to DB instead of reading whole file at once.

If none of them works, that means there may be memory leak in your code. You need to fix it.

Sign up to request clarification or add additional context in comments.

1 Comment

Although this is in general the cause of an OOME, this is not the reason in this case. Look at the comments to the original answer, not calling close() on the prepared statement is the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.