简体   繁体   中英

OutOfMemory Error when transferring data from text file to MySQL table in JAVA

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();
        }
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM