Sobes.tech
Junior

What is OOM (Out of Memory)?

sobes.tech AI

Answer from AI

OOM (Out of Memory) — is a runtime error that occurs when an application or system runs out of available RAM and cannot allocate new memory.

In various technologies, it manifests differently:

  • Java Virtual Machine (JVM): Throws an OutOfMemoryError exception. Causes can vary, for example:
    • Small heap size (java.lang.OutOfMemoryError: Java heap space).
    • Thread or class issues (java.lang.OutOfMemoryError: unable to create new native thread, java.lang.OutOfMemoryError: Metaspace).
    • Memory leaks.
  • Operating systems: The kernel runs the OOM Killer, which forcibly terminates processes with high resource consumption when memory is scarce, trying to free memory to continue system operation. The victim is chosen based on heuristics (e.g., memory consumption, process priority).

Causes of OOM:

  • Insufficient allocated memory: The application or system simply lacks physical or configured memory.
  • Memory leaks: The program allocates memory but does not free it after it is no longer needed, leading to gradual depletion of available memory.
  • Inefficient memory usage: Algorithms or data structures use too much memory for the task.
  • Sudden memory consumption spike: For example, processing a very large dataset.

Various tools and approaches are used for diagnosing and preventing OOM:

  • Monitoring: Monitoring systems (Prometheus, Grafana) allow tracking memory usage by processes and the system as a whole.
  • Profilers: Tools (JProfiler, VisualVM for Java) help analyze application memory consumption and find leaks.
  • Memory parameter tuning: Increasing memory limits for processes or systems (e.g., -Xmx for JVM, cgroup limits in Kubernetes).
  • Code optimization: Reviewing algorithms and data structures for more efficient memory use.
  • Memory dump analysis: Investigating the application's memory state at the time of OOM to identify causes.

Example of setting a memory limit in Kubernetes for a Pod:

# Resource Limits definition for a container in a Pod
resources:
  limits:
    memory: "256Mi" # Maximum memory consumption
  requests:
    memory: "128Mi" # Guaranteed memory consumption