<!--
title: Minimize object allocations
domain: ml-systems
topic: Performance Optimization
language: Java
source: deeplearning4j/deeplearning4j
updated: 2020-02-17
url: https://awesomereviewers.com/reviewers/deeplearning4j-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:

1. **Direct assignment instead of temporary objects**:
   ```java
   // 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:
   ```java
   // 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:
   ```java
   // 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:
   ```java
   // 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.
