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

@Size without max attribute causes a java.lang.OutOfMemoryError: Java heap space #289

Closed
Redid opened this issue Dec 4, 2017 · 3 comments
Labels

Comments

@Redid
Copy link

Redid commented Dec 4, 2017

Hi random-beans Team,

@Size on String is causing java.lang.OutOfMemoryError: Java heap space.
This bug is not always happening, but during debugging there are always problems.

Example:

public class TestString {
    @Size(min = 2)
    public String str1;
}

Also it is not working at all with validation-api pre 2.0, even if Java heap space doesn't appear @Size is just ignored.

Best regards,
Redid

@fmbenhassine
Copy link
Member

This bug is not always happening

Yeah, with random beans, we have random bugs 😄 Sorry, I couldn't resist on this..

Anyway, thank you for pointing this out!
Will take a look at this and get back to you asap

@fmbenhassine
Copy link
Member

fmbenhassine commented Feb 11, 2018

Hi,

The issue does not happen when both min and max attributes are specified.
But I do confirm there is a OOM error when the max attribute is not specified.

Thank you for pointing this out! Let me try to fix that for v3.8.0.

Kr
Mahmoud

@fmbenhassine fmbenhassine added this to the v3.8.0 milestone Feb 11, 2018
@fmbenhassine fmbenhassine changed the title @Size causing java.lang.OutOfMemoryError: Java heap space @Size without max attribute causes a java.lang.OutOfMemoryError: Java heap space Feb 11, 2018
@fmbenhassine
Copy link
Member

fmbenhassine commented Nov 18, 2018

Hi,

When the max attribute is not specified, the default max value is Integer.MAX_VALUE and the generated random string length can be too high and causes the OOM (The reason for that is the arrays copy at java.util.Arrays.copyOf(Arrays.java:3332)).

When I first analysed this issue, I thought that the cause of the OOM is the string concatenation in StringRandomizer#getRandomValue:

@Override
public String getRandomValue() {
   StringBuilder stringBuilder = new StringBuilder();
   int length = minLength + random.nextInt(maxLength - minLength + 1);
   for (int i = 0; i < length; i++) {
      stringBuilder.append(characterRandomizer.getRandomValue());
   }
   return stringBuilder.toString();
}

I changed the above code to:

@Override
public String getRandomValue() {
    int length = minLength + random.nextInt(maxLength - minLength + 1);
    char[] chars = new char[length];
    for (int i = 0; i < length; i++) {
        chars[i] = characterRandomizer.getRandomValue();
    }
    return new String(chars);
}

which is better but the OOM still happens for a large length.

Random Beans can do nothing in this case. We need to:

  • either allocate more memory to the JVM (with a -Xmx512m, I tired to dump the value of the string to a text file when the generated length was 374668018 and it worked, the file size was 374m)
  • or specify a reasonable value for the max size of the string

BTW, here is the exact same code outside of Random Beans which also causes an OOM with a high length value:

import java.util.Random;

class Scratch {
    public static void main(String[] args) {
        Random random = new Random();
        int length = 374668018; 
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = (char) random.nextInt(127);
        }
        String value = new String(chars); // java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332)
        System.out.println("value = " + value);
    }
}

That said, I'm closing this issue.

Kr,
Mahmoud

@fmbenhassine fmbenhassine removed this from the v3.8.0 milestone Nov 18, 2018
@fmbenhassine fmbenhassine added invalid and removed bug labels Nov 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants