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

  • Home
  • SEARCH
  • 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 344271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:58:30+00:00 2026-05-12T10:58:30+00:00

I have a problem with my Java progam suddenly exiting, without any exception thrown

  • 0

I have a problem with my Java progam suddenly exiting, without any exception thrown or the program finishing normally.

I’m writing a program to solve Project Euler‘s 14th problem. This is what I got:

private static final int INITIAL_CACHE_SIZE = 30000;
private static Map<Long, Integer> cache = new HashMap<Long, Integer>(INITIAL_CACHE_SIZE);

public void main(String... args) {
    long number = 0;
    int maxSize = 0;

    for (long i = 1; i <= TARGET; i++) {
        int size = size(i);
        if (size > maxSize) {
            maxSize = size;
            number = i;
        }
    }
}
private static int size(long i) {
    if (i == 1L) {
        return 1;
    }
    final int size = size(process(i)) + 1;
    return size;
}

private static long process(long n) {
    return n % 2 == 0 ? n/2 : 3*n + 1;
}

This runs fine, and finishes correctly in about 5 seconds when using a TARGET of 1 000 000.

I wanted to optimize by adding a cache, so I changed the size method to this:

private static int size(long i) {
    if (i == 1L) {
        return 1;
    }
    if (cache.containsKey(i)) {
        return cache.get(i);
    }
    final int size = size(process(i)) + 1;
    cache.put(i, size);
    return size;
}

Now when I run it, it simply stops (process exits) when I get to 555144. Same number every time. No exception, error, Java VM crash or anything is thrown.

Changing the cache size doesn’t seem to have any effect either, so how could the cache
introduction cause this error?

If I enforce the cache size to be not just initial, but permanent like so:

    if (i < CACHE_SIZE) {
        cache.put(i, size);
    }

the bug no longer occurs.
Edit: When I set the cache size to like 2M, the bug starts showing again.

Can anyone reproduce this, and maybe even provide a suggestion as to why it happens?

  • 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-12T10:58:30+00:00Added an answer on May 12, 2026 at 10:58 am

    This is simply an OutOfMemoryError that is not being printed. The program runs fine if I set a high heap size, otherwise it exits with an unlogged OutOfMemoryError (easy to see in a Debugger, though).

    You can verify this and get a heap dump (as well as printout that an OutOfMemoryError occurred) by passing this JVM arg and re-running your program:

    -XX:+HeapDumpOnOutOfMemoryError

    With this it will then print out something to this effect:

    java.lang.OutOfMemoryError: Java heap space
    Dumping heap to java_pid4192.hprof …
    Heap dump file created [91901809 bytes in 4.464 secs]

    Bump up your heap size with, say, -Xmx200m and you won’t have an issue – At least for TARGET=1000000.

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

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer public static DateTime StartOfRecordedHistory = new DateTime(2004, 1, 1) May 12, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer Looking at the documentation have tried this:- var sb =… May 12, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer Here is a working copy in plain JavaScript using no… May 12, 2026 at 3:11 pm

Related Questions

I'm aware of how to start a java progam with a java agent: java
I have a problem with MDS-CS simulator coming with JDE 4.5.0 installation. I have
I have a problem with a really slow connection between my Java code and
I have a simple-ish cell renderer which is composed of a few JLabel s

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.