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 7809529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:27:57+00:00 2026-06-02T03:27:57+00:00

When parsing a large file I get the following error Caught: java.lang.OutOfMemoryError: Java heap

  • 0

When parsing a large file I get the following error Caught: java.lang.OutOfMemoryError: Java heap space

How do you parse large files in Groovy without exceeding heap size?

example code that fails with large files…

import java.io.File

def inputFile = new File("c:/dev/test.txt")
    inputFile.getText().eachLine{ it, i ->
            ... do something with each line
        }
  • 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-06-02T03:27:58+00:00Added an answer on June 2, 2026 at 3:27 am

    Ensure that the you’re iterating over the file in a way that doesn’t load the whole file into memory…

    • In this case specifically turn inputFile.getText().eachLine into inputFile.eachLine
    • Don’t use .readLines() as it will try and load the whole file into memory, .eachLine{..} should be used instead
    • You can also extend the heap size with a jvm flag, eg to 1GB by using groovy -Xmx1024M myscript.groovy See also answer here

    See this page on the groovy mailing list for more info and further discussion

    Code that works without a heap space error…

    def inputFile = new File("c:/dev/test.txt")
    inputFile.eachLine{ it, i ->
            ... do something with each line
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse a section of a large file with Java's Scanner library,
I am parsing a very large file of records (one per line, each of
I'm working on a project that involves parsing a large csv formatted file in
I am parsing a text file and I get multiple lines in the form
What is the cost of parsing a large XML file using PHP on every
I'm parsing a large file in Perl line-by-line (terminated by \n), but when I
I'm having a bit of an issue. I'm parsing a large xml file using
I have a large Regular Expression I use for parsing my own file format
When I try to paste a large(5000 lines) sql file into PhpMyAdmin, I get
I am currently parsing a large number of zone files at my work so

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.