-2

I wanted to make a program that outputs a string repeated a given number of times (separated by a space) using String.repeat, but have been running into a java.lang.OutOfMemoryError when the string is repeated too many times. How can I determine the maximum number of times a string can be repeated without causing an out of memory error?

I search online for the maximum length of a string and came up with 2147483647. In my code, I divide this maximum length by the length of the string to repeat. I wanted it to round off automatically, so I used the int data type. I expected my program to be able to print the word, but instead of printing the result it still generates an out of memory error. Is the maximum string length correct? If not, what is the maximum string length?

import java.util.*;
public class Darshit {
  public static void main(String[] Darshit1) {
    Scanner Darshit = new Scanner(System.in);
    System.out.println("WELCOME TO WORD RE-PRINTER!");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("Enter the text");
    String b = Darshit.nextLine();
    int len = b.length()+1;
    int e = 2147483647/len;
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("How many times you want to repeat the text");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("Note ~>");
    System.out.println("You can only print the word upto " + e + " times!");
    int a = Darshit.nextInt();
    String c = " ";
    String d = b + c;
    System.out.println(d.repeat(a));
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("Thank you for using it");
    System.out.println("Made by Darshit Sharma");
    System.out.println("8th-C");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  }
}
outis
  • 75,655
  • 22
  • 151
  • 221
  • On the assumption that you entered a large repeat-count - it's simple: there's not enough available memory to hold a string that size. The 2 billion char limit on String length is a theoretical maximum, it does not guarantee that you have enough memory to make one that long. And you don't have enough. That;'s at least 4GB for the single String, probably plus a copy made internally while println() is running. – user16632363 Sep 10 '21 at 11:42
  • Okay , but what is the maximum limit that is recommended for smooth running and how can I print that to user – Darshit Sharma 8th C Sep 10 '21 at 11:44
  • I mean what is the formula for finding out that maximum limit for that string – Darshit Sharma 8th C Sep 10 '21 at 11:45
  • (1) There's no such limit, it depends on the memory available to the Java Virtual Machine, which you can specify at JVM startup (if you use an IDE, I have no idea how they set it). (2) In the general case it is very difficult to predict the limit on one String since it's a whole-program limit. (3) A better way is to choose a reasonable maximum repeat count (value of 'e') and test to that limit. That's general programming advice: decide what you want to support and test to it. – user16632363 Sep 10 '21 at 11:48
  • The period: your friend at the end. – outis Sep 11 '21 at 21:00

1 Answers1

0

Generally speaking, there are two approaches to handling errors:

  1. test beforehand whether performing an operation will cause error and not perform the operation if it will (defensive programming)
  2. trap errors if they happen (exception handling)

Different platforms have different conventions about which approach is preferred in which context (assuming exception handling is even supported). In this case, however, there is no reliable way to test whether there will be an error caused by repeating the string too many times.

In many situations, handling an OutOfMemoryError won't lead anywhere, as there isn't a way to recover and get the application back to a valid state, mainly because if the application is running low on memory, it probably won't be able to do anything useful. It might be able to log or print a message explaining why it's crashing, so the program could catch & rethrow, but usually not much more than that. In this case, your program has enough memory for most of its tasks (just not enough for the primary task of allocating the repeated string) and is simple enough that you can handle it simply: print a message explaining what happened. After that, the application is already close to exiting, so no other handling should be needed.

    try {
        System.out.println(d.repeat(a));
    } catch (java.lang.OutOfMemoryError oome) {
        System.err.println("I ran out of memory trying to repeat the string. You asked for too many repetitions.");
    }
outis
  • 75,655
  • 22
  • 151
  • 221