Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7421347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:23:34+00:00 2026-05-29T08:23:34+00:00

I’m still a relatively new programmer, and an issue I keep having in Java

  • 0

I’m still a relatively new programmer, and an issue I keep having in Java is Out of Memory Errors. I don’t want to increase the memory using -Xmx, because I feel that the error is due to poor programming, and I want to improve my coding rather than rely on more memory.

The work I do involves processing lots of text files, each around 1GB when compressed. The code I have here is meant to loop through a directory where new compressed text files are being dropped. It opens the second most recent text file (not the most recent, because this is still being written to), and uses the Jsoup library to parse certain fields in the text file (fields are separated with custom delimiters: “|nTa|” designates a new column and “|nLa|” designates a new row).

I feel there should be no reason for using a lot of memory. I open a file, scan through it, parse the relevant bits, write the parsed version into another file, close the file, and move onto the next file. I don’t need to store the whole file in memory, and I certainly don’t need to store files that have already been processed in memory.

I’m getting errors when I start parsing the second file, which suggests that I’m not dealing with garbage collection. Please have a look at the code, and see if you can spot things that I’m doing that mean I’m using more memory than I should be. I want to learn how to do this right so I stop getting memory errors!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.jsoup.Jsoup;

public class ParseHTML {

    public static int commentExtractField = 3;
    public static int contentExtractField = 4;
    public static int descriptionField = 5;

    public static void main(String[] args) throws Exception {

        File directoryCompleted = null;     
        File filesCompleted[] = null;

        while(true) {

            // find second most recent file in completed directory  
            directoryCompleted = new File(args[0]);     
            filesCompleted = directoryCompleted.listFiles();

            if (filesCompleted.length > 1) {

                TreeMap<Long, File> timeStamps = new TreeMap<Long, File>(Collections.reverseOrder());

                for (File f : filesCompleted) {
                    timeStamps.put(getTimestamp(f), f);
                }

                File fileToProcess = null;

                int counter = 0;

                for (Long l : timeStamps.keySet()) {
                    fileToProcess = timeStamps.get(l);
                    if (counter == 1) {
                        break;
                    }
                    counter++;
                }   

                // start processing file
                GZIPInputStream gzipInputStream = null;

                if (fileToProcess != null) {
                    gzipInputStream = new GZIPInputStream(new FileInputStream(fileToProcess));
                }

                else {
                    System.err.println("No file to process!");
                    System.exit(1);
                }

                Scanner scanner = new Scanner(gzipInputStream);
                scanner.useDelimiter("\\|nLa\\|");

                GZIPOutputStream output = new GZIPOutputStream(new FileOutputStream("parsed/" + fileToProcess.getName()));

                while (scanner.hasNext()) {
                    Scanner scanner2 = new Scanner(scanner.next()); 
                    scanner2.useDelimiter("\\|nTa\\|");

                    ArrayList<String> row = new ArrayList<String>();

                    while(scanner2.hasNext()) {
                        row.add(scanner2.next());   
                    }

                    for (int index = 0; index < row.size(); index++) {
                        if (index == commentExtractField ||
                                index == contentExtractField ||
                                index == descriptionField) {
                            output.write(jsoupParse(row.get(index)).getBytes("UTF-8"));
                        }

                        else {
                            output.write(row.get(index).getBytes("UTF-8"));
                        }   

                        String delimiter = "";

                        if (index == row.size() - 1) {
                            delimiter = "|nLa|";
                        }

                        else {
                            delimiter = "|nTa|";
                        }

                        output.write(delimiter.getBytes("UTF-8"));
                    }
                }

                output.finish();
                output.close();
                scanner.close();
                gzipInputStream.close();


            }
        }
    }

    public static Long getTimestamp(File f) {
        String name = f.getName();
        String removeExt = name.substring(0, name.length() - 3);
        String timestamp = removeExt.substring(7, removeExt.length());
        return Long.parseLong(timestamp);
    }

    public static String jsoupParse(String s) {
        if (s.length() == 4) {
            return s;
        }

        else {
            return Jsoup.parse(s).text();
        }
    }
}

How can I make sure that when I finish with objects, they are destroyed and not using any resources? For example, each time I close the GZIPInputStream, GZIPOutputStream and Scanner, how can I make sure they’re completely destroyed?

For the record, the error I’m getting is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572)
at java.lang.StringBuilder.append(StringBuilder.java:203)
at org.jsoup.parser.TokeniserState$47.read(TokeniserState.java:1171)
at org.jsoup.parser.Tokeniser.read(Tokeniser.java:42)
at org.jsoup.parser.TreeBuilder.runParser(TreeBuilder.java:101)
at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:53)
at org.jsoup.parser.Parser.parse(Parser.java:24)
at org.jsoup.Jsoup.parse(Jsoup.java:44)
at ParseHTML.jsoupParse(ParseHTML.java:125)
at ParseHTML.main(ParseHTML.java:81)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:23:35+00:00Added an answer on May 29, 2026 at 8:23 am

    Update: This issue was fixed in JSoup 1.6.2

    It looks to me like it’s probably a bug in the JSoup parser that you’re using…at present the documentation for JSoup.parse() has a warning “BETA: if you do get an exception raised, or a bad parse-tree, please file a bug.” Which suggests they aren’t confident that it’s completely safe for use in production code.

    I also found several bug reports mentioning out of memory exceptions, one of which suggests that it’s due to parse error objects being held statically by JSoup, and that downgrading from JSoup 1.6.1 to 1.5.2 may be a work-around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.