Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory not released after processing #541

Open
anitha71 opened this issue May 2, 2024 · 2 comments
Open

Memory not released after processing #541

anitha71 opened this issue May 2, 2024 · 2 comments

Comments

@anitha71
Copy link

anitha71 commented May 2, 2024

I'm using univocity Parser too process csv files, I need to process each and every line so I'm storing each line with corresponding processed data the process ends up in heap space issue and memory is not being released after that

List<Map<String, String>> batchRecords = new ArrayList<>();
StringBuilder keyBuilder = new StringBuilder();
StringBuilder lineDataBuilder = new StringBuilder();
StringBuilder missedColumnFilterValueBuilder = new StringBuilder();
Map<String, String> cleanRecord = new HashMap<>();

        CsvParserSettings parserSettings = new CsvParserSettings();
        parserSettings.setHeaderExtractionEnabled(true);
        parserSettings.getFormat().setDelimiter(compareFileMetaData.userDelimiter);

        parser = new CsvParser(parserSettings);
        parser.beginParsing(new FileReader(inputFile));

        String[] headers = parser.getContext().headers();
        System.out.println(" header processing ......");
        headers = Arrays.stream(headers)
                .map(header -> header.replaceAll("\\p{C}", "").toLowerCase())
                .toArray(String[]::new);

        Map<String, String> row;
        Record record;
        int i=1;
        while ((record = parser.parseNextRecord()) != null) {
            row = record.toFieldMap(headers);
            batchRecords.add(row);
            if (batchRecords.size() == BATCH_SIZE) {
                System.out.println(i*BATCH_SIZE+" Records data preparation started");
                for (Map<String, String> batchCleanRecord : batchRecords) {
                    preprocessMetaData.setRecord(batchCleanRecord);
                    preprocessMetaData.setFileLineData(lineDataBuilder);
                    String actualLineData = fileDataPreprocessing(preprocessMetaData, keyBuilder, missedColumnFilterValueBuilder, cleanRecord);
                    fileContent.put(keyBuilder.toString(), actualLineData);
                    fileMissedFieldFilterMap.put(actualLineData, missedColumnFilterValueBuilder.toString());
                    keyBuilder.setLength(0);
                    missedColumnFilterValueBuilder.setLength(0);
                    lineDataBuilder.setLength(0);
                    cleanRecord.clear();
                }
                batchRecords.clear();
                i++;
            }
        }
        System.out.println("remaining batch processing ");
        for (Map<String, String> batchCleanRecord : batchRecords) {
            preprocessMetaData.setRecord(batchCleanRecord);
            preprocessMetaData.setFileLineData(lineDataBuilder);
            String actualLineData = fileDataPreprocessing(preprocessMetaData, keyBuilder, missedColumnFilterValueBuilder, cleanRecord);
            fileContent.put(keyBuilder.toString(), actualLineData);
            fileMissedFieldFilterMap.put(actualLineData, missedColumnFilterValueBuilder.toString());
            keyBuilder.setLength(0);
            missedColumnFilterValueBuilder.setLength(0);
            lineDataBuilder.setLength(0);
            cleanRecord.clear();
        }
        batchRecords.clear();
        return fileContent;
    } catch (Exception e) {
        System.out.println("file Content size ==>"+fileContent.size());
        e.printStackTrace();
        throw new MatchingServiceException(e.getMessage(), e.getCause());
    }finally {
        if (parser != null) {
            // Close the parser and writer to release resources
            parser.stopParsing();
        }
@ajeethvincil
Copy link

@jbax can you help with this issue ?

@UltraCharge99
Copy link

Please see #534

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants