The optimization

This diagram illustrates the fundamental purpose and stages of optimization.

Basic Purpose of Optimization:

Optimization

  • Core Principle: Perform only necessary actions
  • Code Level: Remove unnecessary elements

Two Goals of Optimization:

1. More Speed

  • O(n): Algorithm (Logic) improvement
  • Techniques: Caching/Parallelization/Recursion optimization

2. Less Resource

  • Memory: Reduce memory usage
  • Management: Dynamic & Static memory optimization

Optimization Implementation Stages:

Stage 1: SW Level (Software Level)

  • Code-level optimization

Stage 2: HW Implementation (Hardware Implementation)

  • Offload heavy workloads to hardware
  • Applied when software optimization is insufficient

Optimization Process:

InputProcessingOutputVerification

  1. Deterministic INPUT Data: Structured input (DB Schema)
  2. Rule-based: Apply rule-based optimization
  3. Deterministic OUTPUT: Predictable results
  4. Verification: Validate speed, resource usage through benchmarking and profiling

Summary:

Optimization aims to increase speed and reduce resources by removing unnecessary operations. It follows a staged approach starting from software-level improvements and extending to hardware implementation when needed. The process ensures predictable, verifiable results through deterministic inputs/outputs and rule-based methods.

#Optimization #PerformanceTuning #CodeOptimization #AlgorithmImprovement #SoftwareEngineering #HardwareAcceleration #ResourceManagement #SpeedOptimization #MemoryOptimization #SystemDesign #Benchmarking #Profiling #EfficientCode #ComputerScience #SoftwareDevelopment

With Claude

OOM (Out-of-Memory) Works

OOM (Out-of-Memory) Mechanism Explained

This diagram illustrates how the Linux OOM (Out-of-Memory) Killer operates when the system runs out of memory.

Main Process Flow (Left Side)

  1. Request
    • An application requests memory from the system
  2. VM Commit (Reserve)
    • The system reserves virtual memory
    • Overcommit policy allows reservation beyond physical capacity
  3. First Use (HW mapping) → Page Fault
    • Hardware mapping occurs when memory is actually accessed
    • Triggers a page fault for physical allocation
  4. Reclaim/Compaction
    • System attempts to free memory through cache, SLAB, writeback, and compaction
    • Can be throttled via cgroup memory.high settings
  5. Swap (if enabled)
    • Uses swap space if available and enabled
  6. OOM Killer
    • As a last resort, terminates processes to free memory

Detailed Decision Points (Center & Right Columns)

Memory Request

  • App asks for memory
  • Controlled via brk/sbrk, mmap/munmap, mremap, and prlimit(RLIMIT_AS)

Virtual Address Allocation

  • Overcommit policy allows reservation beyond physical limits
  • Uses mmap (e.g., MAP_PRIVATE) with madvise(MADV_WILLNEED) hints

Physical Memory Allocation

  • Checks if zone watermarks are OK
  • If yes, maps a physical page; if no, attempts reclamation
  • Optional: mlock/munlock, mprotect, mincore

Any Other Free Memory Space?

  • Attempts to free memory via cache/SLAB/writeback/compaction
  • May throttle on cgroup memory.high
  • Hints: madvise(MADV_DONTNEED)

Swap Space?

  • Checks if swap space is available to offload anonymous pages
  • System: swapon/swapoff; App: mlock* (to avoid swap)

OOM Killer

  • Sends SIGKILL to selected victim when below watermarks or cgroup memory.max is hit
  • Victim selection based on badness/oom_score_adj
  • Configurable via /proc/<pid>/oom_score_adj and vm.panic_on_oom

Summary

When an app requests memory, Linux first reserves virtual address space (overcommit), then allocates physical memory on first use. If physical memory runs low, the system tries to reclaim pages from caches and swap, but when all else fails, the OOM Killer terminates processes based on their oom_score to free up memory and keep the system running.


#Linux #OOM #MemoryManagement #KernelPanic #SystemAdministration #DevOps #OperatingSystem #Performance #MemoryOptimization #LinuxKernel

With Claude