Skip to content

Shrinking of large arrays uses a very large amount of memory #525

Closed

Description

@FeldrinH

I was trying to test a property with the following arbitrary as input:

Arbitraries.randomValue(generateValue).array(long[].class).ofMinSize(1).ofMaxSize(10_000)
// (generateValue is a custom generation function that is not relevant to the problem)

The test works fine on my desktop computer, however, when running tests in a test server with only 1 GB of memory available to Java I got an OutOfMemoryError. Some memory usage profiling seems to indicate that the root cause is the use of Combinatorics.distinctPairs inside ShrinkingCommons.shrinkPairsOfElements. Oddly enough shrinkPairsOfElements is called even though values returned by Arbitraries.randomValue are unshrinkable.

Is there something that can be done to reduce the memory usage during shrinking of large arrays?
Can I somehow explicitly disable shrinking pairs of elements to limit memory usage?

PS: In this particular case adding more memory is not an option. The tests must pass with 1 GB of available memory because the ability to operate with limited memory is also a requirement being tested.

Activity

  1. FeldrinH commented on Oct 26, 2023

    @FeldrinH
    Author

    I propose that a fairly simple solution would be to generate the pairs dynamically without storing them in a list.
    For example something like this:

        public static Stream<Tuple2<Integer, Integer>> distinctPairs(int maxExclusive) {
            if (maxExclusive < 2) {
                return Stream.empty();
            }
            return StreamSupport.stream(new PairSpliterator(maxExclusive), false);
        }
    
        private static class PairSpliterator implements Spliterator<Tuple2<Integer, Integer>> {
            private final int maxExclusive;
    
            private int i = 0;
            private int j = 1;
    
            public PairSpliterator(int maxExclusive) {
                this.maxExclusive = maxExclusive;
            }
    
            @Override
            public boolean tryAdvance(Consumer<? super Tuple2<Integer, Integer>> action) {
                if (j >= maxExclusive) {
                    return false;
                }
                action.accept(Tuple.of(i, j));
                j += 1;
                if (j >= maxExclusive) {
                    i += 1;
                    j = i + 1;
                }
                return true;
            }
    
            @Override
            public Spliterator<Tuple2<Integer, Integer>> trySplit() {
                return null;
            }
    
            @Override
            public long estimateSize() {
                return (long) maxExclusive * (maxExclusive - 1) / 2;
            }
    
            @Override
            public int characteristics() {
                return Spliterator.DISTINCT | Spliterator.ORDERED | Spliterator.SIZED | Spliterator.NONNULL | Spliterator.IMMUTABLE;
            }
        }
  2. jlink commented on Oct 27, 2023

    @jlink
    Collaborator

    @FeldrinH Many thanks for diving into the problem and the suggestion. Since I'm on holidays I don't have the time to try it out. Maybe you want to go for a PR with the suggested change? I'd personally like a test targeting PairSplitterator to make sure it works as expected.

  3. FeldrinH commented on Oct 27, 2023

    @FeldrinH
    Author

    I'll try to look into it. I've already patched jqwik locally and know that the fixes work (or at least don't visibly break the system).

  4. jlink commented on Nov 15, 2023

    @jlink
    Collaborator

    I implemented your suggestion and together with the fix for stream concatenation suggested in #526 it looks like it is working - and working considerably faster.

    @FeldrinH Should I wait for a PR from you or just use it and refer to this issue?

  5. added a commit that references this issue on Nov 15, 2023
  6. jlink commented on Nov 15, 2023

    @jlink
    Collaborator

    @FeldrinH Couldn't wait :-) Will give kudos in release notes.

    Fixed in c6a2d5b

  7. jlink commented on Nov 15, 2023

    @jlink
    Collaborator

    Available in "1.8.2-SNAPSHOT".

  8. jlink commented on Nov 15, 2023

    @jlink
    Collaborator
  9. FeldrinH commented on Nov 15, 2023

    @FeldrinH
    Author

    Sorry about the delayed reply. I've been busy.
    I have found no issues with the implementation posted here, so the PR would have been just that with a test.
    Thanks for accepting this fix. Now I can get rid of the custom patches I've been using.

  10. jlink commented on Nov 23, 2023

    @jlink
    Collaborator

    Released in 1.8.2

  11. FeldrinH commented on Dec 28, 2023

    @FeldrinH
    Author

    I just noticed that the release notes have a small mistake. When noting that this issue was fixed, the release notes link to issue 527, but this is issue 525.

  12. jlink commented on Dec 29, 2023

    @jlink
    Collaborator

    thanks for the catch. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions