> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Memory Leaks and OOM (Out of Memory) Errors

> You can detect memory leaks and Out of Memory errors in Apinizer, optimize JVM heap settings, manage cache configuration, and adjust connection pool settings.

## Problem Symptoms

<Warning>
  Memory leaks and OOM errors usually manifest themselves with the following symptoms:
</Warning>

* Pods continuously restarting
* `OutOfMemoryError` log records
* Slowly increasing memory usage
* Decrease in system performance
* Pods transitioning to `OOMKilled` state in Kubernetes

## Problem Causes

Memory leaks and OOM errors can usually be caused by the following factors:

* **Insufficient Heap Settings**: JVM heap size may be insufficient
* **Memory Leaks**: Unused objects not being cleaned by garbage collection
* **Large Data Processing**: Processing large message bodies
* **Cache Sizes**: Cache growing unlimited
* **Connection Pool Issues**: Connections not being closed
* **Thread Pool Issues**: Threads not being properly cleaned

## Detection Methods

### 1. Log Analysis

Search for OOM errors in log files:

```bash theme={null}
kubectl logs <pod-name> | grep -i "OutOfMemoryError"
kubectl logs <pod-name> | grep -i "java.lang.OutOfMemoryError"
```

### 2. Monitoring Memory Metrics

Monitor memory usage with Prometheus metrics:

* `jvm_memory_used_bytes`
* `jvm_memory_max_bytes`
* `container_memory_usage_bytes`

### 3. Heap Dump Analysis

<Steps>
  <Step title="Take Heap Dump">
    Detect memory leaks by taking heap dump:

    ```bash theme={null}
    # Taking heap dump
    kubectl exec <pod-name> -- jmap -dump:format=b,file=/tmp/heapdump.hprof <pid>

    # Copying heap dump from pod
    kubectl cp <namespace>/<pod-name>:/tmp/heapdump.hprof ./heapdump.hprof
    ```
  </Step>
</Steps>

## Solution Recommendations

### 1. Optimizing JVM Heap Settings

<Info>
  Set JVM parameters in Pod deployment:
</Info>

```yaml theme={null}
env:
  - name: JAVA_OPTS
    value: "-Xms2g -Xmx4g -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
```

<Note>
  **Recommendations:**

  * Apinizer's **automatic memory profile** selects the appropriate GC and heap percentage based on container memory (Serial GC at ≤768 MB, G1GC at 769-1536 MB, ZGC at >1536 MB)
  * For containers with more than 1536 MB, ZGC is auto-selected with 70% heap — providing sub-millisecond GC pauses
  * If you prefer manual configuration, use G1GC with MaxGCPauseMillis for predictable pause times
  * See [JVM Garbage Collector Tuning](/en/operations/administrator-guides/jvm-garbage-collector-tuning) for detailed GC configuration options
</Note>

### 2. Increasing Memory Limits

Increase memory limits in Kubernetes deployment:

```yaml theme={null}
resources:
  limits:
    memory: "4Gi"
  requests:
    memory: "2Gi"
```

### 3. Optimizing Cache Configuration

Set cache TTL and size limits:

* Optimize cache TTL values
* Determine cache size limits
* Clean unnecessary cache entries

### 4. Optimizing Connection Pool Settings

Check connection pool settings:

* Limit maximum connection count
* Set connection timeout values
* Regularly clean idle connections

### 5. Thread Pool Management

Optimize thread pool settings:

* Limit maximum thread count
* Set thread timeout values
* Ensure threads are properly cleaned

### 6. Optimizing Large Message Processing

When processing large message bodies:

* Use streaming
* Determine message size limits
* Avoid unnecessary data copying

## Preventive Measures

### 1. Regular Monitoring

* Regularly monitor memory usage
* Get early warning by setting up alerts
* Detect problems in advance by performing trend analysis

### 2. Load Testing

* Perform regular load tests
* Detect memory leaks early
* Perform capacity planning

### 3. Code Review

* Review code that may cause memory leaks
* Check resource management
* Follow best practices

## Related Resources

* [JVM Tuning](/en/operations/administrator-guides/pod-thread-count-periodic-monitoring)
* [Performance Tuning](/en/operations/administrator-guides/pod-thread-count-periodic-monitoring)
* [Log Analysis](/en/operations/log-analysis-guide/kibana-log-search)
