Minimize object allocations

Avoid creating unnecessary temporary objects or arrays which can impact performance through increased garbage collection and memory pressure. Apply these techniques to optimize memory usage:

copy reviewer prompt

Prompt

Reviewer Prompt

Avoid creating unnecessary temporary objects or arrays which can impact performance through increased garbage collection and memory pressure. Apply these techniques to optimize memory usage:

  1. Direct assignment instead of temporary objects:
    // Inefficient: Creates a temporary array
    INDArray ret = Nd4j.valueArrayOf(new long[] {1, nOut}, gainInit);
    gainParamView.assign(ret);
       
    // Efficient: Direct assignment
    gainParamView.assign(gainInit);
    
  2. Create exact-sized objects: When you need a specific shape or size, create it directly rather than creating a larger object and extracting a subset:
    // Inefficient: Creating larger array then extracting subset
    SDVariable b = SD.zerosLike(uHat).get(SDIndex.all(), SDIndex.all(), SDIndex.all(), 
                                           SDIndex.interval(0, 1), SDIndex.interval(0, 1));
       
    // Efficient: Create array with exact needed dimensions
    SDVariable b = SD.zeros(-1, inputCapsules, capsules, 1, 1);
    
  3. Reuse immutable objects: For thread-safe immutable objects like patterns, make them static class members:
    // Inefficient: New Pattern object per instance
    private final Pattern splitPattern = Pattern.compile("...");
       
    // Efficient: Shared across all instances
    private static final Pattern SPLIT_PATTERN = Pattern.compile("...");
    
  4. Defer object creation until necessary: Use parameterized methods that only create objects when needed:
    // Inefficient: Always creates String objects
    Preconditions.checkArgument(condition, "Labels array size " + labelSize + " does not match " + outputSize);
       
    // Efficient: Only formats if exception is thrown
    Preconditions.checkArgument(condition, "Labels array size %s does not match %s", labelSize, outputSize);
    

Implementing these practices consistently will reduce memory churn, decrease garbage collection pauses, and improve application performance, especially in memory-intensive operations.

Source discussions