<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dima853</title>
    <description>The latest articles on DEV Community by dima853 (@dima853).</description>
    <link>https://dev.to/dima853</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2445850%2F4390e480-a0f0-4421-abfd-bac4b5862234.jpeg</url>
      <title>DEV Community: dima853</title>
      <link>https://dev.to/dima853</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dima853"/>
    <language>en</language>
    <item>
      <title>Free Block Search Algorithms in Memory Managers</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:48:47 +0000</pubDate>
      <link>https://dev.to/dima853/free-block-search-algorithms-in-memory-managers-1b90</link>
      <guid>https://dev.to/dima853/free-block-search-algorithms-in-memory-managers-1b90</guid>
      <description>&lt;h2&gt;
  
  
  FUNDAMENTAL PROBLEM STATEMENT
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The task of allocating a memory block of size &lt;code&gt;n&lt;/code&gt; from a pool of free blocks reduces to finding a block &lt;code&gt;B&lt;/code&gt; in the set of free blocks &lt;code&gt;F&lt;/code&gt; such that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;size(B) ≥ n&lt;/code&gt; (sufficiency)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;B&lt;/code&gt; minimizes some cost function &lt;code&gt;C(B, n)&lt;/code&gt; (optimality)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The set of free blocks&lt;/strong&gt; is represented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F = {B_i = (addr_i, size_i) | B_i is free, 0 ≤ i &amp;lt; m}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;addr_i&lt;/code&gt; is the starting address, &lt;code&gt;size_i&lt;/code&gt; is the block size.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;But, first let's understand what stack and heap are.&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. Conceptual Division: Manager vs. Location
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Memory is physically stored in RAM&lt;/strong&gt; and the processor accesses it by addresses. The terms &lt;strong&gt;"stack"&lt;/strong&gt; and &lt;strong&gt;"heap"&lt;/strong&gt; are not physical areas on chips, but &lt;strong&gt;logical models for organizing memory access&lt;/strong&gt; within the process's virtual address space.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stack&lt;/strong&gt; — is a &lt;strong&gt;LIFO (Last In, First Out)&lt;/strong&gt; model, closely tied to the execution flow (&lt;code&gt;thread&lt;/code&gt;). Its main task is to store function call context: arguments, local variables, return addresses. Its structure is predictable and managed by &lt;strong&gt;the compiler and hardware&lt;/strong&gt; (stack pointer register — &lt;code&gt;SP/ESP/RSP&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heap&lt;/strong&gt; — is a &lt;strong&gt;random access&lt;/strong&gt; model. Its task is to provide memory blocks whose lifetime is not tied to the nesting of function calls. It is managed by &lt;strong&gt;the programmer&lt;/strong&gt; (via &lt;code&gt;malloc/free&lt;/code&gt;, &lt;code&gt;new/delete&lt;/code&gt;) or the &lt;strong&gt;runtime environment&lt;/strong&gt; (garbage collector).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Why does the allocator (memory manager) operate with the concept of "heap"?
&lt;/h3&gt;

&lt;p&gt;Because &lt;strong&gt;"heap" is a pool of unstructured, free-for-allocation memory within the process's address space.&lt;/strong&gt; The allocator is the &lt;strong&gt;manager of this pool&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The algorithms we will analyze later (First-Fit, Buddy System) are precisely strategies for managing this pool: finding a free chunk, splitting it, merging freed chunks (coalescence), fighting fragmentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The stack is managed according to fundamentally different rules that do not require such a "manager":&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Allocation:&lt;/strong&gt; When entering a function, the compiler calculates the total size of all local variables and simply &lt;strong&gt;moves the stack pointer (SP) down by the required number of bytes&lt;/strong&gt;. This is one assembly instruction (&lt;code&gt;SUB&lt;/code&gt;). No free block search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deallocation:&lt;/strong&gt; When exiting the function, the stack pointer &lt;strong&gt;is moved back up&lt;/strong&gt; (&lt;code&gt;ADD&lt;/code&gt; or simply loading the saved value from the frame). No merging, no list of free blocks is maintained.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Fragmentation" of the stack&lt;/strong&gt; — is simply a "hole" between the SP and the end of the stack, which instantly disappears as soon as the SP moves up when returning from functions. This is not fragmentation in the classical sense, but simply an &lt;strong&gt;unused&lt;/strong&gt; area at the moment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The stack does not have the problem of finding a free block. It has only one "free" area — the one above the current SP.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Important Clarification: "Owns" — in what sense?
&lt;/h3&gt;

&lt;p&gt;Here is a key nuance. When we say "the allocator owns the heap," it does not mean that the OS or the process has "given" it to it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;OS Level:&lt;/strong&gt; When a process starts, the OS allocates a virtual address space for it. Part of this space is reserved for the stack(s) of each thread. Another large contiguous area (often growing) is allocated for the &lt;strong&gt;"process heap"&lt;/strong&gt; (for example, the area from which &lt;code&gt;sbrk&lt;/code&gt; or &lt;code&gt;mmap&lt;/code&gt; can allocate memory).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Runtime Level (allocator):&lt;/strong&gt; The standard library of the language (e.g., glibc's &lt;code&gt;malloc&lt;/code&gt;) &lt;strong&gt;"digs under" the OS&lt;/strong&gt;. On the first call to &lt;code&gt;malloc()&lt;/code&gt;, it requests a large block of memory from the OS (e.g., 128KB via &lt;code&gt;sbrk&lt;/code&gt; or &lt;code&gt;mmap&lt;/code&gt;). This block becomes its &lt;strong&gt;primary pool, its "heap" in the context of the manager&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Program Level:&lt;/strong&gt; When you call &lt;code&gt;malloc(100)&lt;/code&gt;, you are not accessing the OS, but this manager. It looks for free 100 bytes within its pool using those very algorithms (First-Fit, Segregated Lists). If there is no space in its pool, it goes to the OS for &lt;strong&gt;another large block&lt;/strong&gt; to expand its pool.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Thus, the "heap" at the allocator level is its internal memory pool, which it organizes itself.&lt;/strong&gt; The OS simply provides it with raw pages of virtual memory upon request.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. CLASSIFICATION OF SEARCH STRATEGIES
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 First-Fit (First Suitable)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Essence:&lt;/strong&gt; Free blocks are organized in a list (most often doubly linked). Metadata (pointers &lt;code&gt;next/prev&lt;/code&gt;) is stored in the body of the free block. During the search, the allocator traverses the list from the head until the first block whose size &lt;code&gt;size &amp;gt;= N&lt;/code&gt;. This block is removed from the free list. If &lt;code&gt;size&lt;/code&gt; is significantly larger than &lt;code&gt;N&lt;/code&gt;, &lt;strong&gt;splitting&lt;/strong&gt; is performed: an allocated block of size &lt;code&gt;N&lt;/code&gt; is created, and the remainder (a new free block) is inserted back into the list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fuploads%2F20250430165625886123%2F6_096mdsbhfjdsnj.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fuploads%2F20250430165625886123%2F6_096mdsbhfjdsnj.webp" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Pseudocode&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;FIRST_FIT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;≥&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;NULL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;But, let's recall what a process and a thread are&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Process and thread are different abstractions of the OS kernel for code execution.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Process&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Technically — it is a &lt;strong&gt;resource container&lt;/strong&gt;. Each process has:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Its own virtual address space&lt;/strong&gt; (isolated page table).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;A table of open files and descriptors&lt;/strong&gt; (files, sockets, pipes).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Credentials and access rights&lt;/strong&gt; (UID, GID, capabilities).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Signals and their handlers&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;At least one execution thread&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The key point:&lt;/strong&gt; Processes are isolated. The memory of one process is &lt;em&gt;by default&lt;/em&gt; invisible to another (for exchange, IPC mechanisms are needed).&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Moreover, if there were no hardware virtual memory, process isolation in its modern understanding would be impossible. That is, process isolation is fundamentally and hardware-guaranteed by virtual memory.&lt;/strong&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is virtual memory?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imagine&lt;/strong&gt; that physical memory (RAM) is a huge warehouse with millions of identical cells (bytes).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without virtual memory&lt;/strong&gt;, all programs receive a single map of this warehouse with real cell numbers. If one program says "put a value in cell &lt;strong&gt;№12345&lt;/strong&gt;", it will put it in the real cell &lt;strong&gt;№12345&lt;/strong&gt;. Another program, reading "cell &lt;strong&gt;№12345&lt;/strong&gt;", will get this value. &lt;strong&gt;No isolation.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Virtual memory&lt;/strong&gt; is a fundamental abstraction that makes each process &lt;em&gt;think&lt;/em&gt; that it alone owns all the computer's physical memory, although in reality the memory is shared among many processes and the OS.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Address abstraction:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The process works with &lt;strong&gt;virtual addresses&lt;/strong&gt; (for example, from &lt;code&gt;0x00000000&lt;/code&gt; to &lt;code&gt;0xFFFFFFFF&lt;/code&gt; in a 32-bit system).&lt;/li&gt;
&lt;li&gt;The hardware (CPU + MMU — Memory Management Unit) translates these virtual addresses into &lt;strong&gt;physical addresses&lt;/strong&gt; of real RAM modules.&lt;/li&gt;
&lt;li&gt;Each process has its own &lt;strong&gt;complete and independent&lt;/strong&gt; virtual address area. The virtual address &lt;code&gt;0x1000&lt;/code&gt; in Process A points to a different physical cell than the same address &lt;code&gt;0x1000&lt;/code&gt; in Process B.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Page Table:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Address translation occurs through hierarchical tables (Page Tables), unique for each process.&lt;/li&gt;
&lt;li&gt;An entry in the table (Page Table Entry, PTE) contains the physical address of the page and control bits (present in RAM, readable/writable, etc.).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Paging:&lt;/strong&gt;&lt;br&gt;
_ Memory is divided into fixed blocks — &lt;strong&gt;pages&lt;/strong&gt; (usually 4 KiB). - KiB is a "kibibyte". 1 KiB = 1024 bytes.&lt;br&gt;
&lt;strong&gt;This is an important clarification because in the context of memory and OS, powers of two are always used, not powers of ten.&lt;/strong&gt; - Virtual address space is a linear sequence of virtual pages. Physical memory is a set of physical page frames.&lt;br&gt;
_ The page table specifies the mapping: &lt;strong&gt;Virtual Page N → Physical Frame M&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Swapping/Paging Mechanism:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If physical memory is insufficient, the OS kernel can &lt;strong&gt;swap out&lt;/strong&gt; rarely used pages to disk (to a special area of a partition or a swap file).&lt;/li&gt;
&lt;li&gt;The PTE of such a page is marked "not present in memory". An attempt to access it causes an exception — &lt;strong&gt;page fault&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The page fault handler in the kernel finds the required page on disk, loads it into a free physical frame (possibly swapping out another page), updates the PTE, and resumes the process execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Thread&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Technically — it is a &lt;strong&gt;unit of scheduling on the CPU (CPU scheduling entity)&lt;/strong&gt;. Each thread has:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Its own register context&lt;/strong&gt; (CPU register values, including stack pointer SP and instruction pointer IP).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Its own call stack&lt;/strong&gt; (but within the process's address space).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Priority and scheduler state&lt;/strong&gt; (running, waiting, ready).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The key point:&lt;/strong&gt; Threads of the same process &lt;strong&gt;share the entire address space and resources&lt;/strong&gt; from the list above. They share the heap, files, global variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Analogy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine a computer with virtualization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Process&lt;/strong&gt; is a &lt;strong&gt;virtual machine (VM)&lt;/strong&gt;. It has its own virtual memory, its own virtual disks, its own network interfaces.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread&lt;/strong&gt; is a &lt;strong&gt;virtual CPU core (vCPU)&lt;/strong&gt; inside this VM. Several vCPUs work inside one VM, sharing all its resources.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2.2 Best-Fit (Best Suitable)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;BEST_FIT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NULL&lt;/span&gt;
    &lt;span class="n"&gt;min_waste&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;∞&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;≥&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;waste&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;waste&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_waste&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;min_waste&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;waste&lt;/span&gt;
                &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Worst-case theorem&lt;/strong&gt;: Best-Fit minimizes external fragmentation in a static scenario, but:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search complexity: O(m)&lt;/li&gt;
&lt;li&gt;Creates the maximum number of residual blocks of minimal size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.3 Worst-Fit (Worst Suitable)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;WORST_FIT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;worst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NULL&lt;/span&gt;
    &lt;span class="n"&gt;max_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;≥&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;max_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;max_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;worst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;worst&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Invariant&lt;/strong&gt;: Aims to leave large free blocks, which theoretically reduces the probability of allocation failure for large requests&lt;/p&gt;

&lt;h2&gt;
  
  
  Chunks/Arenas
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Chunk — atomic unit of management&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A chunk is not just a "piece of memory". It is a container with metadata, a unit of allocation and deallocation.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Chunk structure (using dlmalloc/ptmalloc as an example):&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----------------+ &amp;lt;-- Pointer returned to user (mem)
| size (with     |
| flags)         |   &amp;lt;-- Header (metadata). Hidden from user.
+----------------+ &amp;lt;-- Pointer managed by allocator (chunk)
| User data      |
| (user memory)  |
| ...            |
+----------------+
| size (with     |
| flags)         |   &amp;lt;-- Footer (only for free chunks)
+----------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Footer&lt;/strong&gt; in the context of a memory manager is an additional metadata block placed at the end of a chunk (memory block).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Size&lt;/strong&gt; is stored in the header &lt;strong&gt;together with flags&lt;/strong&gt; (for example, bit &lt;code&gt;P&lt;/code&gt; — previous chunk is free, bit &lt;code&gt;M&lt;/code&gt; — allocated via &lt;code&gt;mmap&lt;/code&gt;, bit &lt;code&gt;A&lt;/code&gt; — belongs to non-main arena).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alignment:&lt;/strong&gt; The chunk address is aligned (usually to 8/16 bytes). The user pointer (&lt;code&gt;mem&lt;/code&gt;) is offset relative to it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boundary tags:&lt;/strong&gt; Size is stored at &lt;strong&gt;the beginning and end&lt;/strong&gt; of the chunk. Why at the end? So that during deallocation, having a pointer to the previous chunk, its size can be determined and checked if it's free — for &lt;strong&gt;coalescence&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In a free chunk&lt;/strong&gt;, the "user data" field is used to store &lt;code&gt;fd&lt;/code&gt; (forward) and &lt;code&gt;bk&lt;/code&gt; (backward) pointers in a doubly linked list of free chunks of the corresponding size (bin).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Arena — memory domain for parallelism&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;An arena is an isolated memory pool (a set of chunks) managed by its own set of data structures (binned heaps, lists).&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The problem that arenas solve:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In traditional &lt;code&gt;malloc&lt;/code&gt; with a single global heap &lt;strong&gt;all threads synchronize on one lock&lt;/strong&gt;. Thread 1 allocates memory → acquires the global mutex → threads 2, 3, ... wait → contention, performance degradation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Split the heap into independent &lt;strong&gt;arenas&lt;/strong&gt;. Threads working with different arenas do not interfere with each other.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Arena architecture (as in glibc's ptmalloc):&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------------------+      +---------------------+
|   Main Arena        |      |   Arena 1           |
|  (Main Arena)       |      |  (Non-Main Arena)   |
|                     |      |                     |
|  +--------------+   |      |  +--------------+   |
|  | Heap         |   |      |  | Heap         |   |
|  | (sbrk/mmap)  |   |      |  |   (only      |   |
|  +--------------+   |      |  |    mmap)     |   |
|  | Binlists     |   |      |  +--------------+   |
|  | (bins)       |   |      |  | Binlists     |   |
|  +--------------+   |      |  | (bins)       |   |
|  | Mutex        |   |      |  +--------------+   |
|  | (lock)       |   |      |  | Mutex        |   |
|  +--------------+   |      |  | (lock)       |   |
+---------------------+      +---------------------+
         ^                              ^
         | (thread 1)                   | (thread 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Main Arena:&lt;/strong&gt; The only one that uses &lt;code&gt;sbrk()&lt;/code&gt; to expand the heap. Always exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Main Arenas:&lt;/strong&gt; Created as needed. Each is an &lt;strong&gt;independent memory region obtained via &lt;code&gt;mmap()&lt;/code&gt;&lt;/strong&gt; (usually 1MB or more).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Each arena has:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Its own &lt;strong&gt;heap&lt;/strong&gt; (one or several &lt;code&gt;mmap&lt;/code&gt; regions).&lt;/li&gt;
&lt;li&gt;Its own &lt;strong&gt;binlists&lt;/strong&gt; (arrays of free chunk lists sorted by size: fast bins, small bins, large bins, unsorted bin).&lt;/li&gt;
&lt;li&gt;Its own &lt;strong&gt;mutex&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Strategy for distributing threads across arenas:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; On the first memory allocation in a thread, &lt;code&gt;malloc()&lt;/code&gt; tries to find a free (unlocked) arena and bind the thread to it.&lt;/li&gt;
&lt;li&gt; If there are no free arenas — a new one is created.&lt;/li&gt;
&lt;li&gt; Subsequently, the thread &lt;strong&gt;when possible&lt;/strong&gt; uses "its" arena, minimizing contention.&lt;/li&gt;
&lt;li&gt; But if there is no free memory of the required size in "its" arena, the thread can &lt;strong&gt;steal&lt;/strong&gt; memory from another arena (through global synchronization).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;This is a trade-off:&lt;/strong&gt; Complete isolation is impossible, as it would lead to inefficient memory use (one arena is full, another is empty). Therefore, there is periodic &lt;strong&gt;rebalancing&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;there may be errors in the text*&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>selfuniversity</category>
    </item>
    <item>
      <title>What is a DAG? Arborescence (Part 2)</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:48:16 +0000</pubDate>
      <link>https://dev.to/dima853/what-is-a-dag-arborescence-part-2-180o</link>
      <guid>https://dev.to/dima853/what-is-a-dag-arborescence-part-2-180o</guid>
      <description>&lt;h3&gt;
  
  
  Part 1: Arborescence (General Definition)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;there may be inaccuracies or simplifications in this article&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.1. Intuitive Representation:&lt;/strong&gt;&lt;br&gt;
Imagine an ordinary undirected tree. Now assign a special role to one of its vertices — the &lt;strong&gt;root&lt;/strong&gt;. Then orient all edges so that they point &lt;strong&gt;outward from the root&lt;/strong&gt;, i.e., in the direction from the root to the leaves. The resulting structure is an arborescence.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Simple Example - Computer Folder Structure ⬇️&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Root — &lt;code&gt;C:\&lt;/code&gt; (or &lt;code&gt;/&lt;/code&gt; on Linux)&lt;/li&gt;
&lt;li&gt;All folders are created via "New Folder"; there are no shortcuts or links.&lt;/li&gt;
&lt;li&gt;Each folder has &lt;strong&gt;exactly one "parent"&lt;/strong&gt; (the folder it resides in).&lt;/li&gt;
&lt;li&gt;You can only go up to the parent. This is a classic &lt;strong&gt;tree (arborescence)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  DAG with Potential Arborescence (real system with &lt;code&gt;mklink&lt;/code&gt; or &lt;code&gt;.lnk&lt;/code&gt;):
&lt;/h3&gt;

&lt;p&gt;Let's say there is a structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Projects\
├── SecretData\
└── CurrentProject\   &amp;lt;-- contains shortcut "Data" → C:\Projects\SecretData
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this is a &lt;strong&gt;DAG&lt;/strong&gt;, but &lt;strong&gt;not an arborescence&lt;/strong&gt;, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;SecretData&lt;/code&gt; has &lt;strong&gt;two "parents"&lt;/strong&gt;:

&lt;ol&gt;
&lt;li&gt; Direct: &lt;code&gt;C:\Projects\&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Via the shortcut: &lt;code&gt;C:\Projects\CurrentProject\Data\&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;The condition &lt;code&gt;indegree = 1&lt;/code&gt; for an arborescence is violated.&lt;/li&gt;
&lt;li&gt;But there are no cycles — it is a DAG.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  "Arborescence(DAG)" in this analogy:
&lt;/h3&gt;

&lt;p&gt;If the administrator strictly prohibits creating shortcuts, links, and mount points, then the file system &lt;strong&gt;is both a DAG (no cycles) and an Arborescence (a tree)&lt;/strong&gt;. This is precisely that simple case of a "simple folder tree".&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1.2. Formal Definition:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;explanation of notations at the end&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let &lt;code&gt;G = (V, E)&lt;/code&gt; be a &lt;strong&gt;directed graph&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;G&lt;/code&gt; is called an &lt;strong&gt;arborescence (oriented tree) with root r ∈ V&lt;/strong&gt; if the following conditions are met:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Existence of a root:&lt;/strong&gt; There exists exactly one vertex &lt;code&gt;r&lt;/code&gt; into which &lt;strong&gt;no edge enters&lt;/strong&gt; (i.e., its &lt;strong&gt;indegree&lt;/strong&gt; &lt;code&gt;deg⁻(r) = 0&lt;/code&gt;). This vertex is called the root.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Uniqueness of the path from the root:&lt;/strong&gt; For any other vertex &lt;code&gt;v ∈ V \ {r}&lt;/code&gt;, there exists &lt;strong&gt;exactly one&lt;/strong&gt; directed path from &lt;code&gt;r&lt;/code&gt; to &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Absence of cycles:&lt;/strong&gt; The graph &lt;code&gt;G&lt;/code&gt; contains no directed cycles.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.3. Key Corollaries and Properties:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Important and equivalent characteristics follow from the definition:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Structure of incoming edges:&lt;/strong&gt; For each vertex v ≠ r, the &lt;strong&gt;indegree is 1&lt;/strong&gt; (deg⁻(v) = 1). For the root r, it is 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The root is also a vertex.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In graph theory, the terms "vertex" and "node" mean the same thing. The root is simply a special, designated vertex.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The indegree of the root is zero.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
This means that no edge leads into the root — there is no arrow pointing to it. It is the starting point of all paths in the graph.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;All other vertices have an indegree of one.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Every vertex except the root has exactly one incoming edge — one arrow coming into it from another vertex.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;This is a direct consequence of the definition of an arborescence.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Precisely because the graph is an arborescence (directed tree), these rules hold: one root with no incoming edges, and every other vertex has exactly one "parent".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thus, the structure of incoming edges in an arborescence is strictly defined:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;0 → for the root, 1 → for all other vertices.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Connectivity (in the directed sense):&lt;/strong&gt; The graph is &lt;strong&gt;weakly connected&lt;/strong&gt; (if edge directions are ignored, it is connected) and &lt;strong&gt;reachable from the root&lt;/strong&gt; (any vertex v can be reached from r).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Number of edges:&lt;/strong&gt; If an arborescence has n vertices, then it has exactly n-1 edges.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orientation from ancestor to descendant:&lt;/strong&gt; For any edge (u, v), vertex u is an &lt;strong&gt;ancestor&lt;/strong&gt; of vertex v, and v is a &lt;strong&gt;descendant&lt;/strong&gt; of u. The root is an ancestor of all vertices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Equivalence to a tree:&lt;/strong&gt; If directed edges are replaced with undirected ones, the result is an ordinary connected acyclic undirected tree.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Part 2: Arborescence in a Directed Acyclic Graph (DAG)
&lt;/h3&gt;

&lt;p&gt;Now consider a situation where an arborescence is a &lt;strong&gt;subgraph&lt;/strong&gt; of a larger &lt;strong&gt;Directed Acyclic Graph (DAG)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2.1. Context and Motivation:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A DAG is a directed graph without cycles. Examples: task dependency (makefile), course order in a curriculum, partial ordering of events. In such a graph, a common task is to find a &lt;strong&gt;structure linking all or many vertices in the form of a tree&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2.2. Definition of Arborescence in a DAG:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let G = (V, E) be a DAG. An &lt;strong&gt;arborescence in a DAG&lt;/strong&gt; (often called a &lt;em&gt;directed spanning tree&lt;/em&gt;) is a subgraph T = (V, E_T), where E_T ⊆ E, which itself is an arborescence (in the sense of Part 1) with some root r ∈ V.&lt;br&gt;
&lt;strong&gt;Spanning&lt;/strong&gt; is the key &lt;strong&gt;word&lt;/strong&gt;, meaning &lt;strong&gt;"covering all vertices"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key point:&lt;/strong&gt; All edges of the arborescence T are taken &lt;strong&gt;from the original DAG G&lt;/strong&gt; and retain their orientation. The arborescence "fits" into the &lt;em&gt;topological order&lt;/em&gt; of the DAG.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Topological order (or topological sorting)&lt;/em&gt; is a way to linearly order the vertices of a directed graph such that for any edge from vertex A to vertex B, vertex A always comes before vertex B in the list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Main condition:&lt;/strong&gt; Works only in graphs without cycles (DAG). If it's possible to return from the end to the beginning, the order cannot be constructed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Essence:&lt;/strong&gt; If there is a path from A to B, then in the list A will always be to the left of B. This is a "timeline" or task execution queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reachability vs. Order:&lt;/strong&gt; If A→B, then A comes before B (always).
If A comes before B in the list, this does not mean there is a path between them (they might simply be independent parallel tasks).
&lt;strong&gt;Remember:&lt;/strong&gt; Topological order is a to-do list where you will never encounter a situation where to perform the current step you need to do something that comes later in the list. (There are no cycles).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;2.3. Existence and Construction:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In an arbitrary DAG, an arborescence covering &lt;strong&gt;all&lt;/strong&gt; vertices (i.e., a &lt;em&gt;spanning&lt;/em&gt; one) does &lt;strong&gt;not always&lt;/strong&gt; exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Necessary conditions for the existence of a spanning arborescence in a DAG:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Existence of a unique root-source:&lt;/strong&gt; The DAG must have &lt;strong&gt;exactly one&lt;/strong&gt; node with zero indegree (a source). This node becomes a candidate for the root r of the arborescence. If there are several such nodes, constructing a single spanning tree is impossible (several disconnected trees will result).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Reachability from the root:&lt;/strong&gt; This unique source r must be reachable &lt;strong&gt;from all other vertices&lt;/strong&gt; of the DAG. This condition does not automatically follow from the first — there can be components isolated from r in the DAG, even if r is the only source in the entire graph.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Important note:&lt;/strong&gt; Even if both conditions are met, &lt;strong&gt;not any&lt;/strong&gt; choice of one incoming edge for each vertex will lead to a connected arborescence. A poor choice may create a forest (several trees) or violate reachability from the root.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Correct construction algorithm (traversal from the root):&lt;/strong&gt;&lt;br&gt;
If the conditions are met, a spanning arborescence can be guaranteed by traversal (BFS/DFS) starting from the root r and selecting &lt;em&gt;parent&lt;/em&gt; edges.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize an empty subgraph T.&lt;/li&gt;
&lt;li&gt;Select the unique source r as the root and add it to T.&lt;/li&gt;
&lt;li&gt;Start a traversal (e.g., BFS) from r along the edges of the original DAG G.&lt;/li&gt;
&lt;li&gt;For each new vertex v reached during traversal via edge (u, v) ∈ E, add this edge (u, v) to T as the sole incoming edge for v in the arborescence.&lt;/li&gt;
&lt;li&gt;After traversal is complete, check that T contains all vertices V. If yes, then T is the desired spanning arborescence. If some vertices were not reached, then despite the conditions being met, the choice of edges during traversal might have led to a dead end; in this case, a more complex algorithm for finding a &lt;em&gt;spanning arborescence&lt;/em&gt; is required.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Alternative approach (parent selection):&lt;/strong&gt;&lt;br&gt;
A simpler-to-implement method that &lt;strong&gt;guarantees&lt;/strong&gt; the construction of an arborescence if it exists:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Perform a topological sort of the DAG G.&lt;/li&gt;
&lt;li&gt; For each vertex v ≠ r (in topological order), choose as its &lt;em&gt;parent&lt;/em&gt; any vertex u for which there exists an edge (u, v) ∈ E. Thanks to the topological order, u will be processed before v, which guarantees the absence of cycles and reachability from the root (provided the root r is the first vertex in the order). All selected edges (u, v) form the desired arborescence T.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;By definition, every arborescence is a DAG (directed acyclic graph).&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  Part 3: Comparison and Summary Table
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Arborescence (general)&lt;/th&gt;
&lt;th&gt;Arborescence as a subgraph of a DAG&lt;/th&gt;
&lt;th&gt;"Arborescence(Dag)" (as a property of a single graph)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Base Graph&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;It is a tree by itself.&lt;/td&gt;
&lt;td&gt;It is a &lt;strong&gt;subgraph&lt;/strong&gt; of a larger DAG.&lt;/td&gt;
&lt;td&gt;The single graph considered possesses two properties.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Root&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;One, with &lt;code&gt;deg⁻ = 0&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;The root is the unique source in the DAG (if it's a spanning tree).&lt;/td&gt;
&lt;td&gt;One, with &lt;code&gt;deg⁻ = 0&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Indegree&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;deg⁻(v) = 1&lt;/code&gt; for all &lt;code&gt;v ≠ r&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;deg⁻_T(v) = 1&lt;/code&gt; in subgraph T, but in the original DAG &lt;code&gt;deg⁻_G(v)&lt;/code&gt; can be &amp;gt;1.&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;deg⁻(v) = 1&lt;/code&gt; for all &lt;code&gt;v ≠ r&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cycles&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No directed cycles (follows from the definition).&lt;/td&gt;
&lt;td&gt;None (in both T and G).&lt;/td&gt;
&lt;td&gt;None (follows from both properties).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Main Idea&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Oriented tree-like hierarchy.&lt;/td&gt;
&lt;td&gt;Extracting a tree-like hierarchy from a dependency graph.&lt;/td&gt;
&lt;td&gt;Emphasizes that the graph is a strict hierarchical tree without cycles.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Example for DAG (constructing an arborescence):
&lt;/h3&gt;

&lt;p&gt;Consider a DAG:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     A (source, deg⁻=0)
    / \
   B   C
    \ / \
     D   E
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is one source &lt;code&gt;A&lt;/code&gt;. Let's try to construct an arborescence (spanning tree):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root: &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;B&lt;/code&gt;: Choose edge &lt;code&gt;A -&amp;gt; B&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;C&lt;/code&gt;: Choose edge &lt;code&gt;A -&amp;gt; C&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;D&lt;/code&gt;: There are two incoming edges (&lt;code&gt;B-&amp;gt;D&lt;/code&gt;, &lt;code&gt;C-&amp;gt;D&lt;/code&gt;). Choose, for example, &lt;code&gt;B-&amp;gt;D&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;E&lt;/code&gt;: There is an edge &lt;code&gt;C-&amp;gt;E&lt;/code&gt;. Choose it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We get the arborescence: &lt;code&gt;A -&amp;gt; B, A -&amp;gt; C, B -&amp;gt; D, C -&amp;gt; E&lt;/code&gt;. It covers all vertices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arborescence&lt;/strong&gt; is a fundamental structure, the directed analogue of a tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arborescence in a DAG&lt;/strong&gt; is a way to select a "main ancestor" for each vertex in a dependency graph, turning it into a hierarchy.&lt;/li&gt;
&lt;li&gt;The statement that a graph is both usually emphasizes its strict tree-like nature and acyclicity, which simplifies analysis (e.g., allowing root traversals and DP on DAGs simultaneously).&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Notations:
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;V&lt;/code&gt;&lt;/strong&gt; (from &lt;strong&gt;V&lt;/strong&gt;ertices) — the &lt;strong&gt;set of vertices&lt;/strong&gt; of a graph.&lt;br&gt;&lt;br&gt;
For example: &lt;code&gt;V = {A, B, C, D}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;E&lt;/code&gt;&lt;/strong&gt; (from &lt;strong&gt;E&lt;/strong&gt;dges) — the &lt;strong&gt;set of edges&lt;/strong&gt; of a graph.&lt;br&gt;&lt;br&gt;
An edge is a pair of vertices.&lt;br&gt;&lt;br&gt;
For an &lt;strong&gt;undirected graph&lt;/strong&gt;: &lt;code&gt;E = {{A,B}, {B,C}}&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
For a &lt;strong&gt;directed graph&lt;/strong&gt; (arcs): &lt;code&gt;E = {(A→B), (B→C), (C→A)}&lt;/code&gt; — where the order of vertices matters.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;r ∈ V&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;r&lt;/code&gt; — denotes the &lt;strong&gt;root&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;∈&lt;/code&gt; — the &lt;strong&gt;membership&lt;/strong&gt; symbol ("belongs to")&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;V&lt;/code&gt; — the set of all &lt;strong&gt;vertices&lt;/strong&gt; of the graph&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complete meaning:&lt;/strong&gt; &lt;code&gt;r&lt;/code&gt; is one of the vertices of the graph&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;code&gt;deg⁻(r) = 0&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;deg⁻(v)&lt;/code&gt; — the &lt;strong&gt;indegree&lt;/strong&gt; of a vertex (number of edges entering the vertex)

&lt;ul&gt;
&lt;li&gt;Often denoted as &lt;code&gt;deg^{-}(v)&lt;/code&gt; or &lt;code&gt;d_in(v)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(r)&lt;/code&gt; — for vertex &lt;code&gt;r&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;= 0&lt;/code&gt; — equals zero&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complete meaning:&lt;/strong&gt; no edge &lt;strong&gt;enters&lt;/strong&gt; the root (there is no arrow pointing to this vertex)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;code&gt;v ∈ V \ {r}&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;v&lt;/code&gt; — an arbitrary vertex&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;∈&lt;/code&gt; — belongs to&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;V \ {r}&lt;/code&gt; — the set of all vertices &lt;strong&gt;except&lt;/strong&gt; the root &lt;code&gt;r&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complete meaning:&lt;/strong&gt; for each vertex that &lt;strong&gt;is not the root&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Directed path from &lt;code&gt;r&lt;/code&gt; to &lt;code&gt;v&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Directed path&lt;/strong&gt; — a sequence of vertices &lt;code&gt;r → ... → v&lt;/code&gt;, where each edge is directed from the previous vertex to the next&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exactly one&lt;/strong&gt; — there exists only one such path; you cannot reach it in different ways&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Directed cycle
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Directed cycle&lt;/strong&gt; — a path that starts and ends at the same vertex, following the direction of edges&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;A → B → C → A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Absence of cycles&lt;/strong&gt; — no such closed routes exist in the graph&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;In simple terms:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There is one special root vertex &lt;code&gt;r&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing leads into it&lt;/strong&gt; (like the source of a river)&lt;/li&gt;
&lt;li&gt;From the root to any other vertex, you can go &lt;strong&gt;in only one way&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You cannot go in circles — only from the root downwards&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  &lt;a href="https://leetcode.com/problems/course-schedule/description/" rel="noopener noreferrer"&gt;Solution for Problem 207. Course Schedule (Leetcode)&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;The meaning of the task is extremely simple:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Check if there are cycles in the list of dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If there are no cycles:&lt;/strong&gt; You can create a study plan and complete all courses → &lt;strong&gt;true&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If there is a cycle:&lt;/strong&gt; (for example, course 0 requires course 1, and course 1 requires course 0), you fall into a "vicious circle" and cannot even start → &lt;strong&gt;false&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Visually:&lt;br&gt;&lt;br&gt;
&lt;code&gt;[[1,0]]&lt;/code&gt; — this is a straight line (0→1). All good.&lt;br&gt;&lt;br&gt;
&lt;code&gt;[[1,0],[0,1]]&lt;/code&gt; — this is a ring (0↔1). Deadlock.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;canFinish&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num_courses&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;prerequisites&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 1. BUILD THE GRAPH STRUCTURE&lt;/span&gt;
        &lt;span class="c1"&gt;// Create an adjacency list where each index represents a course,&lt;/span&gt;
        &lt;span class="c1"&gt;// and the inner list contains courses that depend on it.&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;adj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num_courses&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="n"&gt;adj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;());&lt;/span&gt;

        &lt;span class="c1"&gt;// 2. FILL THE ADJACENCY LIST AND COUNTER "INDEGREE"&lt;/span&gt;
        &lt;span class="c1"&gt;// indegree[i] stores the number of prerequisites required before taking course i.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;indegree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num_courses&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prerequisites&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;course&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// The dependent course&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;    &lt;span class="c1"&gt;// The prerequisite course&lt;/span&gt;
            &lt;span class="n"&gt;adj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pre&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;course&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Directed edge: pre -&amp;gt; course&lt;/span&gt;
            &lt;span class="n"&gt;indegree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;course&lt;/span&gt;&lt;span class="o"&gt;]++;&lt;/span&gt;       &lt;span class="c1"&gt;// Increment the number of incoming edges for the target&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// 3. INITIALIZE STARTING POINTS&lt;/span&gt;
        &lt;span class="c1"&gt;// The queue 'q' will store courses with NO prerequisites (indegree == 0).&lt;/span&gt;
        &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num_courses&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// If a course has no dependencies, it can be taken immediately.&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indegree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// 4. PROCESS THE COURSES (Kahn's Algorithm / BFS)&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;visited_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Track how many courses we successfully completed&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Take a course from the queue&lt;/span&gt;
            &lt;span class="n"&gt;visited_count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;     &lt;span class="c1"&gt;// Mark it as "taken"&lt;/span&gt;

            &lt;span class="c1"&gt;// Look at all courses that depend on the current finished course&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;neighbor&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;adj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;indegree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="o"&gt;]--;&lt;/span&gt; &lt;span class="c1"&gt;// "Remove" the dependency (decrement indegree)&lt;/span&gt;

                &lt;span class="c1"&gt;// If all prerequisites are met (indegree is 0),&lt;/span&gt;
                &lt;span class="c1"&gt;// the neighbor course becomes available to take.&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indegree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;neighbor&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// 5. FINAL CYCLE CHECK&lt;/span&gt;
        &lt;span class="c1"&gt;// If visited_count equals num_courses, it means we found a valid linear order (no cycles).&lt;/span&gt;
        &lt;span class="c1"&gt;// If not, there is at least one cycle (deadlock) in the graph.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;visited_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;num_courses&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>What is a DAG? The Essence in a Nutshell (Part 1)</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:47:54 +0000</pubDate>
      <link>https://dev.to/dima853/what-is-a-dag-the-essence-in-a-nutshell-part-1-4n62</link>
      <guid>https://dev.to/dima853/what-is-a-dag-the-essence-in-a-nutshell-part-1-4n62</guid>
      <description>&lt;p&gt;&lt;strong&gt;You know what build scripts like Make, Git history, the IOTA blockchain, and AI in TensorFlow have in common? They all operate on the same foundation — the DAG!&lt;/strong&gt; This isn't just abstract mathematics: from software builds to analyzing cause-and-effect in epidemiology, DAGs are everywhere you need to order dependencies without cycles.&lt;/p&gt;

&lt;p&gt;*simplified article&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Directed Acyclic Graph (DAG)&lt;/strong&gt; is a directed graph without cycles.&lt;/p&gt;

&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Graph:&lt;/strong&gt; A structure consisting of &lt;strong&gt;vertices (nodes)&lt;/strong&gt; and &lt;strong&gt;edges (arcs)&lt;/strong&gt; connecting them.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Directed:&lt;/strong&gt; Each edge has a direction — it goes &lt;strong&gt;from&lt;/strong&gt; one vertex &lt;strong&gt;to&lt;/strong&gt; another. Denoted by an arrow: &lt;code&gt;A -&amp;gt; B&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Acyclic:&lt;/strong&gt; The graph &lt;strong&gt;has no cycles&lt;/strong&gt;. You cannot start from some vertex, follow the direction of the arrows, and return to the starting vertex. This is a fundamental property that establishes order and causality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;A simple analogy:&lt;/strong&gt; Imagine one-way streets in a city where you cannot drive around the block and return to your starting point. Or a chain of tasks where some tasks must be strictly completed before others.&lt;/p&gt;




&lt;h3&gt;
  
  
  I. In-Depth Definitions and Mathematical Properties
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Reachability Relation, Transitive Closure, and Transitive Reduction
&lt;/h4&gt;

&lt;p&gt;These are key concepts for understanding the "power" of a DAG.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reachability Relation:&lt;/strong&gt; If there is a path from vertex &lt;code&gt;u&lt;/code&gt; to vertex &lt;code&gt;v&lt;/code&gt; (following the arrows), then we say &lt;code&gt;v&lt;/code&gt; is &lt;strong&gt;reachable&lt;/strong&gt; from &lt;code&gt;u&lt;/code&gt;. This relation is a &lt;strong&gt;partial order&lt;/strong&gt; on the set of vertices of a DAG.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transitive Closure:&lt;/strong&gt; A new graph built on the same vertices, where an edge &lt;code&gt;u -&amp;gt; v&lt;/code&gt; is added &lt;strong&gt;for every pair&lt;/strong&gt; of vertices where &lt;code&gt;v&lt;/code&gt; is reachable from &lt;code&gt;u&lt;/code&gt;. Simply put, it makes implicit connections explicit.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Example from the article:&lt;/em&gt; If the original DAG has edges &lt;code&gt;u -&amp;gt; v&lt;/code&gt; and &lt;code&gt;v -&amp;gt; w&lt;/code&gt;, then the transitive closure will also include the edge &lt;code&gt;u -&amp;gt; w&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why is it needed?&lt;/strong&gt; To quickly answer the question "is one vertex reachable from another?" — now it can be checked in O(1) (by checking for a direct edge).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transitive Reduction:&lt;/strong&gt; The graph with the &lt;strong&gt;minimum&lt;/strong&gt; number of edges that preserves the same reachability relation. This is the "skeleton" of the DAG, where all redundant edges (which duplicate longer paths) are removed.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; In a graph with edges &lt;code&gt;u -&amp;gt; v&lt;/code&gt;, &lt;code&gt;v -&amp;gt; w&lt;/code&gt;, and &lt;code&gt;u -&amp;gt; w&lt;/code&gt;, the edge &lt;code&gt;u -&amp;gt; w&lt;/code&gt; is redundant because a path from &lt;code&gt;u&lt;/code&gt; to &lt;code&gt;w&lt;/code&gt; already exists via &lt;code&gt;v&lt;/code&gt;. It can be removed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why is it needed?&lt;/strong&gt; Simplifies visualization and analysis by removing "noise." A &lt;strong&gt;Hasse diagram&lt;/strong&gt; in order theory is precisely a drawing of the transitive reduction.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt; The same partial order (reachability relation) can be represented by different DAGs (e.g., the full one and its reduction). Transitive closure and transitive reduction are the canonical forms of this representation.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Topological Sorting
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;The cornerstone of DAG theory.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Topological — relating to topology, a branch of mathematics that studies properties of figures and spaces that remain unchanged under continuous deformations (stretching, compressing, bending) without tearing or gluing. It describes qualitative structure, connectedness, and neighborhood of elements, ignoring precise metric characteristics (distances, angles).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Here, the term "topological" is used in a narrower, figurative sense, but there is a connection to the general idea!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In general topology, we study the continuous order of points in space.&lt;/li&gt;
&lt;li&gt;In a DAG, we study the partial order of vertices defined by directed edges.&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition:&lt;/strong&gt; A topological sort is a linear ordering of vertices such that for every edge &lt;code&gt;u -&amp;gt; v&lt;/code&gt;, vertex &lt;code&gt;u&lt;/code&gt; comes &lt;strong&gt;before&lt;/strong&gt; &lt;code&gt;v&lt;/code&gt; in this order.
&amp;gt; A topological sort exists only for a Directed Acyclic Graph (DAG).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fundamental Theorem:&lt;/strong&gt; &lt;strong&gt;A directed graph is acyclic (a DAG) if and only if it has a topological ordering.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Uniqueness:&lt;/strong&gt; In general, there can be many topological sorts. It is unique only when the DAG is itself a single directed path (a Hamiltonian path).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Algorithms&lt;/strong&gt; (two main approaches) ⬇️
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topological sorting algorithms are needed to process any system with dependencies where a correct execution order must be determined.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;A) Kahn's Algorithm (1962) — "intuitive," based on in-degrees&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Idea:&lt;/strong&gt; Start with vertices that have &lt;strong&gt;no incoming edges&lt;/strong&gt; (nothing depends on them). Remove them from the graph and repeat the process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; For each vertex &lt;code&gt;v&lt;/code&gt;, compute the number of incoming edges (&lt;code&gt;in_degree[v]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; Find all vertices with &lt;code&gt;in_degree[v] = 0&lt;/code&gt; and place them in a queue (or list).&lt;/li&gt;
&lt;li&gt; While the queue is not empty:

&lt;ul&gt;
&lt;li&gt;Remove a vertex &lt;code&gt;u&lt;/code&gt; from the front of the queue and append it to the result order &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;each&lt;/strong&gt; neighbor &lt;code&gt;v&lt;/code&gt; (where an edge goes from &lt;code&gt;u&lt;/code&gt; to &lt;code&gt;v&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;Decrement &lt;code&gt;in_degree[v]&lt;/code&gt; by 1 (as if "removing" the edge &lt;code&gt;u → v&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;in_degree[v]&lt;/code&gt; becomes 0, add &lt;code&gt;v&lt;/code&gt; to the queue.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; If the length of &lt;code&gt;result&lt;/code&gt; is less than the number of vertices in the graph — &lt;strong&gt;a cycle exists in the graph&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
For graph &lt;code&gt;(a,b,c,d,e)&lt;/code&gt; with edges &lt;code&gt;(a,b), (a,c), (a,d), (a,e), (b,d), (c,d), (c,e), (d,e)&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 0: Vertex &lt;code&gt;a&lt;/code&gt; has no incoming edges (&lt;code&gt;in_degree=0&lt;/code&gt;). Take &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Step 1: After removing &lt;code&gt;a&lt;/code&gt;, vertices &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; get &lt;code&gt;in_degree&lt;/code&gt; of 0. We can take &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;c&lt;/code&gt;. Possible orders: &lt;code&gt;[a, b, c, d, e]&lt;/code&gt; or &lt;code&gt;[a, c, b, d, e]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;B) Tarjan's Algorithm (1976) — "elegant," based on DFS (Depth-First Search)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Idea:&lt;/strong&gt; Run a depth-first traversal (DFS) of the graph. Add a vertex to the final list at the moment of &lt;strong&gt;finishing&lt;/strong&gt; its processing (when all its descendants have been visited). The final list then needs to be &lt;strong&gt;reversed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps (with coloring for cycle detection):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;White&lt;/strong&gt; — vertex not visited.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gray&lt;/strong&gt; — vertex is in the current recursive traversal stack (we started processing it but haven't finished).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Black&lt;/strong&gt; — vertex fully processed (all its descendants visited).&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; For each &lt;strong&gt;white&lt;/strong&gt; vertex &lt;code&gt;u&lt;/code&gt;, run DFS-visit(&lt;code&gt;u&lt;/code&gt;):&lt;/li&gt;
&lt;li&gt; Color &lt;code&gt;u&lt;/code&gt; &lt;strong&gt;gray&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; Recursively visit &lt;strong&gt;all&lt;/strong&gt; its neighbors &lt;code&gt;v&lt;/code&gt; (via outgoing edges).

&lt;ul&gt;
&lt;li&gt;If a &lt;strong&gt;gray&lt;/strong&gt; vertex is encountered — a &lt;strong&gt;cycle&lt;/strong&gt; is found! Sorting is impossible.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; After processing all neighbors, color &lt;code&gt;u&lt;/code&gt; &lt;strong&gt;black&lt;/strong&gt; and &lt;strong&gt;prepend it to the list&lt;/strong&gt; &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; At the end, &lt;strong&gt;reverse&lt;/strong&gt; the &lt;code&gt;result&lt;/code&gt; list (or equivalently, if vertices were prepended, no reversal is needed).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why does this work?&lt;/strong&gt; When we color a vertex black, all vertices reachable from it are already processed and black (i.e., they are in the result list). This guarantees that all "successors" of &lt;code&gt;u&lt;/code&gt; will already be in the list &lt;em&gt;before it&lt;/em&gt;, and after reversal will end up &lt;em&gt;after it&lt;/em&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  II. Families of Related Graphs
&lt;/h3&gt;

&lt;p&gt;DAG is a general concept. Its special cases include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multitree (Mangrove):&lt;/strong&gt; A DAG where between any pair of vertices there exists &lt;strong&gt;at most one&lt;/strong&gt; directed path. No "crossroads" where different paths can converge and diverge.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Polytree:&lt;/strong&gt; A directed tree. Obtained by taking an ordinary undirected tree and assigning directions to edges. A special case of a multitree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arborescence:&lt;/strong&gt; A polytree with a designated &lt;strong&gt;root&lt;/strong&gt;, from which all paths are directed. The classic "tree" in computer science (e.g., a filesystem).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  III. Computational Problems and Algorithms (Practical Value)
&lt;/h3&gt;

&lt;p&gt;The power of DAGs lies in the fact that some NP-hard problems for general graphs become efficiently solvable.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Topological Sorting and Cycle Detection:&lt;/strong&gt; Solvable in &lt;strong&gt;O(V+E)&lt;/strong&gt;. For most real-world &lt;strong&gt;DAGs&lt;/strong&gt;, which are sparse &lt;strong&gt;(E = O(V))&lt;/strong&gt;, this effectively means linear time &lt;strong&gt;O(V)&lt;/strong&gt;. Used as a first step in many algorithms.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Finding Shortest and Longest Paths:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In a General Graph:&lt;/strong&gt; Shortest path (with non-negative weights) — Dijkstra's algorithm (O(E log V)). Longest path — NP-hard problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In a DAG:&lt;/strong&gt; Both are solvable in &lt;strong&gt;O(V+E)&lt;/strong&gt; using topological sorting! It's enough to process vertices in topological order and update distances to neighbors using the standard relaxation rule.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Application:&lt;/em&gt; The critical path in project management (PERT) is precisely the longest path in a DAG of tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Closure Problem:&lt;/strong&gt; Given vertex weights (can be negative). Find a subset of vertices C with &lt;strong&gt;maximum total weight&lt;/strong&gt;, such that no edges leave C (a closure). Solved by reduction to the &lt;strong&gt;maximum flow/minimum cut&lt;/strong&gt; problem.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Construction from Cyclic Graphs:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Condensation:&lt;/strong&gt; Any directed graph can be transformed into a DAG by &lt;strong&gt;contracting its strongly connected components (SCCs)&lt;/strong&gt; into a single "supervertex." This is the basis of many algorithms (e.g., Kosaraju's algorithm).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orientation as a DAG:&lt;/strong&gt; For an undirected graph, one can try to assign directions to edges to form a DAG (this is always possible — simply order the vertices). The number of such &lt;strong&gt;acyclic orientations&lt;/strong&gt; equals the value of the graph's &lt;strong&gt;chromatic polynomial&lt;/strong&gt; at point (-1).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  IV. DAG Applications (Where is all this used?)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Build Systems and Task Scheduling:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Make, CMake, Gradle, etc.:&lt;/strong&gt; Build files describe dependencies between modules. The DAG guarantees that each module is compiled after all its dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Task Schedulers (Airflow, Luigi, Prefect):&lt;/strong&gt; Modern data orchestration tools represent workflows precisely as DAGs. This allows for visualization, parallelization, and task restarting.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Distributed Systems and Cryptocurrencies:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Version Control Systems (Git):&lt;/strong&gt; Commit history is a DAG (due to branch merges), not a tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Chain-Based Blockchain:&lt;/strong&gt; IOTA, Hedera Hashgraph use a DAG structure (Tangle) for transaction confirmation, where each new transaction confirms several previous ones. This is an alternative to a linear blockchain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Processing and Parallel Computing:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dataflow Programming (Apache Spark, TensorFlow):&lt;/strong&gt; Computations are represented as a graph of operations on data. The DAG allows optimization of execution order, identification of independent branches for parallelism, and avoidance of repeated computations (memoization).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Causal Models and Probabilistic Graphs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bayesian Networks:&lt;/strong&gt; A classic example of a DAG. Vertices are random variables, edges are causal links or direct influences. Acyclicity is critical for correct computation of joint probabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structural Causal Models:&lt;/strong&gt; The foundation of modern causal inference. The DAG encodes assumptions about the absence of hidden common causes for certain variables (the marginality assumption).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compilers and Static Analysis:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Control Flow Graph (CFG)&lt;/strong&gt; &lt;em&gt;is usually cyclic&lt;/em&gt; (due to loops in code). But many analyses (e.g., constant propagation) work on its &lt;strong&gt;dominance tree&lt;/strong&gt;, which is a DAG.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Dependency Graph (DDG):&lt;/strong&gt; Shows which computations depend on which others. A pure DAG, used for vectorization and instruction scheduling.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Compression and Efficient Data Structures:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compressed Dictionaries (DAWG, CDAWG):&lt;/strong&gt; Used for efficient storage of string sets (e.g., all words in a dictionary) with shared &lt;strong&gt;prefixes and suffixes&lt;/strong&gt;, saving memory compared to a trie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binary Decision Diagrams (BDD):&lt;/strong&gt; A canonical representation of Boolean functions as a DAG. The basis for efficient digital circuit and model verification.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;A DAG is not just an abstract mathematical structure, but a &lt;strong&gt;fundamental language&lt;/strong&gt; for describing relations of order, dependency, and causality in computer science, engineering, and data science. Its magic lies in the combination of expressiveness (it can model complex dependencies) and computational "friendliness" (the existence of a topological sort opens the door to efficient algorithms).&lt;/p&gt;

&lt;p&gt;Understanding DAGs allows one to see commonality in seemingly different things: from building software to drawing conclusions about cause-and-effect in epidemiology. It is a concept well worth mastering.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>JVM, Memory Management и Performance</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:47:31 +0000</pubDate>
      <link>https://dev.to/dima853/jvm-memory-management-i-performance-5568</link>
      <guid>https://dev.to/dima853/jvm-memory-management-i-performance-5568</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;JVM, Memory Management, and Performance&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Explain the complete lifecycle of an object in the heap (Heap).&lt;/strong&gt; From creation to garbage collection, including generations (Young Gen, Old Gen), Eden, S0, S1.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What is Garbage Collection (GC)?&lt;/strong&gt; Explain the main algorithms (Mark-Sweep, Mark-Compact, Copying) and their trade-offs.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Describe the differences between Serial, Parallel, CMS, G1, and ZGC garbage collectors.&lt;/strong&gt; In which scenarios is each preferable?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What are Stop-The-World (STW) pauses?&lt;/strong&gt; How do different GCs affect their duration and frequency?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Explain what a "memory leak" is in Java.&lt;/strong&gt; Provide concrete examples from practice (e.g., in static collections, caches, unclosed resources).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What is Metaspace (Java 8+) and how does it differ from PermGen?&lt;/strong&gt; What causes OutOfMemoryError: Metaspace?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Explain the String Pool (String Table).&lt;/strong&gt; How does the &lt;code&gt;intern()&lt;/code&gt; method work and when is its use justified?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What is Escape Analysis and how does it help with optimization?&lt;/strong&gt; (Connection to Stack Allocation and Scalar Replacement).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Describe the memory structure of a Java thread (Stack Memory).&lt;/strong&gt; What is stored in a method frame (local variables, operand stack, reference to runtime constant pool)?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is JIT compilation (C1, C2/C1 and C2 (Tiered Compilation))?&lt;/strong&gt; What is code "profiling" and deoptimization?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explain the principle of operation of a &lt;code&gt;volatile&lt;/code&gt; variable.&lt;/strong&gt; What is "happens-before" and how does it ensure visibility of changes between threads?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is false sharing and how to avoid it?&lt;/strong&gt; (For example, using &lt;code&gt;@Contended&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Answers to Questions:
&lt;/h1&gt;

&lt;p&gt;*&lt;strong&gt;&lt;em&gt;this article contains simplifications&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Lifecycle of an object in the heap: from allocation to reincarnation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Creation (Allocation):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The vast majority&lt;/strong&gt; of objects are allocated in &lt;strong&gt;Eden Space&lt;/strong&gt; (Young Generation). Allocation happens via the &lt;strong&gt;Pointer Bump&lt;/strong&gt; mechanism (&lt;code&gt;TLAB&lt;/code&gt; — Thread-Local Allocation Buffer), which reduces the operation to pointer increment — O(1), requiring almost no synchronization.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Large objects&lt;/strong&gt; (threshold depends on JVM, often &amp;gt; 512KB-1MB) go directly into &lt;strong&gt;Old Generation&lt;/strong&gt; (Humongous Region in G1), bypassing Young Gen, to avoid expensive copying.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Early life in Young Generation (Short-lived objects):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Eden&lt;/strong&gt;: When Eden fills up, a &lt;strong&gt;Minor GC&lt;/strong&gt; is initiated.&lt;br&gt;
&lt;strong&gt;Minor GC&lt;/strong&gt; is a fast, partial cleanup of RAM in Java that only affects the area called &lt;strong&gt;Young Generation&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Copying Algorithm&lt;/strong&gt;: Live objects (reachable from GC Roots) are copied from Eden and &lt;em&gt;one&lt;/em&gt; of the Survivor Spaces (S0 or S1) to the &lt;strong&gt;second&lt;/strong&gt; Survivor Space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Survivor Spaces (S0/S1, or From/To)&lt;/strong&gt;: Two identically sized spaces, &lt;strong&gt;always one is empty&lt;/strong&gt;.&lt;br&gt;
(&lt;strong&gt;If both Survivors contained data:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;There would be nowhere to copy new live objects from Eden&lt;/strong&gt;)&lt;br&gt;
After each Minor GC, live objects are copied between them, and their age (&lt;code&gt;age&lt;/code&gt;) is incremented. This space &lt;strong&gt;filters out&lt;/strong&gt; short-lived objects with minimal overhead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Promotion&lt;/strong&gt;: Upon reaching the age threshold (&lt;code&gt;MaxTenuringThreshold&lt;/code&gt;, usually 15), an object is considered long-lived and is moved (promoted) to &lt;strong&gt;Old Generation&lt;/strong&gt;. &lt;strong&gt;&lt;em&gt;(simplification)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Maturity in Old Generation (Long-lived objects):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Objects survive for a long time.&lt;/li&gt;
&lt;li&gt;Filling up Old Gen (or reaching a certain threshold, &lt;code&gt;InitiatingHeapOccupancyPercent&lt;/code&gt;) triggers a &lt;strong&gt;Major GC&lt;/strong&gt; (or Full GC, depending on the collector), which works with the entire heap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithms in Old Gen&lt;/strong&gt; are more complex: Mark-Sweep-Compact (Serial, Parallel), Concurrent Mark-Sweep (CMS), or mixed ones, as in G1/ZGC/Shenandoah.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Death and recycling (Garbage Collection):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An object becomes garbage when &lt;strong&gt;there is not a single reference&lt;/strong&gt; from a live object (GC Root) via &lt;strong&gt;any reachability path&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GC Roots&lt;/strong&gt;: Static variables, active Stack Frames, JNI References, loaded system classes.&lt;/li&gt;
&lt;li&gt;Memory is freed by the collector. In Eden/Survivor — by copying live objects (dead ones are ignored). In Old Gen — by "sweeping" and subsequent "compaction" to combat fragmentation.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Garbage Collection: Basic algorithms and trade-offs&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Garbage Collection&lt;/strong&gt; is an automated dynamic memory management system that frees objects unreachable by the executing program.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Algorithms:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Mark-Sweep:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Phase 1 (Mark)&lt;/strong&gt;: Traversing the reachability graph from GC Roots. Live objects are marked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phase 2 (Sweep)&lt;/strong&gt;: Linear pass through the entire memory. Unmarked (dead) blocks are marked as free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-offs&lt;/strong&gt;: &lt;strong&gt;Causes fragmentation.&lt;/strong&gt; Low overhead, but leads to a &lt;strong&gt;"holey"&lt;/strong&gt; heap, degrading allocation performance and potentially causing OOM due to lack of contiguous space.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Copying:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Divides memory into two semi-spaces (&lt;code&gt;From&lt;/code&gt; and &lt;code&gt;To&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Live objects are copied from &lt;code&gt;From&lt;/code&gt; to &lt;code&gt;To&lt;/code&gt;. After copying, the entire &lt;code&gt;From&lt;/code&gt; space is considered free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-offs&lt;/strong&gt;: &lt;strong&gt;Requires 2x more memory&lt;/strong&gt; (half is always empty). &lt;strong&gt;Does not fragment memory.&lt;/strong&gt; Extremely efficient if most objects die young. Used &lt;strong&gt;only in Young Generation&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Mark-Compact:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Phase 1 (Mark)&lt;/strong&gt;: Same as Mark-Sweep.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phase 2 (Compact)&lt;/strong&gt;: Live objects are moved to the beginning of the region, forming a contiguous block of memory. All references to moved objects are updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-offs&lt;/strong&gt;: &lt;strong&gt;Eliminates fragmentation.&lt;/strong&gt; The most expensive operation due to the cost of moving and updating references. Used primarily in &lt;strong&gt;Old Generation&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Evolutionary conclusion:&lt;/strong&gt; Young Gen uses &lt;strong&gt;Copying&lt;/strong&gt; (high mortality, efficiency). Old Gen uses hybrids of &lt;strong&gt;Mark-Sweep/Compact&lt;/strong&gt; (low mortality, combating fragmentation). Modern GCs (G1, ZGC) divide the heap into regions, applying algorithms precisely.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Garbage Collectors: Strategic Choice&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Serial GC (&lt;code&gt;-XX:+UseSerialGC&lt;/code&gt;)&lt;/strong&gt;: Single-threaded, for &lt;strong&gt;Mark, Sweep, Compact&lt;/strong&gt;. STW only. &lt;strong&gt;Scenario:&lt;/strong&gt; Single-threaded applications, microcontrollers, environments with minimal resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel GC (Throughput Collector) (&lt;code&gt;-XX:+UseParallelGC&lt;/code&gt;)&lt;/strong&gt;: Multi-threaded versions of Serial for Young and Old Gen. &lt;strong&gt;Maximizes throughput&lt;/strong&gt; at the cost of more aggressive CPU usage and &lt;strong&gt;longer STW pauses&lt;/strong&gt;. &lt;strong&gt;Scenario:&lt;/strong&gt; Batch processing, computations, where pauses of hundreds of milliseconds to seconds are acceptable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CMS – Concurrent Mark Sweep (&lt;code&gt;-XX:+UseConcMarkSweepGC&lt;/code&gt;)&lt;/strong&gt;: &lt;strong&gt;Reduces STW pause duration&lt;/strong&gt; by having the collector work concurrently with the application.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Phases:&lt;/strong&gt; &lt;code&gt;Initial Mark&lt;/code&gt; (STW, fast), &lt;code&gt;Concurrent Mark&lt;/code&gt;, &lt;code&gt;Concurrent Preclean&lt;/code&gt;, &lt;code&gt;Remark&lt;/code&gt; (STW), &lt;code&gt;Concurrent Sweep&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-offs:&lt;/strong&gt; Does not perform compaction by default → &lt;strong&gt;fragmentation&lt;/strong&gt;, possible &lt;code&gt;Concurrent Mode Failure&lt;/code&gt; (forced Full GC). High CPU consumption in background.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;G1 – Garbage First (&lt;code&gt;-XX:+UseG1GC&lt;/code&gt;, default from Java 9-11)&lt;/strong&gt;: Regional (&lt;code&gt;-XX:G1HeapRegionSize&lt;/code&gt;), predictive.

&lt;ul&gt;
&lt;li&gt;Divides the heap into ~2000 regions. Collects regions with the most garbage first (&lt;code&gt;Garbage First&lt;/code&gt;). Has &lt;strong&gt;soft real-time goals&lt;/strong&gt; (&lt;code&gt;-XX:MaxGCPauseMillis&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenario:&lt;/strong&gt; Universal balance between throughput and latency. The main choice for most applications with heap &amp;gt;4-6GB.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ZGC (&lt;code&gt;-XX:+UseZGC&lt;/code&gt;) and Shenandoah (&lt;code&gt;-XX:+UseShenandoahGC&lt;/code&gt;)&lt;/strong&gt;: &lt;strong&gt;Low-latency&lt;/strong&gt; (&lt;code&gt;sub-millisecond&lt;/code&gt; goals) collectors.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key feature:&lt;/strong&gt; Almost all phases, including object relocation, are performed &lt;strong&gt;concurrently&lt;/strong&gt; with the application.&lt;/li&gt;
&lt;li&gt;Use colored pointers and read/write barriers (&lt;code&gt;load barriers&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenario:&lt;/strong&gt; Latency-critical applications: financial transactions, high-load web services, large heaps (terabytes).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Selection strategy:&lt;/strong&gt; The &lt;strong&gt;lower the acceptable latency&lt;/strong&gt;, the more advanced and concurrent a collector is required. &lt;strong&gt;Throughput -&amp;gt; Latency&lt;/strong&gt; gradient: Parallel -&amp;gt; G1 -&amp;gt; ZGC/Shenandoah.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;4. Stop-The-World (STW): Anatomy of a Freeze&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;STW&lt;/strong&gt; — a phase when all application threads are suspended to perform a GC operation safe against a changing object graph.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Causes:&lt;/strong&gt; Root scanning (&lt;code&gt;Root Scanning&lt;/code&gt;), the &lt;code&gt;Remark&lt;/code&gt; phase in CMS/G1 (accounting for changes during concurrent marking), evacuation and compaction in non-concurrent phases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GC Impact:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Serial/Parallel:&lt;/strong&gt; Dominant, long STW phases. Pauses grow with heap size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CMS:&lt;/strong&gt; Significantly reduces STW (&lt;code&gt;Initial Mark&lt;/code&gt;, &lt;code&gt;Remark&lt;/code&gt;), but leaves the risk of &lt;code&gt;Concurrent Mode Failure&lt;/code&gt; (long STW).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;G1:&lt;/strong&gt; Predictable, manageable pauses (&lt;code&gt;MaxGCPauseMillis&lt;/code&gt;). STW is limited to evacuating a selected set of regions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ZGC/Shenandoah:&lt;/strong&gt; STW is reduced to microsecond root scanning (&lt;code&gt;Root Scanning&lt;/code&gt;). Most of the work is concurrent.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;5. Memory Leak in Java: Systematic Failure&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory leak&lt;/strong&gt; — a situation where objects are no longer used by the application but cannot be collected by GC due to remaining &lt;strong&gt;incorrect references&lt;/strong&gt; stored in live data structures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;This is not a JVM bug, but a logical error in the code.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Canonical examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Static collections (Classic):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LeakyClass&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;STATIC_CACHE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="no"&gt;STATIC_CACHE&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The data object is forever reachable via the static field&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Uncontrolled caches (Guava Cache, Caffeine without eviction policy):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Caffeine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newBuilder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// No expireAfterWrite or maximumSize&lt;/span&gt;
&lt;span class="c1"&gt;// Cache grows indefinitely.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unclosed resources (&lt;code&gt;InputStream&lt;/code&gt;, &lt;code&gt;Connection&lt;/code&gt;, &lt;code&gt;Session&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
Resources often hold references to internal buffers or objects in native memory. &lt;strong&gt;Solution:&lt;/strong&gt; &lt;code&gt;try-with-resources&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event listeners (Listeners) and inner classes:&lt;/strong&gt;&lt;br&gt;
Not unsubscribing from a listener stored in a global context keeps a reference to the outer class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;ThreadLocal&lt;/code&gt; without cleanup (especially in thread pools):&lt;/strong&gt;&lt;br&gt;
The value in &lt;code&gt;ThreadLocal&lt;/code&gt; lives as long as the thread lives. In web applications, a thread returns to the pool and lives for years.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ThreadLocal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HeavyContext&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;threadLocal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ThreadLocal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// After use, it is necessary to: threadLocal.remove();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Diagnosis:&lt;/strong&gt; Monitoring Old Gen (constant growth), analyzing heap dump (&lt;code&gt;jmap -dump&lt;/code&gt;, &lt;code&gt;MAT&lt;/code&gt;, &lt;code&gt;VisualVM&lt;/code&gt;), searching for &lt;code&gt;java.lang.Object[]&lt;/code&gt; with the largest retained size.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;6. Metaspace vs PermGen: Evolution of Metadata&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;PermGen (up to Java 7)&lt;/strong&gt; — a fixed heap segment for class metadata, causing frequent &lt;code&gt;OutOfMemoryError&lt;/code&gt; and requiring manual size tuning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Metaspace (since Java 8)&lt;/strong&gt; — a dynamic area in native memory, automatically managed by the OS, eliminating PermGen problems and allowing efficient loading and unloading of classes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PermGen (≤ Java 7)&lt;/strong&gt;: Fixed size (&lt;code&gt;-XX:MaxPermSize&lt;/code&gt;). Stored class metadata, interned strings, static members. Frequent cause of &lt;code&gt;OutOfMemoryError: PermGen space&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metaspace (Java 8+)&lt;/strong&gt;: &lt;strong&gt;Native memory&lt;/strong&gt; (not part of Java Heap).

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Managed by the OS&lt;/strong&gt;, unlimited by default (limited by physical memory/swap).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic growth and cleanup.&lt;/strong&gt; Class-loaders and their loaded classes are collected by GC.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Divided:&lt;/strong&gt; &lt;code&gt;Klass Metaspace&lt;/code&gt; (non-droppable metadata), &lt;code&gt;NoKlass Metaspace&lt;/code&gt; for other things.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;OutOfMemoryError: Metaspace&lt;/code&gt; occurs when:&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt; The limit is reached (&lt;code&gt;-XX:MaxMetaspaceSize&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Metadata leak (ClassLoader Leak)&lt;/strong&gt;: A common cause — containers (Tomcat, OSGi) where applications are reloaded, but the old ClassLoader is held (e.g., via a thread or static reference), preventing its classes from being unloaded.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;7. String Pool (String Table): Deduplication Mechanism&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;String Pool&lt;/strong&gt; — a hash table (&lt;code&gt;Hashtable&lt;/code&gt;) in the heap (previously in PermGen), storing &lt;strong&gt;canonical&lt;/strong&gt; (&lt;code&gt;interned&lt;/code&gt;) instances of &lt;code&gt;String&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules:&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt; String literals (&lt;code&gt;"text"&lt;/code&gt;) are added to the Pool &lt;strong&gt;during class loading&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;String.intern()&lt;/code&gt;: Allows &lt;strong&gt;adding&lt;/strong&gt; a string created at runtime to the Pool. Returns the canonical representation.

&lt;ul&gt;
&lt;li&gt;If the string is already in the Pool — returns a reference to it.&lt;/li&gt;
&lt;li&gt;If not — adds the current object to the Pool and returns it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When to use &lt;code&gt;intern()&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Almost never&lt;/strong&gt; in typical application code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Justified:&lt;/strong&gt; When processing huge volumes of data with &lt;strong&gt;a high degree of string duplication&lt;/strong&gt; (parsing CSV, tags, enum-like values), when it is required:&lt;/li&gt;
&lt;li&gt;Significant memory savings (one string for many identical values).&lt;/li&gt;
&lt;li&gt;Accelerated comparison via &lt;code&gt;==&lt;/code&gt; (replacing &lt;code&gt;.equals()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Danger:&lt;/strong&gt; Uncontrolled use leads to &lt;strong&gt;growth of the Pool, which is never cleared&lt;/strong&gt; (before Java 7). Since Java 7+, interned strings reside in the heap and can be collected by GC if the ClassLoader is unloaded.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;8. Escape Analysis: Compiler Magic for Optimization&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Escape Analysis (EA)&lt;/strong&gt; — JIT compiler (C2) analysis determining the &lt;strong&gt;visibility scope&lt;/strong&gt; of a created object.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NoEscape&lt;/strong&gt;: The object does not leave the method and/or thread bounds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ArgEscape&lt;/strong&gt;: The object is passed to another method but does not "escape" the thread.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GlobalEscape&lt;/strong&gt;: The object is published (saved to a static field, passed to another thread).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Based on EA, JIT applies optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Scalar Replacement:&lt;/strong&gt; If an object is &lt;code&gt;NoEscape&lt;/code&gt;, JIT &lt;strong&gt;does not allocate it on the heap&lt;/strong&gt;. Instead, its fields are transformed into local variables of the method (primitives/references) on the stack. &lt;strong&gt;Ideal optimization:&lt;/strong&gt; zero allocation overhead, zero GC overhead.
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Before optimization&lt;/span&gt;
  &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// After Scalar Replacement&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;p_x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p_y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p_x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p_y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Point object is not created.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Stack Allocation:&lt;/strong&gt; A special case of Scalar Replacement. Theoretical allocation on the stack, but in HotSpot it is implemented precisely as decomposition.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Lock Elision:&lt;/strong&gt; If the monitor of an object is &lt;code&gt;NoEscape&lt;/code&gt; (e.g., a synchronized block on a local object), the lock &lt;strong&gt;is removed&lt;/strong&gt;, as it cannot be contended in another thread.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Activation:&lt;/strong&gt; Enabled by default (&lt;code&gt;-XX:+DoEscapeAnalysis&lt;/code&gt;). Effective for short-lived, local objects (DTOs, iterators, builders).&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;9. Thread Memory (Stack Memory): Frame Architecture&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Each JVM thread has a &lt;strong&gt;private stack&lt;/strong&gt;, created when it starts. The stack consists of &lt;strong&gt;stack frames&lt;/strong&gt;, pushed on method call and popped on its completion (normal or exceptional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structure of a method frame:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Local Variable Array (LVA)&lt;/strong&gt;: Array of method variables, indexed from 0.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; (for non-static methods) is stored in &lt;code&gt;LVA[0]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Method parameters — in &lt;code&gt;LVA[1]&lt;/code&gt;, &lt;code&gt;LVA[2]&lt;/code&gt;, ...&lt;/li&gt;
&lt;li&gt;Local variables — in subsequent slots.&lt;/li&gt;
&lt;li&gt;Each slot is 32 bits (&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;reference&lt;/code&gt;). &lt;code&gt;long&lt;/code&gt;/&lt;code&gt;double&lt;/code&gt; occupy 2 slots.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Operand Stack (OS)&lt;/strong&gt;: Working area for computations (stack-architecture style). Bytecode instructions (&lt;code&gt;iload&lt;/code&gt;, &lt;code&gt;iadd&lt;/code&gt;, &lt;code&gt;invokevirtual&lt;/code&gt;) operate on this stack (push/pop values).&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Bytecode:&lt;/span&gt;
&lt;span class="n"&gt;iconst_5&lt;/span&gt; &lt;span class="c1"&gt;// push 5 -&amp;gt; OS&lt;/span&gt;
&lt;span class="n"&gt;istore_1&lt;/span&gt; &lt;span class="c1"&gt;// pop OS -&amp;gt; LVA[1] (a)&lt;/span&gt;
&lt;span class="n"&gt;iconst_3&lt;/span&gt; &lt;span class="c1"&gt;// push 3 -&amp;gt; OS&lt;/span&gt;
&lt;span class="n"&gt;istore_2&lt;/span&gt; &lt;span class="c1"&gt;// pop OS -&amp;gt; LVA[2] (b)&lt;/span&gt;
&lt;span class="n"&gt;iload_1&lt;/span&gt;  &lt;span class="c1"&gt;// push LVA[1] (a) -&amp;gt; OS&lt;/span&gt;
&lt;span class="n"&gt;iload_2&lt;/span&gt;  &lt;span class="c1"&gt;// push LVA[2] (b) -&amp;gt; OS&lt;/span&gt;
&lt;span class="n"&gt;iadd&lt;/span&gt;     &lt;span class="c1"&gt;// pop 2 values, add, push result -&amp;gt; OS&lt;/span&gt;
&lt;span class="n"&gt;istore_3&lt;/span&gt; &lt;span class="c1"&gt;// pop OS -&amp;gt; LVA[3] (c)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reference to Runtime Constant Pool (RCP)&lt;/strong&gt;: Pointer to the class's Constant Pool, needed for resolving symbolic references (method names, classes, constants) at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Size:&lt;/strong&gt; Set by the &lt;code&gt;-Xss&lt;/code&gt; parameter (default ~1MB). Overflow → &lt;code&gt;StackOverflowError&lt;/code&gt;. Dynamic expansion → &lt;code&gt;OutOfMemoryError&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;10. JIT Compilation: C1, C2, and Adaptive Optimization&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JIT (Just-In-Time)&lt;/strong&gt; — compilation of "hot" bytecode into native machine code at runtime.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compilation levels in HotSpot (Tiered Compilation, &lt;code&gt;-XX:+TieredCompilation&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interpreter&lt;/strong&gt;: Executes bytecode. Zero startup overhead, but low speed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C1 (Client Compiler)&lt;/strong&gt;: Fast, lightweight compilation. Applies basic optimizations (inlining, simple data flow analysis). Goal — quickly get working native code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C2 (Server Compiler)&lt;/strong&gt;: Aggressive, heavy optimizing compiler. Uses &lt;strong&gt;complex static analysis&lt;/strong&gt; (EA, scalar replacement, loop unrolling, macro- and micro-fusion, memory and barrier optimizations). Compiles &lt;strong&gt;the hottest methods&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Profiling&lt;/strong&gt;: JVM collects data about code operation at runtime:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Method invocation counters.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Branching&lt;/strong&gt;: Which &lt;code&gt;if&lt;/code&gt; branch executes more often.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type Profile&lt;/strong&gt;: Which concrete classes arrive at a polymorphic call (&lt;code&gt;invokevirtual&lt;/code&gt;). This allows &lt;strong&gt;devirtualization&lt;/strong&gt; — replacing a virtual call with a direct one, and then &lt;strong&gt;inlining&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deoptimization&lt;/strong&gt;: The reverse process. If the optimizer's assumptions are violated (e.g., a new type arrives, not accounted for in the profile), JVM &lt;strong&gt;rolls back&lt;/strong&gt; the compiled native code back to interpreted bytecode.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Triggers:&lt;/strong&gt; "Stale" profile (class loading, new polymorphic types), debug points (breakpoint), dependency reset. &lt;strong&gt;(simplification)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cycle:&lt;/strong&gt; Interpreter → profiling → C1 → profiling → C2 → (deoptimization if necessary). This is &lt;strong&gt;Adaptive Optimization&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;11. &lt;code&gt;volatile&lt;/code&gt;: Guarantees of Visibility and Ordering&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;volatile&lt;/code&gt;&lt;/strong&gt; — a variable modifier providing &lt;strong&gt;guarantees of visibility and ordering&lt;/strong&gt; at the memory level, without atomicity for compound operations (&lt;code&gt;i++&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Semantics:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Visibility&lt;/strong&gt;: A write to a &lt;code&gt;volatile&lt;/code&gt; variable by one thread &lt;strong&gt;is guaranteed to become visible&lt;/strong&gt; to all subsequent reads of that variable from other threads.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Prevention of Reordering&lt;/strong&gt;: JVM and processor cannot reorder read/write operations of a &lt;code&gt;volatile&lt;/code&gt; variable with other memory operations in a way that violates the &lt;strong&gt;happens-before rule&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Happens-Before&lt;/strong&gt;: The formal Java memory model defining &lt;strong&gt;guarantees of visibility of changes between threads&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rule for &lt;code&gt;volatile&lt;/code&gt; (JLS 17.4.5)&lt;/strong&gt;: A write to a &lt;code&gt;volatile&lt;/code&gt; field &lt;strong&gt;happens-before&lt;/strong&gt; every subsequent read of the same field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consequence (Transitivity)&lt;/strong&gt;: If thread A writes to &lt;code&gt;volatile V&lt;/code&gt;, and then thread B reads &lt;code&gt;V&lt;/code&gt;, then &lt;strong&gt;all memory changes made by thread A before writing to &lt;code&gt;V&lt;/code&gt; become visible to thread B after reading &lt;code&gt;V&lt;/code&gt;&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Thread 1&lt;/span&gt;
&lt;span class="n"&gt;sharedNonVolatileData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;...;&lt;/span&gt; &lt;span class="c1"&gt;// (1)&lt;/span&gt;
&lt;span class="n"&gt;volatileFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// (2) volatile write&lt;/span&gt;
&lt;span class="c1"&gt;// Thread 2&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;volatileFlag&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;           &lt;span class="c1"&gt;// (3) volatile read (will see true)&lt;/span&gt;
    &lt;span class="c1"&gt;// Here, the value of sharedNonVolatileData from (1) is guaranteed to be visible&lt;/span&gt;
    &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sharedNonVolatileData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Implementation:&lt;/strong&gt; At the processor level, this is usually implemented via &lt;strong&gt;memory barriers&lt;/strong&gt; (&lt;code&gt;Memory Barrier&lt;/code&gt; or &lt;code&gt;Fence&lt;/code&gt;). Writing &lt;code&gt;volatile&lt;/code&gt; includes &lt;code&gt;StoreStore&lt;/code&gt; + &lt;code&gt;StoreLoad&lt;/code&gt; barriers. Reading — &lt;code&gt;LoadLoad&lt;/code&gt; + &lt;code&gt;LoadStore&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; For completion flags, publishing results (safe publication), in patterns like &lt;code&gt;double-checked locking&lt;/code&gt; (with &lt;code&gt;volatile&lt;/code&gt;).&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;12. False Sharing: The Hidden Performance Enemy&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;False Sharing&lt;/strong&gt; — performance degradation in multi-threaded systems, occurring when two independent &lt;strong&gt;frequently modified&lt;/strong&gt; fields (&lt;code&gt;M1&lt;/code&gt; and &lt;code&gt;M2&lt;/code&gt;), belonging to different objects (or different array elements), fall into &lt;strong&gt;the same cache line (cache line, usually 64 bytes)&lt;/strong&gt; of the processor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mechanism:&lt;/strong&gt; Processors maintain cache coherency via the MESI protocol. If a thread on core 1 modifies &lt;code&gt;M1&lt;/code&gt;, the entire cache line is marked as "modified" (&lt;code&gt;Modified&lt;/code&gt;), invalidating the same cache line on core 2, even if it only contains &lt;code&gt;M2&lt;/code&gt;. Core 2, when accessing &lt;code&gt;M2&lt;/code&gt;, is forced to re-read the line from memory, even though the value &lt;code&gt;M2&lt;/code&gt; itself hasn't changed. This causes &lt;strong&gt;cascading invalidation&lt;/strong&gt; and a "race" for the cache line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consequence:&lt;/strong&gt; Seemingly independent operations start competing synchronously, causing a sharp drop in scalability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution — Alignment (Padding, @Contended):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Classic padding (pre-Java 8):&lt;/strong&gt; Adding "empty" fields to separate critical fields into different cache lines.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;count1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Padding ~56 bytes&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;count2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;@sun.misc.Contended&lt;/code&gt; (Java 8+)&lt;/strong&gt;: Annotation instructing JVM to &lt;strong&gt;automatically add padding&lt;/strong&gt; around a field or the entire class.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;jdk.internal.vm.annotation.Contended&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripedCounter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Contended&lt;/span&gt; &lt;span class="c1"&gt;// JVM will add padding (~128 bytes) around each field&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;cell1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@Contended&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;cell2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Requires `-XX:-RestrictContended` for use outside `java.base`.
- Widely used in JDK internals (`LongAdder`, `Thread`, `ForkJoinPool`).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Alternatives:&lt;/strong&gt; Designing data structures so that threads work with independent memory areas (local variables, &lt;code&gt;ThreadLocal&lt;/code&gt;), or using thread-local structures like &lt;code&gt;LongAdder&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Diagnosis:&lt;/strong&gt; Profilers (VTune, &lt;code&gt;perf&lt;/code&gt;) can track events like &lt;code&gt;RESOURCE_STALLS.L1D_MISS_CYCLES&lt;/code&gt; or &lt;code&gt;MEM_LOAD_RETIRED.L2_MISS&lt;/code&gt;. In Java — empirically, by performance degradation when adding seemingly independent operations.&lt;/p&gt;
&lt;h3&gt;
  
  
  Once again, and perhaps a bit more clearly -&amp;gt;
&lt;/h3&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;PART 1: JVM MEMORY ARCHITECTURE - MACRO LEVEL&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Heap: The Dominant Structure in JVM&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Physical organization (64-bit HotSpot JVM)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────────┐
│                     HEAP (Max: 32/64 TB)                    │
├──────────────┬─────────────────┬────────────────────────────┤
│  YOUNG GEN   │                 │        OLD GEN             │
│  (1-3 regions) │                 │       (2/3 of heap)        │
├──────────────┼─────────────────┼────────────────────────────┤
│    EDEN      │   SURVIVOR S0   │                            │
│   (80% YG)   │   SURVIVOR S1   │       Long-lived           │
│              │   (10% YG each) │       objects, survived    │
│              │                 │       many GCs             │
├──────────────┴─────────────────┴────────────────────────────┤
│                     METASPACE                               │
│  (Class metadata, methods, constants, annotations)          │
└─────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Quantitative parameters (default)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-Xms&lt;/code&gt; / &lt;code&gt;-Xmx&lt;/code&gt;: Initial/Maximum heap size&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:NewRatio=2&lt;/code&gt;: OldGen:YoungGen = 2:1&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:SurvivorRatio=8&lt;/code&gt;: Eden:Survivor = 8:1 (each Survivor)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:MaxTenuringThreshold=15&lt;/code&gt;: Maximum age for promotion&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Object Lifecycle: Detailed Chronology&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Phase 1: Allocation in Eden&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AllocationPatterns&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TLAB (Thread-Local Allocation Buffer) - key optimization&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;demonstrateTLAB&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// When creating an object:&lt;/span&gt;
        &lt;span class="c1"&gt;// 1. Check: is there enough space in the current TLAB?&lt;/span&gt;
        &lt;span class="c1"&gt;// 2. If yes: pointer bump allocation (pointer += size)&lt;/span&gt;
        &lt;span class="c1"&gt;// 3. If no: request a new TLAB from Eden&lt;/span&gt;

        &lt;span class="c1"&gt;// TLAB size is configurable:&lt;/span&gt;
        &lt;span class="c1"&gt;// -XX:TLABSize=512k (size)&lt;/span&gt;
        &lt;span class="c1"&gt;// -XX:+ResizeTLAB (automatic resize)&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100_000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// 99% of objects are allocated here&lt;/span&gt;
            &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ~12 bytes + overhead&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Allocation mechanics&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pointer Bump in TLAB&lt;/strong&gt;: &lt;code&gt;current_ptr += object_size&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zeroing memory&lt;/strong&gt;: JVM zeroes memory for safety&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Setting Mark Word&lt;/strong&gt;: &lt;code&gt;mark = hash/age/lock_bits&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Setting Klass Pointer&lt;/strong&gt;: reference to object's &lt;code&gt;Class&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cost&lt;/strong&gt;: 10-20 CPU cycles for a small object&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Phase 2: First Minor GC&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Trigger&lt;/strong&gt;: Eden is 80-90% full (adaptive)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Copying Collector Algorithm&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// HotSpot pseudo-code (Young GC)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;youngGC&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Stop-The-World: suspend all threads&lt;/span&gt;
    &lt;span class="n"&gt;stop_all_threads&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Root scanning (very fast)&lt;/span&gt;
    &lt;span class="n"&gt;scan_roots&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. Copy live objects from Eden and From-Survivor to To-Survivor&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Eden&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;From_Survivor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_alive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;new_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;To_Survivor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;forward_pointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_location&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// To update references&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 4. Swap Survivor spaces&lt;/span&gt;
    &lt;span class="n"&gt;swap_survivors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 5. Age objects in Survivor&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;To_Survivor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;promote_to_old_gen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 6. Resume&lt;/span&gt;
    &lt;span class="n"&gt;resume_all_threads&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Critical details&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Card Table&lt;/strong&gt;: Bitmap for tracking references from OldGen to YoungGen&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remembered Sets&lt;/strong&gt;: In G1/ZGC for tracking inter-region references&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Phase 3: Promotion to Old Generation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Promotion conditions&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Age threshold&lt;/strong&gt;: &lt;code&gt;age &amp;gt;= MaxTenuringThreshold&lt;/code&gt; (usually 15)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Survivor size&lt;/strong&gt;: If Survivor overflows, oldest objects are promoted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large objects&lt;/strong&gt;: &amp;gt; &lt;code&gt;-XX:PretenureSizeThreshold&lt;/code&gt; (usually 1MB) go directly to OldGen
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example: creating long-lived objects&lt;/span&gt;
&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createLongLivedObjects&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;longLived&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// These objects will survive several Minor GCs&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 100KB - enough for promotion after several GCs&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;102400&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;longLived&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Create garbage to provoke GC&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;garbage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Will be collected&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Garbage Collector Models: Evolution of Algorithms&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Serial Collector (Mark-Sweep-Compact)&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Algorithm:
  1. Mark: Traverse reachability graph from GC Roots
  2. Sweep: Free unmarked areas
  3. Compact: Defragmentation (optional)

Features:
  - Single-threaded (STW for the entire time)
  - Simple, low overhead
  - Ideal for embedded and client applications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. Parallel / Throughput Collector&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Algorithm:
  - Multi-threaded versions of Serial for all phases
  - Goal: maximize throughput (application/GC)

Configuration:
  -XX:+UseParallelGC
  -XX:ParallelGCThreads=(CPU cores)
  -XX:MaxGCPauseMillis=200 (target)
  -XX:GCTimeRatio=99 (99% time for application)

Usage: batch processing, ETL, scientific computing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;3. CMS - Concurrent Mark Sweep (deprecated)&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CMS phases:&lt;/span&gt;
&lt;span class="mf"&gt;1.&lt;/span&gt; &lt;span class="n"&gt;Initial&lt;/span&gt; &lt;span class="n"&gt;Mark&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;STW&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// Fast, only direct roots&lt;/span&gt;
&lt;span class="mf"&gt;2.&lt;/span&gt; &lt;span class="n"&gt;Concurrent&lt;/span&gt; &lt;span class="n"&gt;Mark&lt;/span&gt;         &lt;span class="c1"&gt;// Concurrent with application&lt;/span&gt;
&lt;span class="mf"&gt;3.&lt;/span&gt; &lt;span class="n"&gt;Remark&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;STW&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// Account for changes during concurrent mark&lt;/span&gt;
&lt;span class="mf"&gt;4.&lt;/span&gt; &lt;span class="n"&gt;Concurrent&lt;/span&gt; &lt;span class="n"&gt;Sweep&lt;/span&gt;       &lt;span class="c1"&gt;// Cleanup&lt;/span&gt;

&lt;span class="n"&gt;Problems&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Fragmentation&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;compaction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Concurrent&lt;/span&gt; &lt;span class="n"&gt;Mode&lt;/span&gt; &lt;span class="n"&gt;Failure&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;rapid&lt;/span&gt; &lt;span class="n"&gt;filling&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;High&lt;/span&gt; &lt;span class="n"&gt;CPU&lt;/span&gt; &lt;span class="n"&gt;usage&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;concurrent&lt;/span&gt; &lt;span class="n"&gt;phases&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;4. G1 - Garbage First (default since Java 9)&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Architecture:
  - Heap divided into ~2000 regions (1-32MB)
  - Young generation = set of regions (not fixed)
  - Humongous regions for objects &amp;gt;50% region

Algorithm:
  1. Concurrent marking (like CMS)
  2. Evacuation: copying live objects from "garbage first" regions
  3. Compaction on-the-fly

Configuration:
  -XX:+UseG1GC
  -XX:G1HeapRegionSize={1,2,4,8,16,32}M
  -XX:MaxGCPauseMillis=200
  -XX:InitiatingHeapOccupancyPercent=45
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;5. ZGC / Shenandoah (Low-Latency)&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Innovations:
  - Load barriers instead of write barriers
  - Colored pointers (metadata in pointers)
  - Region-based like G1, but all phases concurrent

ZGC pointer structure:
  ┌─────────┬──────┬──────┬──────────────────────┐
  │ 42 bits │ 4b   │ 4b   │ 14b                  │
  │ Address │ 0000 │ Mark │ Unused               │
  └─────────┴──────┴──────┴──────────────────────┘

Advantages:
  - STW &amp;lt; 1ms regardless of heap size
  - Support for terabyte heaps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 2: STOP-THE-WORLD - ARCHITECTURAL VIEW&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Anatomy of a JVM Pause&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// HotSpot VM safepoint operation&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SafepointSynchronize&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Set safepoint flag&lt;/span&gt;
    &lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_synchronizing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Stop all threads at safe points&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JavaThread&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Threads&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;safepoint_state&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;examine_state_of_thread&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Thread must stop in one of:&lt;/span&gt;
        &lt;span class="c1"&gt;// - Between bytecode instructions (in interpreted)&lt;/span&gt;
        &lt;span class="c1"&gt;// - At safepoint polling page (in compiled code)&lt;/span&gt;
        &lt;span class="c1"&gt;// - Blocked in native code&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. All threads stopped&lt;/span&gt;
    &lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_synchronized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// 4. Perform operation (GC, deopt, etc.)&lt;/span&gt;
    &lt;span class="n"&gt;do_operation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 5. Resume&lt;/span&gt;
    &lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_not_synchronized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Safepoint Polling in Compiled Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; x86_64 generated JIT code
compiled_method:
    ; Prologue
    push   rbp
    mov    rbp, rsp

    ; Method body
    mov    rax, [rsi+0x10]  ; Load field
    add    rax, 0x1
    mov    [rsi+0x10], rax  ; Store

    ; Safepoint poll (every ~1000 instructions)
    test   byte ptr [rip+safepoint_page], 0xff
    jnz    safepoint_handler  ; Jump if safepoint

    ; Continue
    ret

safepoint_page:  ; Memory page, changed on safepoint
    .byte 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 3: MEMORY LEAK - SYSTEMATIC ANALYSIS&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Memory Leak Typology&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Classic leak via statics&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClassicLeak&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Global cache without limits&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;CACHE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Leak: objects never removed&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Key&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extractKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computeExpensiveValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="no"&gt;CACHE&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Forever in memory&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Solution 1: WeakHashMap&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;WEAK_CACHE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;synchronizedMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WeakHashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Solution 2: Guava Cache with policies&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;GUAVA_CACHE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nc"&gt;CacheBuilder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newBuilder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;maximumSize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;expireAfterWrite&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MINUTES&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;weakKeys&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. ThreadLocal in thread pool&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThreadLocalLeak&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ThreadLocal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ByteBuffer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;BUFFER_HOLDER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ThreadLocal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ByteBuffer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;@Override&lt;/span&gt;
            &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;ByteBuffer&lt;/span&gt; &lt;span class="nf"&gt;initialValue&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ByteBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allocateDirect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1MB direct buffer&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// In web application (Tomcat):&lt;/span&gt;
    &lt;span class="c1"&gt;// Thread returns to pool after request&lt;/span&gt;
    &lt;span class="c1"&gt;// ThreadLocal is not automatically cleaned!&lt;/span&gt;
    &lt;span class="c1"&gt;// Memory accumulates: pool_size * buffer_size&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpServletRequest&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ByteBuffer&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BUFFER_HOLDER&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// use...&lt;/span&gt;
        &lt;span class="c1"&gt;// FORGET: BUFFER_HOLDER.remove();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;3. Incorrect event listeners&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ListenerLeak&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EventListener&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CopyOnWriteArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;registerListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EventListener&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;listeners&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// NO unregisterListener method!&lt;/span&gt;
    &lt;span class="c1"&gt;// Listener holds reference to outer object&lt;/span&gt;
    &lt;span class="c1"&gt;// → leak of entire reference chain&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;4. JNI/Off-Heap leaks&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NativeMemoryLeak&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;loadLibrary&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"native"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;native&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;allocateNativeMemory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;native&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;freeNativeMemory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;leak&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allocateNativeMemory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1MB native&lt;/span&gt;
        &lt;span class="c1"&gt;// Forget to call freeNativeMemory(ptr)&lt;/span&gt;
        &lt;span class="c1"&gt;// → leak in native heap (not visible in Java heap dump!)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Leak Diagnostics:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Real-time monitoring&lt;/span&gt;
jstat &lt;span class="nt"&gt;-gc&lt;/span&gt; &amp;lt;pid&amp;gt; 1s  &lt;span class="c"&gt;# Check OldGen growth after Full GC&lt;/span&gt;

&lt;span class="c"&gt;# 2. Taking heap dump (production with caution!)&lt;/span&gt;
jmap &lt;span class="nt"&gt;-dump&lt;/span&gt;:live,format&lt;span class="o"&gt;=&lt;/span&gt;b,file&lt;span class="o"&gt;=&lt;/span&gt;heap.hprof &amp;lt;pid&amp;gt;

&lt;span class="c"&gt;# 3. Analysis in Eclipse MAT&lt;/span&gt;
&lt;span class="c"&gt;#    Key queries:&lt;/span&gt;
&lt;span class="c"&gt;#    - "Leak Suspects Report"&lt;/span&gt;
&lt;span class="c"&gt;#    - "Top Consumers"&lt;/span&gt;
&lt;span class="c"&gt;#    - "Histogram grouped by class"&lt;/span&gt;
&lt;span class="c"&gt;#    - "Path to GC Roots"&lt;/span&gt;

&lt;span class="c"&gt;# 4. Command line analysis&lt;/span&gt;
jmap &lt;span class="nt"&gt;-histo&lt;/span&gt;:live &amp;lt;pid&amp;gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-20&lt;/span&gt;  &lt;span class="c"&gt;# Largest classes&lt;/span&gt;

&lt;span class="c"&gt;# 5. JFR (Java Flight Recorder) for dynamic analysis&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; JFR.start &lt;span class="nv"&gt;duration&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;60s &lt;span class="nv"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;leak.jfr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 4: METASPACE - CLASS METADATA&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Evolution from PermGen to Metaspace&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;PermGen (≤ Java 7)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────┐
│           PERMGEN               │
│  (Fixed size, part of Heap)     │
├─────────────────────────────────┤
│ • Class metadata                │
│ • Bytecode                      │
│ • Runtime constant pool         │
│ • String intern table           │
│ • JIT code cache (partially)    │
└─────────────────────────────────┘
Problems: OOM, manual size tuning, inefficient GC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Metaspace (Java 8+)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────┐
│         NATIVE MEMORY           │
│  (Not Heap, managed by OS)      │
├─────────────────────────────────┤
│     METASPACE                   │
│  ┌─────────────────────────┐    │
│  │  Non-Class Metaspace    │    │
│  │  ┌───────────────────┐  │    │
│  │  │ Chunk (2MB)       │  │    │
│  │  │ • Constant Pool   │  │    │
│  │  │ • Annotations     │  │    │
│  │  │ • Methods         │  │    │
│  │  └───────────────────┘  │    │
│  │  ...                    │    │
│  └─────────────────────────┘    │
│                                 │
│  ┌─────────────────────────┐    │
│  │   Class Metaspace       │    │
│  │  (Compressed Class      │    │
│  │   Space, if enabled)    │    │
│  │  • Klass structures     │    │
│  │  • vtables              │    │
│  │  • itables              │    │
│  └─────────────────────────┘    │
└─────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Metaspace Structure&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Simplified Metaspace structure in HotSpot&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Metaspace&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Arena-based allocator&lt;/span&gt;
    &lt;span class="n"&gt;Metachunk&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_chunks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// List of chunks&lt;/span&gt;

    &lt;span class="c1"&gt;// Statistics&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;_used_words&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;_capacity_words&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;_committed_words&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Metadata chunk&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Metachunk&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Header&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;_word_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Metablock&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_blocks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Type: Non-Class (methods, constants) or Class (Klass)&lt;/span&gt;
    &lt;span class="n"&gt;MetaspaceType&lt;/span&gt; &lt;span class="n"&gt;_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;ClassLoader Leak - Main Cause of OOM: Metaspace&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClassLoaderLeak&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Web application, reloaded in Tomcat&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;leak&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// 1. Create isolated ClassLoader&lt;/span&gt;
            &lt;span class="nc"&gt;URLClassLoader&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLClassLoader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;[]{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file:///app.jar"&lt;/span&gt;&lt;span class="o"&gt;)},&lt;/span&gt;
                &lt;span class="kc"&gt;null&lt;/span&gt;  &lt;span class="c1"&gt;// Parent = null (isolation)&lt;/span&gt;
            &lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// 2. Load class&lt;/span&gt;
            &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;clazz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;loadClass&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.example.SomeClass"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clazz&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// 3. Store reference somewhere global&lt;/span&gt;
            &lt;span class="nc"&gt;GlobalCache&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// LEAK!&lt;/span&gt;

            &lt;span class="c1"&gt;// 4. ClassLoader cannot be unloaded,&lt;/span&gt;
            &lt;span class="c1"&gt;//    because its classes are reachable through instance&lt;/span&gt;
            &lt;span class="c1"&gt;//    → Metaspace grows with each reload&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ClassLoader leak diagnostics&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Check number of ClassLoaders&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; VM.classloader_stats

&lt;span class="c"&gt;# 2. Dump classes&lt;/span&gt;
jmap &lt;span class="nt"&gt;-clstats&lt;/span&gt; &amp;lt;pid&amp;gt;

&lt;span class="c"&gt;# 3. Enable class loading logging&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+TraceClassLoading &lt;span class="nt"&gt;-XX&lt;/span&gt;:+TraceClassUnloading

&lt;span class="c"&gt;# 4. Limit Metaspace&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxMetaspaceSize&lt;span class="o"&gt;=&lt;/span&gt;256m
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MetaspaceSize&lt;span class="o"&gt;=&lt;/span&gt;64m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 5: STRING POOL AND INTERNING&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;String Pool: Hash Table in Heap&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Internal String Pool implementation (StringTable)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringTable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Hash table with separate chaining&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Main intern() method&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hashString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;str&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;str&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Existing string&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Adding new string&lt;/span&gt;
        &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;newEntry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newEntry&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;String Pool Evolution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Java 6 and earlier&lt;/strong&gt;: In PermGen, fixed size, not cleared&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-XX&lt;/span&gt;:StringTableSize&lt;span class="o"&gt;=&lt;/span&gt;1009  &lt;span class="c"&gt;# Small and fixed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Java 7+&lt;/strong&gt;: In Heap, dynamic size, cleared by GC&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-XX&lt;/span&gt;:StringTableSize&lt;span class="o"&gt;=&lt;/span&gt;60013  &lt;span class="c"&gt;# Size can be configured&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;When to use intern()?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Anti-pattern&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// NEVER DO THIS&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;interned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// All strings in pool!&lt;/span&gt;
    &lt;span class="c1"&gt;// Pool fills up, GC won't help&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Possibly correct usage&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TokenProcessor&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Limited set of known tokens&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;KNOWN_TOKENS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"PUT"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"DELETE"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"HEAD"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;String:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toSet&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Frequently used enum-like values&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpMethod&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Only 6 possible values&lt;/span&gt;
        &lt;span class="c1"&gt;// Fast comparison via ==&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// SAFE: "GET" guaranteed interned&lt;/span&gt;
            &lt;span class="c1"&gt;// ...&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CSV parser optimization&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CSVParser&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;internIfFrequent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Strategy: intern only frequently repeating values&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Long strings not interned&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Add only if occurs frequently&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shouldIntern&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;interned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;interned&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;interned&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 6: JIT COMPILATION - C1, C2, ADAPTIVE OPTIMIZATIONS&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Three-Tier Compilation (Tiered Compilation)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────┐
│           INTERPRETER (Level 0)                 │
│  • Zero startup overhead                        │
│  • Slow execution                               │
│  • Profile collection: counters, types, branches│
└─────────────────┬───────────────────────────────┘
                  ↓ (1000+ method calls)
┌─────────────────────────────────────────────────┐
│           C1 (CLIENT) COMPILER                  │
│  • Fast compilation (level 1 optimizations)     │
│  • Inlining small methods                       │
│  • Local optimizations                          │
│  • Continue profile collection                  │
└─────────────────┬───────────────────────────────┘
                  ↓ (10000+ method calls)
┌─────────────────────────────────────────────────┐
│           C2 (SERVER) COMPILER                  │
│  • Aggressive optimizations (level 4)           │
│  • Global data flow analysis                    │
│  • Escape Analysis and Scalar Replacement       │
│  • Devirtualization and inlining                │
│  • Vectorization (Auto-Vectorization)           │
└─────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Compilation Configuration&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Compilation levels (0-4)&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:CompileThreshold&lt;span class="o"&gt;=&lt;/span&gt;10000        &lt;span class="c"&gt;# Threshold for C2&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:Tier3InvocationThreshold&lt;span class="o"&gt;=&lt;/span&gt;2000 &lt;span class="c"&gt;# For C1-&amp;gt;C2&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:Tier4InvocationThreshold&lt;span class="o"&gt;=&lt;/span&gt;15000

&lt;span class="c"&gt;# Cache sizes&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ReservedCodeCacheSize&lt;span class="o"&gt;=&lt;/span&gt;240m    &lt;span class="c"&gt;# Native code cache&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:InitialCodeCacheSize&lt;span class="o"&gt;=&lt;/span&gt;160m

&lt;span class="c"&gt;# Compiler control&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+TieredCompilation           &lt;span class="c"&gt;# Enable multi-tier (default)&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:-TieredCompilation          &lt;span class="c"&gt;# Only C2 (slower start)&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:CompileCommand&lt;span class="o"&gt;=&lt;/span&gt;exclude,com/example/expensiveMethod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Profiling and Devirtualization&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DevirtualizationExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;totalArea&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;shapes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;shapes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;area&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Virtual call&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optimization process&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Interpreter&lt;/strong&gt;: Collects type profile

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Shape#area()&lt;/code&gt;: 95% Circle, 5% Square&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C1 compiler&lt;/strong&gt;: Adds type check
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;area&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Direct call&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;area&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Virtual call&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;C2 compiler&lt;/strong&gt;: If profile is stable

&lt;ul&gt;
&lt;li&gt;Creates two specialized loop versions&lt;/li&gt;
&lt;li&gt;For Circle: completely removes checks&lt;/li&gt;
&lt;li&gt;For Square: separate rare path&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Escape Analysis and Scalar Replacement&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;compute&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// NoEscape: doesn't leave method&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// After Scalar Replacement:&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;compute_optimized&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Point object not created!&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;p_x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Field decomposed into local variable&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;p_y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Second field decomposed&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p_x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p_y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Application conditions&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;NoEscape&lt;/strong&gt;: Object not passed outside method&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ArgEscape&lt;/strong&gt;: Passed, but not published&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GlobalEscape&lt;/strong&gt;: Published (not optimized)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Enable/Disable&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-XX&lt;/span&gt;:+DoEscapeAnalysis      &lt;span class="c"&gt;# Enable (default)&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+EliminateAllocations  &lt;span class="c"&gt;# Scalar Replacement (default)&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintEscapeAnalysis   &lt;span class="c"&gt;# Logging&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 7: VOLATILE AND JAVA MEMORY MODEL&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Java Memory Model (JMM)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Happens-before rules&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Program order&lt;/strong&gt;: Actions in a thread happen in program order&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor lock&lt;/strong&gt;: Releasing a monitor happens-before subsequent acquisition&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Volatile&lt;/strong&gt;: Write to volatile happens-before read of same field&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread start&lt;/strong&gt;: &lt;code&gt;Thread.start()&lt;/code&gt; happens-before any actions in the thread&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread join&lt;/strong&gt;: All actions in a thread happen-before &lt;code&gt;Thread.join()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transitivity&lt;/strong&gt;: If A happens-before B and B happens-before C, then A happens-before C&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Volatile Implementation at Processor Level&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VolatileExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// (1) Normal write&lt;/span&gt;
        &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// (2) Volatile write&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;          &lt;span class="c1"&gt;// (3) Volatile read&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (4) Will see 42&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Memory barriers for x86&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; writer()
mov    [data], 42        ; Store data
; StoreStore barrier (x86 doesn't require)
mov    [flag], 1         ; Store flag (volatile)
sfence                   ; StoreLoad barrier (x86 requires)

; reader()
lfence                   ; LoadLoad barrier (x86 requires)
mov    rax, [flag]       ; Load flag (volatile)
test   rax, rax
jz     .done
; LoadStore barrier (x86 doesn't require)
mov    rbx, [data]       ; Load data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;False Sharing and @Contended&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;False sharing problem&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FalseSharing&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Two fields in one cache line (64 bytes)&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// [0-7]&lt;/span&gt;
    &lt;span class="c1"&gt;// ... 56 bytes ...&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// [56-63]&lt;/span&gt;

    &lt;span class="c1"&gt;// Thread 1: constantly writes to value1&lt;/span&gt;
    &lt;span class="c1"&gt;// Thread 2: constantly reads value2&lt;/span&gt;
    &lt;span class="c1"&gt;// RESULT: cache line constantly invalidated&lt;/span&gt;
    &lt;span class="c1"&gt;// → performance drops significantly&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution with @Contended&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaddedData&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// JVM will add 128 bytes padding on each side&lt;/span&gt;
    &lt;span class="nd"&gt;@Contended&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Contended&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Memory layout:&lt;/span&gt;
    &lt;span class="c1"&gt;// [value1][128 bytes padding][... other fields ...][128 bytes padding][value2]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Manual solution (pre-Java 8)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ManualPadding&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Explicit padding&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 56 bytes&lt;/span&gt;

    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;p8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p13&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Another 56 bytes&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;False sharing diagnostics&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Linux: perf for cache miss monitoring&lt;/span&gt;
perf &lt;span class="nb"&gt;stat&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; cache-misses,cache-references java &lt;span class="nt"&gt;-jar&lt;/span&gt; app.jar

&lt;span class="c"&gt;# JVM flags for @Contended&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:-RestrictContended        &lt;span class="c"&gt;# Allow use outside java.base&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ContendedPaddingWidth&lt;span class="o"&gt;=&lt;/span&gt;128 &lt;span class="c"&gt;# Padding size (default 128)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 8: PROFILING AND OPTIMIZATION IN PRACTICE&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Scenario: High-Load Service&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Initial state&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100k RPS, 95th percentile 200ms, heap 8GB&lt;/li&gt;
&lt;li&gt;Frequent Full GC pauses 2-3 seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Data collection&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. JFR for pause analysis&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; JFR.start &lt;span class="nv"&gt;duration&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;60s &lt;span class="nv"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gc.jfr

&lt;span class="c"&gt;# 2. Detailed GC logs&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintGCDetails &lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintGCDateStamps &lt;span class="nt"&gt;-Xloggc&lt;/span&gt;:gc.log

&lt;span class="c"&gt;# 3. Heap dump just before Full GC&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+HeapDumpBeforeFullGC &lt;span class="nt"&gt;-XX&lt;/span&gt;:HeapDumpPath&lt;span class="o"&gt;=&lt;/span&gt;/path/to/dumps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Analysis&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Typical problems:&lt;/span&gt;
&lt;span class="c1"&gt;// 1. Too large Young/Old ratio&lt;/span&gt;
&lt;span class="c1"&gt;// 2. Frequent promotions due to large Survivor&lt;/span&gt;
&lt;span class="c1"&gt;// 3. Memory leak in caches&lt;/span&gt;
&lt;span class="c1"&gt;// 4. Too aggressive allocation rate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Optimization&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Switch to G1 GC&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+UseG1GC
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxGCPauseMillis&lt;span class="o"&gt;=&lt;/span&gt;100
&lt;span class="nt"&gt;-XX&lt;/span&gt;:InitiatingHeapOccupancyPercent&lt;span class="o"&gt;=&lt;/span&gt;35  &lt;span class="c"&gt;# Start concurrent cycle earlier&lt;/span&gt;

&lt;span class="c"&gt;# Tune Young Gen&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:NewRatio&lt;span class="o"&gt;=&lt;/span&gt;1                          &lt;span class="c"&gt;# More Young for short-lived&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:SurvivorRatio&lt;span class="o"&gt;=&lt;/span&gt;6                     &lt;span class="c"&gt;# More Eden&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxTenuringThreshold&lt;span class="o"&gt;=&lt;/span&gt;5              &lt;span class="c"&gt;# Faster promotion for medium-lived&lt;/span&gt;

&lt;span class="c"&gt;# Monitoring&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintAdaptiveSizePolicy            &lt;span class="c"&gt;# How JVM tunes sizes&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintTenuringDistribution          &lt;span class="c"&gt;# Age distribution&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Anti-patterns and Their Fixes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Anti-pattern 1: Manual System.gc()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BAD&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processBatch&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gc&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Full GC pause at unpredictable moment&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Solution: rely on JVM or use&lt;/span&gt;
&lt;span class="c1"&gt;// -XX:+ExplicitGCInvokesConcurrent for G1&lt;/span&gt;
&lt;span class="c1"&gt;// -XX:+DisableExplicitGC in production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Anti-pattern 2: Large arrays in Young Gen&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BAD: 2MB array in Eden&lt;/span&gt;
&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Solution: direct allocator or tuning&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nl"&gt;XX:&lt;/span&gt;&lt;span class="nc"&gt;PretenureSizeThreshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="no"&gt;M&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="no"&gt;MB&lt;/span&gt; &lt;span class="n"&gt;directly&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nc"&gt;OldGen&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Anti-pattern 3: String concat in loop&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BAD: O(n²) memory&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// New StringBuilder each time&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Solution:&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;estimatedSize&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 9: SPECIFIC CONFIGURATIONS FOR DIFFERENT SCENARIOS&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Microservice (REST API, 4GB heap)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# G1 with aggressive latency goals&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+UseG1GC
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxGCPauseMillis&lt;span class="o"&gt;=&lt;/span&gt;50
&lt;span class="nt"&gt;-XX&lt;/span&gt;:G1HeapRegionSize&lt;span class="o"&gt;=&lt;/span&gt;4M
&lt;span class="nt"&gt;-XX&lt;/span&gt;:InitiatingHeapOccupancyPercent&lt;span class="o"&gt;=&lt;/span&gt;30
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ConcGCThreads&lt;span class="o"&gt;=&lt;/span&gt;2
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ParallelGCThreads&lt;span class="o"&gt;=&lt;/span&gt;4

&lt;span class="c"&gt;# Metaspace limits&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxMetaspaceSize&lt;span class="o"&gt;=&lt;/span&gt;128M
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MetaspaceSize&lt;span class="o"&gt;=&lt;/span&gt;64M

&lt;span class="c"&gt;# JIT settings&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ReservedCodeCacheSize&lt;span class="o"&gt;=&lt;/span&gt;128M
&lt;span class="nt"&gt;-XX&lt;/span&gt;:InitialCodeCacheSize&lt;span class="o"&gt;=&lt;/span&gt;64M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Batch Data Processing (32GB heap)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Throughput oriented&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+UseParallelGC
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+UseParallelOldGC
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ParallelGCThreads&lt;span class="o"&gt;=&lt;/span&gt;8
&lt;span class="nt"&gt;-XX&lt;/span&gt;:GCTimeRatio&lt;span class="o"&gt;=&lt;/span&gt;99
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxGCPauseMillis&lt;span class="o"&gt;=&lt;/span&gt;500

&lt;span class="c"&gt;# Large objects&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:PretenureSizeThreshold&lt;span class="o"&gt;=&lt;/span&gt;10M
&lt;span class="nt"&gt;-XX&lt;/span&gt;:SurvivorRatio&lt;span class="o"&gt;=&lt;/span&gt;10

&lt;span class="c"&gt;# Monitoring&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintGCDetails
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+PrintGCApplicationStoppedTime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Low-Latency System (Financial Transactions)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# ZGC for sub-millisecond pauses&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:+UseZGC
&lt;span class="nt"&gt;-XX&lt;/span&gt;:MaxGCPauseMillis&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="nt"&gt;-XX&lt;/span&gt;:ConcGCThreads&lt;span class="o"&gt;=&lt;/span&gt;4
&lt;span class="nt"&gt;-Xmx16g&lt;/span&gt;
&lt;span class="nt"&gt;-Xms16g&lt;/span&gt;  &lt;span class="c"&gt;# Fixed heap&lt;/span&gt;

&lt;span class="c"&gt;# Disable bias locking for stability&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:-UseBiasedLocking

&lt;span class="c"&gt;# Aggressive JIT compilation&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:-TieredCompilation  &lt;span class="c"&gt;# Only C2&lt;/span&gt;
&lt;span class="nt"&gt;-XX&lt;/span&gt;:CompileThreshold&lt;span class="o"&gt;=&lt;/span&gt;1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;PART 10: MONITORING AND DIAGNOSTICS IN REAL TIME&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Utilities and Their Purpose&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;jcmd - universal command&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Full list of available commands&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; &lt;span class="nb"&gt;help&lt;/span&gt;

&lt;span class="c"&gt;# Heap dump&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; GC.heap_dump &lt;span class="nv"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;heap.hprof

&lt;span class="c"&gt;# Class status&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; GC.class_histogram

&lt;span class="c"&gt;# JFR management&lt;/span&gt;
jcmd &amp;lt;pid&amp;gt; JFR.start &lt;span class="nv"&gt;duration&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;60s &lt;span class="nv"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;recording.jfr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;jstat - GC statistics&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Every second, 10 times&lt;/span&gt;
jstat &lt;span class="nt"&gt;-gc&lt;/span&gt; &amp;lt;pid&amp;gt; 1s 10

&lt;span class="c"&gt;# Key metrics:&lt;/span&gt;
&lt;span class="c"&gt;# S0C/S1C: Survivor capacity&lt;/span&gt;
&lt;span class="c"&gt;# S0U/S1U: Survivor used&lt;/span&gt;
&lt;span class="c"&gt;# EC/EU: Eden capacity/used&lt;/span&gt;
&lt;span class="c"&gt;# OC/OU: Old capacity/used&lt;/span&gt;
&lt;span class="c"&gt;# YGC/YGCT: Young GC count/time&lt;/span&gt;
&lt;span class="c"&gt;# FGC/FGCT: Full GC count/time&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;async-profiler - low-level profiler&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# CPU profiling&lt;/span&gt;
./profiler.sh &lt;span class="nt"&gt;-d&lt;/span&gt; 30 &lt;span class="nt"&gt;-f&lt;/span&gt; cpu.svg &amp;lt;pid&amp;gt;

&lt;span class="c"&gt;# Allocation profiling&lt;/span&gt;
./profiler.sh &lt;span class="nt"&gt;-d&lt;/span&gt; 30 &lt;span class="nt"&gt;-e&lt;/span&gt; alloc &lt;span class="nt"&gt;-f&lt;/span&gt; alloc.svg &amp;lt;pid&amp;gt;

&lt;span class="c"&gt;# Contended lock profiling&lt;/span&gt;
./profiler.sh &lt;span class="nt"&gt;-d&lt;/span&gt; 30 &lt;span class="nt"&gt;-e&lt;/span&gt; lock &lt;span class="nt"&gt;-f&lt;/span&gt; lock.svg &amp;lt;pid&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Configuring GC Logs for Analysis&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Detailed logs with timestamps&lt;/span&gt;
&lt;span class="nt"&gt;-Xlog&lt;/span&gt;:gc&lt;span class="k"&gt;*&lt;/span&gt;,gc+age&lt;span class="o"&gt;=&lt;/span&gt;trace,gc+heap&lt;span class="o"&gt;=&lt;/span&gt;debug:file&lt;span class="o"&gt;=&lt;/span&gt;gc.log:uptime,level,tags

&lt;span class="c"&gt;# For G1 specifically&lt;/span&gt;
&lt;span class="nt"&gt;-Xlog&lt;/span&gt;:gc+g1&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;debug,gc+phases&lt;span class="o"&gt;=&lt;/span&gt;debug:file&lt;span class="o"&gt;=&lt;/span&gt;g1.log

&lt;span class="c"&gt;# Parsing logs with utilities&lt;/span&gt;
&lt;span class="c"&gt;# 1. GCViewer: visualization&lt;/span&gt;
&lt;span class="c"&gt;# 2. gceasy.io: online analysis&lt;/span&gt;
&lt;span class="c"&gt;# 3. jClarity Censum: commercial tool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>11 Fundamental Programming Concepts</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:46:59 +0000</pubDate>
      <link>https://dev.to/dima853/11-fundamental-programming-concepts-42d2</link>
      <guid>https://dev.to/dima853/11-fundamental-programming-concepts-42d2</guid>
      <description>&lt;p&gt;If you’re new to programming, the sheer number of new terms can feel overwhelming. But here’s the secret: every complex program is built from just a handful of simple ideas. Once you understand these 11 concepts, you’ll have a solid foundation for any language you decide to learn.&lt;/p&gt;

&lt;p&gt;I’ve put together a set of &lt;strong&gt;interactive puzzles&lt;/strong&gt; that let you play with each idea directly in the browser – no setup required. After reading each explanation, click the link to solve a quick puzzle and lock in your understanding.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Note:&lt;/strong&gt; This post is a beginner-friendly introduction to the core concepts of programming. It's intentionally simplified to help you build a mental model without getting lost in details. In reality, each of these topics has depth and nuance that entire books are dedicated to. This is just the first step — a foundation that will make it easier to understand the more complex ideas later.
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. What is a Variable?
&lt;/h2&gt;

&lt;p&gt;Think of a variable as a labeled box. You write a name on the box (like &lt;code&gt;shoes&lt;/code&gt; or &lt;code&gt;favoriteNumber&lt;/code&gt;) and then you can put something inside it, look inside, or change the contents – all without changing the label.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  You &lt;strong&gt;declare&lt;/strong&gt; the box by giving it a name.&lt;/li&gt;
&lt;li&gt;  You &lt;strong&gt;assign&lt;/strong&gt; a value by putting something in it (like &lt;code&gt;x = 10&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  You can &lt;strong&gt;change&lt;/strong&gt; the value later (like &lt;code&gt;x = 20&lt;/code&gt;). The old value is gone, but the name stays the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why it’s called a "variable" – its content can vary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/what-is-a-variable"&gt;Solve the puzzle: What is a variable?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Numbers vs Strings: Why &lt;code&gt;1 + 1 = 2&lt;/code&gt; but &lt;code&gt;"1" + "1" = "11"&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Computers treat different kinds of data differently. The two most basic kinds are &lt;strong&gt;numbers&lt;/strong&gt; and &lt;strong&gt;strings&lt;/strong&gt; (text).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Numbers&lt;/strong&gt; are for math. If you write &lt;code&gt;5&lt;/code&gt; without quotes, the computer knows it can add, subtract, multiply, and divide it. So &lt;code&gt;1 + 1&lt;/code&gt; equals &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Strings&lt;/strong&gt; are for text. They are always wrapped in quotes, like &lt;code&gt;"hello"&lt;/code&gt; or &lt;code&gt;"5"&lt;/code&gt;. Even if there are digits inside the quotes, the computer sees them as characters, not numbers. The &lt;code&gt;+&lt;/code&gt; sign with strings means "glue together". So &lt;code&gt;"1" + "1"&lt;/code&gt; becomes &lt;code&gt;"11"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you put a number in quotes, you turn it into a string and it loses its mathematical powers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/numbers-vs-strings"&gt;Solve the puzzle: Numbers vs Strings&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Conditionals: if/else – Making Decisions
&lt;/h2&gt;

&lt;p&gt;Programs often need to choose between two paths. That's exactly what &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; do.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;if&lt;/code&gt; checks a condition that is either &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt; (like &lt;code&gt;age &amp;gt;= 18&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  If the condition is true, the computer runs the block of code inside the &lt;code&gt;if&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  If the condition is false, the computer skips the &lt;code&gt;if&lt;/code&gt; block and runs the &lt;code&gt;else&lt;/code&gt; block instead (if there is one).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works just like a real‑life decision: "If it's raining, take an umbrella. Otherwise, leave it at home."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/if-else-decisions"&gt;Solve the puzzle: if/else decisions&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Loops: for and while – Repeating Actions
&lt;/h2&gt;

&lt;p&gt;Doing the same thing over and over is what computers are best at. Loops let you repeat a block of code without writing it again.&lt;br&gt;
Computers repeat actions perfectly because their processors consist of electronic switches that execute billions of mathematical commands per second without fatigue or errors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;for&lt;/code&gt; loop:&lt;/strong&gt; Use this when you know exactly how many times you want to repeat something. It's like telling someone "count from 1 to 100".&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;while&lt;/code&gt; loop:&lt;/strong&gt; Use this when you want to repeat something &lt;strong&gt;as long as a condition is true&lt;/strong&gt;. It's like "keep eating soup while the bowl is not empty".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be careful with &lt;code&gt;while&lt;/code&gt;: if the condition never becomes false, the loop runs forever and your program freezes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/for-while-loops"&gt;Solve the puzzle: for and while loops&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  5. What is a Function?
&lt;/h2&gt;

&lt;p&gt;A function is a reusable block of code with a name. Instead of copying and pasting the same code everywhere, you write it once and then &lt;strong&gt;call&lt;/strong&gt; it by its name whenever you need it.&lt;/p&gt;

&lt;p&gt;It's like a "make coffee" button on a coffee machine. You press the button (call the function), and the machine runs through the whole process: grind beans, heat water, pour coffee. You don't need to know the details, you just use the button.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Define&lt;/strong&gt; the function: &lt;code&gt;def greet(): print("Hello!")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Call&lt;/strong&gt; the function: &lt;code&gt;greet()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/what-is-a-function"&gt;Solve the puzzle: What is a function?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  6. Function Arguments and Return Values
&lt;/h2&gt;

&lt;p&gt;Functions become truly powerful when you can give them input and get output back.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Arguments (parameters)&lt;/strong&gt; are the input you give to a function. They act like placeholders. When you call &lt;code&gt;add(3, 4)&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; are the arguments.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Return value&lt;/strong&gt; is what the function sends back. The &lt;code&gt;return&lt;/code&gt; keyword stops the function and hands a value back to the caller.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of a blender: you put in fruit (arguments), it blends (the function body), and it returns a smoothie (the return value). You can then pour that smoothie into a glass (assign to a variable).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/function-arguments-return"&gt;Solve the puzzle: arguments and return&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  7. What is an Array (List)?
&lt;/h2&gt;

&lt;p&gt;A single variable holds one thing. But what if you need to store a whole shopping list? That's what an &lt;strong&gt;array&lt;/strong&gt; (or list) is for.&lt;/p&gt;

&lt;p&gt;It's a single name that holds multiple items in a numbered sequence. The items are stored in order, and you access them by their &lt;strong&gt;index&lt;/strong&gt; (position number). Importantly, counting starts at &lt;strong&gt;0&lt;/strong&gt;, so the first item is at index &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cherry&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# "apple"
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# "cherry"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrays let you store, access, and modify collections of data in an organized way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/what-is-an-array"&gt;Solve the puzzle: What is an array?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  8. What is an Algorithm?
&lt;/h2&gt;

&lt;p&gt;An algorithm is simply a step‑by‑step recipe to solve a problem or accomplish a task. You follow algorithms all the time: a cooking recipe, the instructions to assemble furniture, or the process of doing laundry.&lt;/p&gt;

&lt;p&gt;In programming, an algorithm is a sequence of clear, unambiguous steps that the computer can follow. To create one, you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Define the goal&lt;/strong&gt; (what should the result be?).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Break it down&lt;/strong&gt; into the tiniest logical steps.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Order the steps&lt;/strong&gt; correctly – some things must happen before others.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Execute&lt;/strong&gt; them one by one.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The computer doesn't "think"; it just blindly follows your instructions, so every step must be precise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/what-is-an-algorithm"&gt;Solve the puzzle: What is an algorithm?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Syntax Errors vs Logic Errors (Bugs)
&lt;/h2&gt;

&lt;p&gt;Not all errors are created equal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Syntax errors&lt;/strong&gt; are like spelling and grammar mistakes. You forgot a quote, a bracket, or misspelled &lt;code&gt;print&lt;/code&gt;. The computer immediately yells at you and refuses to run the program until you fix it.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Logic errors&lt;/strong&gt; are the sneaky ones. The code runs perfectly – no error messages – but the result is wrong. For example, you wrote &lt;code&gt;price + tax&lt;/code&gt; instead of &lt;code&gt;price - tax&lt;/code&gt;. The computer did exactly what you told it, but your instruction was wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finding logic errors is called &lt;strong&gt;debugging&lt;/strong&gt; – you play detective, check your variables, and trace through your steps until you find where your thinking went wrong.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/syntax-vs-logic-errors"&gt;Solve the puzzle: Syntax vs Logic errors&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Compiler vs Interpreter
&lt;/h2&gt;

&lt;p&gt;Your high‑level code (Python, JavaScript, C++) can't run directly on the processor. It needs a translator.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A &lt;strong&gt;compiler&lt;/strong&gt; takes your entire program at once, translates it into a separate executable file (like an &lt;code&gt;.exe&lt;/code&gt;), and then you run that file later. It's like translating a whole book and then printing it. Examples: C++, Go, Rust.&lt;/li&gt;
&lt;li&gt;  An &lt;strong&gt;interpreter&lt;/strong&gt; translates your code line‑by‑line &lt;strong&gt;as it runs&lt;/strong&gt;. There's no separate file; the translator is always there during execution. It's like a live interpreter at a meeting. Examples: Python, JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compiled programs usually run faster, while interpreted programs are easier to test quickly because you don't have to wait for a full build.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/compiler-vs-interpreter"&gt;Solve the puzzle: Compiler vs Interpreter&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  11. What are Comments in Code?
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to leave a note for yourself or another developer without the computer trying to run it as code. That's a &lt;strong&gt;comment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You mark a line as a comment with a special symbol (like &lt;code&gt;#&lt;/code&gt; in Python or &lt;code&gt;//&lt;/code&gt; in JavaScript). The translator completely ignores that line – it's only for humans.&lt;/p&gt;

&lt;p&gt;Developers use comments to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Explain &lt;strong&gt;why&lt;/strong&gt; the code is written a certain way.&lt;/li&gt;
&lt;li&gt;  Leave reminders or warnings ("Don't change this formula!").&lt;/li&gt;
&lt;li&gt;  Temporarily disable a line of code while debugging (called "commenting out").&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good comments clarify intent; they don't just repeat what the code already says.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://dev.to/en/puzzles/what-are-comments"&gt;Solve the puzzle: What are comments?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Keep Exploring
&lt;/h2&gt;

&lt;p&gt;These 11 concepts are the bedrock of every program you'll ever write. If you've read through them and solved the puzzles, you're already ahead of most beginners. Next, pick a language and start building small projects – you'll be surprised how much you can already do. Happy coding!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Why is all this needed? PRG | PRF | GGM</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:46:35 +0000</pubDate>
      <link>https://dev.to/dima853/why-is-all-this-needed-prg-prf-ggm-277c</link>
      <guid>https://dev.to/dima853/why-is-all-this-needed-prg-prf-ggm-277c</guid>
      <description>&lt;p&gt;&lt;em&gt;This article may have inaccuracies, study this to get a general understanding of what it is.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is a task: to encrypt data, random bits are needed. Lots of them. Generating true randomness is a slow and expensive thing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;...a slow and expensive thing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clarification:&lt;/strong&gt; This is true for &lt;em&gt;high-quality&lt;/em&gt; entropy. Modern CPUs have instructions like RDRAND, but for cryptography, multiple entropy sources (timings, ADC noise) are often combined, which indeed requires time to accumulate a sufficient number of bits.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the same time, in many algorithms (encryption, authentication) we don't need randomness "in a vacuum" — we need the result to &lt;em&gt;look&lt;/em&gt; random to anyone who doesn't have the secret key.&lt;/p&gt;

&lt;p&gt;This gives rise to two basic concepts.&lt;/p&gt;




&lt;h3&gt;
  
  
  PRG — Stretching Randomness
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: we have a small amount of "true" randomness (e.g., 256 bits), but need a lot (gigabytes).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: take a short random &lt;strong&gt;seed&lt;/strong&gt; and pass it through a deterministic algorithm that outputs a long pseudorandom sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirement&lt;/strong&gt;: the output must be &lt;em&gt;computationally&lt;/em&gt; indistinguishable from a truly random string for anyone who doesn't know the seed. No statistical anomalies, no patterns.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The key word is "computationally" - meaning there is no efficient algorithm that can distinguish them in polynomial time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: if you are given two sheets with numbers — one with PRG output, the other with true randomness — you won't be able to tell which is which.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember: truly random bits are the gold standard we aim for when creating cryptographic keys and seeds. PRG and PRF are cryptographic primitives that must be computationally indistinguishable from this standard for anyone without the secret key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: if the seed is known, the entire pseudorandom sequence can be reproduced. Security relies on the secrecy of the seed and the &lt;strong&gt;strength of the PRG itself&lt;/strong&gt;. If the PRG is vulnerable, the output can be predicted even with a secret seed.&lt;/p&gt;




&lt;h3&gt;
  
  
  PRF — Pseudorandom Functions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: we need not just to generate numbers, but to get a "random" result for each input. For example, for message authentication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: a function that, given a key and an arbitrary message, outputs a pseudorandom value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirement&lt;/strong&gt;: even knowing millions of "input-output" pairs, one cannot predict the value for a new input without the key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: you have a magic box with a setting (the key). For each query, it gives a random-looking answer. Even if you've seen millions of answers to other queries, you won't guess the answer to a new one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: PRF is a cryptographic function family that behaves almost indistinguishably from a perfectly random function for anyone without the key. MACs, key derivation schemes, and much more are built on PRFs.&lt;/p&gt;




&lt;h3&gt;
  
  
  GGM — The Bridge Between PRG and PRF
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Intuition&lt;/strong&gt;: if we have a PRG (can stretch randomness), can we make a PRF (a function with arbitrary input) from it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It turns out, yes&lt;/strong&gt; — via the Goldreich-Goldwasser-Micali tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take a PRG that turns one seed into two unpredictable ones&lt;/li&gt;
&lt;li&gt;Build a binary tree where the root is our secret key&lt;/li&gt;
&lt;li&gt;To compute the value for a message &lt;code&gt;x&lt;/code&gt;, traverse the tree: each bit of &lt;code&gt;x&lt;/code&gt; tells which branch to take&lt;/li&gt;
&lt;li&gt;The result is the leaf we end up at&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Simpler&lt;/strong&gt;: turn the sequence of message bits into a path through the tree, applying the PRG at each step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: GGM is primarily a &lt;strong&gt;theoretical construction&lt;/strong&gt; that &lt;strong&gt;proves in principle&lt;/strong&gt; that any secure PRG can be used to build a secure PRF. In practice, more efficient constructions are used (HMAC, CMAC), but GGM is important for security proofs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Clarification:&lt;/strong&gt; In GGM, the tree is not built physically, but virtually — we compute the path on the fly, saving memory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;PRG&lt;/strong&gt; is a static generator. You give it a seed, you get a long predetermined sequence. Everything is predictable in advance after initialization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PRF&lt;/strong&gt; is a function that can adapt. You give it a key and an arbitrary message — you get a unique result depending on that message.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GGM&lt;/strong&gt; creates this adaptability from a PRG via a tree. Each bit of the input message tells the PRG which branch of the tree to go to next. Thus, different messages lead to different endpoints in the tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PRF&lt;/strong&gt; is needed precisely for this adaptability — so that one key can generate different unpredictable results for different inputs, rather than just producing a static data stream like a PRG.&lt;/p&gt;

&lt;p&gt;it's needed for &lt;em&gt;variability&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  The Bottom Line
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PRG&lt;/strong&gt; is needed to make a lot of pseudorandom data from a small random seed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PRF&lt;/strong&gt; is needed to get deterministic but random-looking results for any inputs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GGM&lt;/strong&gt; shows that these concepts are related: having a PRG, you can build a PRF&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All modern cryptography uses these ideas in one way or another. When you hear about HMAC, stream ciphers, authentication schemes — behind them are almost always either PRGs, PRFs, or their hybrids.&lt;/p&gt;

&lt;p&gt;This is not abstract theory — it's the foundation upon which everything else is built.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Fundamental Principles of Proxy Server Operation</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:46:02 +0000</pubDate>
      <link>https://dev.to/dima853/fundamental-principles-of-proxy-server-operation-3j7c</link>
      <guid>https://dev.to/dima853/fundamental-principles-of-proxy-server-operation-3j7c</guid>
      <description>&lt;h1&gt;
  
  
  Fundamental Principles of Proxy Server Operation
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;This article explains what proxies can and cannot do for your anonymity. You will learn the difference between HTTP/SOCKS/transparent proxies, learn how to choose the right type for your task, and understand why just changing an IP is not even close to sufficient for true anonymity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Introduction: The Principle of Intermediation in Computer Networks
&lt;/h3&gt;

&lt;p&gt;To understand what a proxy server is, one must first consider the standard model of interaction on the Internet without its participation. This model is known as the "direct connection client-server architecture."&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Standard Model (without a proxy):&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Your device (client)&lt;/strong&gt;, be it a computer, smartphone, or tablet, has a unique digital identifier — an IP address (Internet Protocol Address). This is your "network passport" that you present every time you go online.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you type a website address (e.g., &lt;code&gt;google.com&lt;/code&gt;) into your browser, your computer establishes a direct TCP connection with the server where that site is located.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your browser sends an HTTP request to the target server. &lt;strong&gt;Important point: the IP address itself is not part of the HTTP headers.&lt;/strong&gt; It is an element of a lower network layer (IP packet). However, a standard HTTP request contains headers such as &lt;strong&gt;Host&lt;/strong&gt;, &lt;strong&gt;User-Agent&lt;/strong&gt;, and, critically, when using a proxy, a service header &lt;code&gt;X-Forwarded-For&lt;/code&gt;[1] is often added, which is precisely intended to convey the client's original IP address. When using &lt;strong&gt;HTTPS&lt;/strong&gt;, the request content is encrypted, but the connection establishment information (IP addresses) remains visible at the network level.↙️&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔍 [1]&lt;code&gt;X-Forwarded-For&lt;/code&gt;: This is a de facto standard header used by proxy servers and load balancers to convey the client's IP address. The first IP address in the list is usually the user's real IP address.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server receives the request, processes it, and returns data back to your IP address, which the browser displays as a web page.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  | ☠️ Problem |
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The target server knows significantly more about you than it seems:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Network and system data:&lt;/strong&gt; It receives your real IP address (and consequently your approximate geographical location and internet provider), as well as detailed information about your software, extracted from the HTTP request headers (e.g., &lt;code&gt;User-Agent&lt;/code&gt; indicates the operating system, browser, and its version).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Digital Fingerprint (Browser Fingerprinting):&lt;/strong&gt; This is a deeper and more insidious data collection technique. The server executes code on your browser's side (typically in JavaScript) that allows it to gather a unique "portrait":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complete list of installed fonts&lt;/strong&gt; (which can be obtained via CSS or Canvas API).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Screen dimensions and parameters&lt;/strong&gt; (resolution, color depth, touch interface availability).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List of plugins and MIME types.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Temporal characteristics&lt;/strong&gt; (graphics subsystem performance, clock frequency).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection data&lt;/strong&gt; (list of time zones, language settings).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data from browser local storage (Local Storage, Session Storage, Cookies).&lt;/strong&gt; If you have visited the site before, it might have saved a unique identifier in your storage, which upon a revisit will uniquely link the new session to your old one, even if you changed your IP address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EXIF data&lt;/strong&gt; — this is a metadata standard (Exchangeable Image File Format) stored in digital photos and contains information about the shot and camera settings.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key takeaway:&lt;/strong&gt; A proxy server ONLY effectively masks your IP address and, to some extent, your provider's data, hahah. However, it does &lt;strong&gt;not protect&lt;/strong&gt; against digital fingerprinting methods and tracking through browser storage. Countering these threats requires additional tools: disabling JavaScript, using specialized anti-fingerprinting browsers, etc.&lt;/p&gt;

&lt;p&gt;⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣&lt;/p&gt;

&lt;h3&gt;
  
  
  Why a Proxy is NOT Anonymity (and often the illusion of it):
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. The Proxy Provider Knows EVERYTHING.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Your traffic goes through the chain: &lt;strong&gt;You -&amp;gt; Your ISP -&amp;gt; Proxy Server -&amp;gt; Target Site.&lt;/strong&gt;&lt;br&gt;
The owner of the proxy server sees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your real IP address.&lt;/li&gt;
&lt;li&gt;All your unencrypted traffic (logins, passwords, browsing history).&lt;/li&gt;
&lt;li&gt;Your connection times.&lt;/li&gt;
&lt;li&gt;Metadata.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If it's a commercial or state-owned proxy, it keeps logs. &lt;strong&gt;You are simply swapping one "witness" (the target site) for another, often more centralized and dangerous one (the proxy provider).&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;2. DNS Leaks.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A classic mistake. You configured a proxy in your browser for HTTP/HTTPS traffic, but domain name resolution requests (e.g., converting &lt;code&gt;google.com&lt;/code&gt; to an IP address) might bypass the proxy, going through your standard connection. In this case, your ISP and anyone listening on the network can see which sites you are visiting, even if the content itself loads through the proxy. SOCKS5 can proxy DNS, but this must be explicitly configured.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;3. WebRTC Leaks.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is a technology for video chats and P2P connections in the browser. Through a special JavaScript request, a site can force the browser to reveal your real, local &lt;strong&gt;(behind NAT)[2]&lt;/strong&gt; and public IP address, &lt;strong&gt;completely ignoring the proxy settings&lt;/strong&gt;. This can only be disabled manually in the browser settings.&lt;/p&gt;

&lt;p&gt;🔒 &lt;a href="https://github.com/dima853/tg/blob/main/DoH_1.md" rel="noopener noreferrer"&gt;Complete guide to protection against DNS and WebRTC leaks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;strong&gt;NAT (Network Address Translation)&lt;/strong&gt; — is not a protocol or a service, but a mechanism used by routers to solve one of the &lt;strong&gt;biggest problems of the early internet&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fc%2Fc7%2FNAT_Concept-en.svg%2F1920px-NAT_Concept-en.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fc%2Fc7%2FNAT_Concept-en.svg%2F1920px-NAT_Concept-en.svg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Network address translation between a private network and the Internet&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Briefly about the problem:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;IPv4 addresses ran out.&lt;/strong&gt; There are only about 4.3 billion of them, but there are many more devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NAT Solution:&lt;/strong&gt;&lt;br&gt;
The router creates a &lt;strong&gt;private network&lt;/strong&gt; (home, office) with "internal" addresses (e.g., 192.168.1.x). The router itself goes out to the internet with one "public" IP address.&lt;/p&gt;

&lt;p&gt;NAT substitutes the addresses of devices from the private network with its own "public" IP, remembering which internal device the connection belongs to in order to return the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple analogy:&lt;/strong&gt;&lt;br&gt;
It's like in an office of 100 employees, the secretary has one phone number. Everyone calls &lt;em&gt;from the office&lt;/em&gt; through the secretary, and they know which employee to transfer an incoming call to.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;4. Digital Fingerprint (Fingerprinting).&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A site identifies you not by IP, but by a unique combination of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Screen resolution, font list, plugins.&lt;/li&gt;
&lt;li&gt;Browser and OS version.&lt;/li&gt;
&lt;li&gt;Behavioral factors (typing rhythm, mouse movement).
This "fingerprint" will be the same, whether through a proxy or without. The proxy is powerless here.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;5. Behavioral Analysis.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Even if you logged in via a proxy, the analytics system on the site sees that "a user with IP 1.1.1.1 came from &lt;code&gt;google.com&lt;/code&gt; via the query 'how to hack the pentagon', immediately logged into the account &lt;code&gt;ivan_ivanov@mail.ru&lt;/code&gt; and started downloading instructions." The IP is different, but the person and their intentions are the same.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;6. Cookies and Local Storage.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The browser, as it stored your cookies and data in Local Storage, continues to send them to the sites. The proxy again has nothing to do with it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;"A proxy server changes the IP address visible to the target server. This creates a basic, primitive level of concealment, which is easily bypassed by modern tracking methods and is useless against targeted de-anonymization.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effective anonymity is a set of measures:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Trustless model:&lt;/strong&gt; Using systems like &lt;strong&gt;Tor&lt;/strong&gt; (where several independent nodes do not know the full route) or high-quality &lt;strong&gt;VPNs&lt;/strong&gt; with a No-Logs policy (which you are &lt;em&gt;forced&lt;/em&gt; to trust).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Briefly:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"No-Logs" VPN is not a myth, but requires verification. The key principle: &lt;strong&gt;you cannot hand over what you don't have (i.e., if you don't collect it in the first place.)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to look:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Jurisdiction:&lt;/strong&gt; Countries outside surveillance alliances (like the "14 Eyes"). Examples: Panama, British Virgin Islands, Seychelles.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Practical verification:&lt;/strong&gt; Trust only those whose "No-Logs" has been confirmed in court or by an audit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Examples (verified):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ExpressVPN&lt;/strong&gt; (BVI) — when servers were seized, no logs were found.
&lt;a href="https://proprivacy.com/privacy-news/expressvpn-cannot-hand-over-logs" rel="noopener noreferrer"&gt;https://proprivacy.com/privacy-news/expressvpn-cannot-hand-over-logs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mullvad VPN&lt;/strong&gt; (Sweden) — police left empty-handed during a raid.
&lt;a href="https://myseldon.com/ru/news/index/282486389" rel="noopener noreferrer"&gt;https://myseldon.com/ru/news/index/282486389&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Proton VPN&lt;/strong&gt; (Switzerland) — strong jurisdiction and audits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What to check (checklist):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jurisdiction&lt;/li&gt;
&lt;li&gt;Confirmed cases of no logs&lt;/li&gt;
&lt;li&gt;Independent audit&lt;/li&gt;
&lt;li&gt;Technologies like RAM-only servers&lt;/li&gt;
&lt;li&gt;Clean reputation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; There is no absolute guarantee, but choosing the right VPN minimizes the risk, as the provider &lt;strong&gt;physically and legally cannot&lt;/strong&gt; provide non-existent logs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Fighting fingerprinting:&lt;/strong&gt; Using browsers like Tor Browser, which "pretend" to be the same for all users.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Changing behavior:&lt;/strong&gt; Refraining from logging into your accounts, using separate sessions, being cautious with JavaScript.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Using just a public proxy for anonymity is like hiding your face with a paper bag while leaving a tag with your name and address on your back. It solves one narrow task (changing IP), but does not make you anonymous."&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Detailed Breakdown of the Waiter in a Restaurant Analogy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You (the client)&lt;/strong&gt; — is your computer with a running browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Waiter (the proxy)&lt;/strong&gt; — is the proxy server with its own IP address.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Kitchen (the web server)&lt;/strong&gt; — is the target server, for example, Google's servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Order (the HTTP request)&lt;/strong&gt; — is a technically formed data packet containing the URL, method (GET, POST), headers, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Dish (the HTTP response)&lt;/strong&gt; — is the HTML code of the page, images, CSS styles, and JavaScript files.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The waiter can change the order.&lt;/strong&gt; A proxy server is not always a "dumb" relay. It can modify the outgoing request. For example, it can:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remove or change certain headers (e.g., &lt;code&gt;Referer&lt;/code&gt;, which tells the server which page you came from).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add its own headers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compress outgoing data to save traffic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Block the request if it is directed to a prohibited resource (in corporate or ISP proxies).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The waiter can give you something you didn't ask for.&lt;/strong&gt; The proxy server might return not the current data from the server, but a cached copy it has, if it exists and hasn't expired. This is the very "traffic saving" that is rarely used on the client side today but is actively used on the ISP side to offload channels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Not all waiters are the same.&lt;/strong&gt; The analogy does not account for fundamentally different types of proxies (HTTP, SOCKS, transparent), which operate at different layers of the OSI network model. A SOCKS proxy, for example, is not a "waiter" but more like a "courier service" that can deliver any cargo (any type of network traffic), not just "dishes from the restaurant" (web traffic).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Why is this needed? Detailed analysis of use cases
&lt;/h3&gt;

&lt;p&gt;Using a proxy server is not a magical solution to all problems, but a specific tool for achieving certain network tasks.&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Bypassing Blocks (IP-banning, geo-restrictions)
&lt;/h4&gt;

&lt;p&gt;This is the most common and understandable use case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Technical blocking mechanism:&lt;/strong&gt; Most blocks on the internet are implemented based on IP addresses. A network administrator (in an office, school, country) or the owner of a web service (e.g., a streaming platform) compiles a blacklist of IP addresses or entire ranges. When a request comes from an IP address in this list, the server either does not respond or returns a blocking message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;How a proxy bypasses the block:&lt;/strong&gt; Since your request comes from the proxy server's IP address, which is not on the blacklist, the block does not trigger. The request is processed successfully.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Important nuances:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IP address quality.&lt;/strong&gt; If you use a public, free proxy, its IP address is highly likely already on the blacklists of many services, as it has been used for similar purposes before you.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Geolocation.&lt;/strong&gt; To bypass geo-restrictions (e.g., to watch a service available only in the USA), you must use a proxy server with an IP address located in the required country. The service determines your "location" by the geolocation of the proxy's IP address.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  2. Anonymity: Correcting the Understanding of the Term
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Critically important:&lt;/strong&gt; A proxy server does not make you an "invisible man" on the internet in an absolute sense. It provides a certain &lt;em&gt;level&lt;/em&gt; of anonymity, which depends on its type and configuration.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What is hidden:&lt;/strong&gt; Your real IP address from the &lt;strong&gt;target web server&lt;/strong&gt;. This is the main thing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What is NOT hidden or can be revealed:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;proxy server provider&lt;/strong&gt; knows your real IP address, as you are the one establishing the connection with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the connection between you and the proxy server is not encrypted (HTTPS or SOCKS5 with encryption is not used), then your internet provider or anyone eavesdropping on your network (e.g., on public Wi-Fi) can see that you are using a proxy and potentially intercept your unencrypted traffic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are different levels of proxy anonymity. Some types (transparent) do not hide the fact of using a proxy or your IP at all, while others (anonymous) hide the IP but do not hide the fact of using a proxy. Only elite (high anonymous) proxies do not inform the target server of their intermediation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your identity can be de-anonymized by behavioral factors ("digital fingerprint") — browser plugins used, screen resolution, fonts, even typing rhythm. A proxy does not protect against this.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; A proxy is a tool for hiding an IP address from the end server, not a panacea for complete anonymity. For the latter, more complex systems like Tor are required.&lt;/p&gt;
&lt;h4&gt;
  
  
  3. Security: An Additional Barrier with Caveats
&lt;/h4&gt;

&lt;p&gt;Using a proxy can enhance security, but this is not its primary or most reliable function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content filtering:&lt;/strong&gt; Corporate or ISP proxies are often used to block access to malicious sites. The proxy server can check the requested URL against a database of phishing and malware resources and block the connection before it reaches your device.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Virus scanning:&lt;/strong&gt; Some proxies can check downloaded files (e.g., &lt;code&gt;.exe&lt;/code&gt;, &lt;code&gt;.zip&lt;/code&gt;) using antivirus engines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limiting risks on public Wi-Fi:&lt;/strong&gt; When connected through a proxy, all your web traffic is redirected through it. If an attacker on the same public network tries to redirect you to a fake website, a filtering proxy can prevent this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Caution:&lt;/strong&gt; The proxy server itself represents a point of vulnerability. If you use an unreliable (especially free) proxy, its owner has the technical ability to intercept and analyze all your unencrypted traffic, including logins and passwords. Therefore, you should only trust verified providers, and it is always preferable to use encryption (HTTPS) over the proxy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  4. Data Parsing (Web Scraping)
&lt;/h4&gt;

&lt;p&gt;This is a professional task where the proxy ceases to be a tool for the average user and becomes a critically important infrastructure component.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; When automatically collecting data from sites, you send a large number of requests from one IP address in a short period of time. The site's protection systems (e.g., Cloudflare) easily detect such behavior as non-human and block the IP address on suspicion of a DDoS attack or scraping.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; Using a pool (rotator) of proxy servers. The parsing program sends requests in turn through different proxies, thus distributing the load across hundreds or thousands of different IP addresses. For the target server, this looks like normal traffic from different devices around the world, which does not raise suspicions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Requirements for parsing proxies:&lt;/strong&gt; High speed, reliability, a large number of IP addresses in the pool, and often residential IP addresses (the IP type will be covered in part 2) are required, which are harder to block.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  5. SEO and SMM (Managing Multiple Accounts)
&lt;/h4&gt;

&lt;p&gt;The principle here is similar to parsing, but applied in marketing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; Social media platforms (Instagram, Facebook, TikTok) and search engines (Google) strictly monitor account behavior. If several accounts log in from the same IP address, the system algorithms can link them together ("link by IP"). If one account is banned, there is a high probability of blocking all accounts linked to it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; Each account or group of accounts is assigned its own unique proxy server with a permanent (static) IP address. Thus, for the platform, each account exists in its own isolated network environment, as if it were managed from different apartments, offices, or cities. This significantly reduces the risk of mass blocking.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  6. Traffic Saving (Caching) — Historical Context
&lt;/h4&gt;

&lt;p&gt;This is a function that has lost its relevance for the end user but remains important at the infrastructure level.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;How it worked:&lt;/strong&gt; The proxy server saved (cached) copies of frequently requested resources: images, CSS files, even entire HTML pages. When the next user on the same network (e.g., a company employee) requested the same resource, the proxy server delivered the data from its cache without accessing the external internet. This saved external bandwidth and sped up loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why it's rare now:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encryption (HTTPS everywhere).&lt;/strong&gt; The modern internet almost entirely operates over HTTPS. This means the connection between the browser and the server is encrypted. The proxy server cannot decrypt this traffic and, consequently, cannot analyze and cache its content. It can only cache unencrypted HTTP traffic, the share of which is negligible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic content.&lt;/strong&gt; Web pages have become dynamic and personalized. Social feeds, news streams, advertising — the content on the same URL is different for different users and at different times. Such content is pointless to cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CDN (Content Delivery Network).&lt;/strong&gt; The caching function has been taken over by global content delivery networks (Cloudflare, Akamai, etc.), which place copies of content on servers worldwide, close to users.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Today, caching proxies are used mainly by internet providers to reduce load on backbone channels (caching popular video from YouTube) and in large corporate networks for caching operating system and program updates.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Most Important Proxy Types: Classification by Protocol and Operation Level
&lt;/h3&gt;

&lt;p&gt;Classifying proxies by protocol is the foundation, as it determines what specific network traffic the proxy can work with.&lt;/p&gt;


&lt;h3&gt;
  
  
  1. HTTP(S) Proxy — Specialized Web Inspectors
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Operation Level:&lt;/strong&gt; Application Layer of the OSI/TCP model. The key feature is that the proxy &lt;strong&gt;understands the structure and semantics of the HTTP protocol&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Principle of Operation:&lt;/strong&gt; Deep understanding of HTTP allows such a proxy to:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Analyze and modify HTTP headers&lt;/strong&gt; (add, remove, change).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache content&lt;/strong&gt; — save copies of frequently requested resources to speed up access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filter traffic&lt;/strong&gt; based on URL, MIME types, or content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Require authentication&lt;/strong&gt; from the user (login/password).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical Difference between HTTP and HTTPS:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP proxy:&lt;/strong&gt; Works with unencrypted traffic. &lt;strong&gt;Sees everything:&lt;/strong&gt; full URLs, sent data (logins, passwords), page contents. It is a "Man-in-the-Middle" by nature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTPS proxy (CONNECT mode):&lt;/strong&gt; To protect content from being viewed, the &lt;code&gt;CONNECT&lt;/code&gt; command is used. The proxy establishes an &lt;strong&gt;end-to-end TLS tunnel&lt;/strong&gt; between the client and the server. &lt;strong&gt;Important:&lt;/strong&gt; In this mode, the proxy &lt;strong&gt;cannot&lt;/strong&gt; decrypt or modify the transmitted data. It only relays encrypted bytes, acting as a "dumb pipe." It only sees the hostname of the target server (specified in &lt;code&gt;CONNECT&lt;/code&gt;), but not the full URL-path or data inside the session.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard Limitations:&lt;/strong&gt; Work &lt;strong&gt;EXCLUSIVELY&lt;/strong&gt; with HTTP/HTTPS traffic. Not suitable for FTP, game servers, VoIP, or torrents (with rare exceptions when the client can only proxy HTTP requests to trackers).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scope of Application:&lt;/strong&gt; Web browsers and applications using HTTP/HTTPS APIs (REST, GraphQL clients).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  2. SOCKS Proxy — Universal Transport Relays
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Operation Level:&lt;/strong&gt; Session Layer or even between Session and Transport Layers. SOCKS operates &lt;strong&gt;below&lt;/strong&gt; HTTP proxies and is &lt;strong&gt;abstracted from application layer protocols&lt;/strong&gt;. For it, the transmitted data is an opaque byte stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Principle of Operation:&lt;/strong&gt; A SOCKS proxy redirects TCP/UDP packets without delving into their content. It does not analyze headers, cache, or filter content. Its task is to create a "transport corridor."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Evolution:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SOCKS4:&lt;/strong&gt; Outdated standard. Supports only TCP, authentication is based on the client's IP address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SOCKS5:&lt;/strong&gt; Modern standard. Key improvements:&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UDP support:&lt;/strong&gt; Indispensable for DNS queries, VoIP (Zoom, Telegram), online games.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexible authentication:&lt;/strong&gt; Methods &lt;code&gt;LOGIN/PASSWORD&lt;/code&gt; and &lt;code&gt;NO AUTHENTICATION&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IPv6 support.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remote DNS resolution (Remote DNS):&lt;/strong&gt; &lt;strong&gt;Critically important option.&lt;/strong&gt; When activated on the client, DNS queries are proxied through the SOCKS connection, preventing leaks. Without it, DNS queries bypass the proxy, revealing browsing history.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scope of Application:&lt;/strong&gt; Any applications not limited to web protocols: torrent clients, online games, messengers, SSH and FTP clients.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Excellent! Let's break down RFC 1928 — SOCKS Protocol Version 5 meticulously. This is not just a dry standard, but an elegant engineering solution that remains relevant almost 30 years later.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Architecture and Philosophy of SOCKS5 &lt;a href="https://datatracker.ietf.org/doc/html/rfc1928" rel="noopener noreferrer"&gt;(RFC 1928)&lt;/a&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Key Idea:&lt;/strong&gt; SOCKS5 is a "shim-layer" &lt;strong&gt;between the Application (L7) and Transport (L4) layers&lt;/strong&gt;, providing transparent traffic tunneling through firewalls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Application] → [SOCKS-client] → [SOCKS-server] → [Target Server]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Connection Establishment Process (3 Phases)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Phase 1: Greeting and Authentication Method Selection&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The client connects to TCP port &lt;strong&gt;1080&lt;/strong&gt; and sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+----------+----------+
|VER | NMETHODS | METHODS  |
+----+----------+----------+
| 0x05|    1     | 1 to 255 |
+----+----------+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Breakdown:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;VER = 0x05&lt;/code&gt; — SOCKS5 version&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NMETHODS&lt;/code&gt; — number of supported authentication methods&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;METHODS&lt;/code&gt; — list of methods:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x00 - NO AUTHENTICATION REQUIRED
0x01 - GSSAPI (Kerberos)
0x02 - USERNAME/PASSWORD
0x03-0x7F - IANA ASSIGNED
0x80-0xFE - PRIVATE METHODS
0xFF - NO ACCEPTABLE METHODS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Server responds:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+--------+
|VER | METHOD |
+----+--------+
| 0x05| chosen |
+----+--------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the server returns &lt;code&gt;0xFF&lt;/code&gt;, the connection is terminated.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Phase 2: Authentication (method-dependent)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;For method 0x02 (USERNAME/PASSWORD) — RFC 1929:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+-----+-------+------+----------+------+----------+
|VER | ULEN | UNAME | PLEN | PASSWD | VER | STATUS |
+----+-----+-------+------+----------+------+----------+
| 0x01|  1   |  var  |  1   |   var   | 0x01 |    1     |
+----+-----+-------+------+----------+------+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Statuses:&lt;/strong&gt; &lt;code&gt;0x00&lt;/code&gt; — success, &lt;code&gt;0x01&lt;/code&gt; — failure&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Phase 3: Sending the Request&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After successful authentication, the client sends the main request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+-----+-------+------+----------+----------+
|VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
+----+-----+-------+------+----------+----------+
| 1  |  1  | 0x00  |  1   | Variable |    2     |
+----+-----+-------+------+----------+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Request Types (CMD)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CONNECT (0x01)&lt;/strong&gt; — establish a TCP connection to the target server&lt;br&gt;
&lt;strong&gt;BIND (0x02)&lt;/strong&gt; — reverse connection (for FTP, P2P)&lt;br&gt;
&lt;strong&gt;UDP ASSOCIATE (0x03)&lt;/strong&gt; — work with UDP traffic&lt;/p&gt;

&lt;h2&gt;
  
  
  🌐 &lt;strong&gt;Address Types (ATYP)&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x01 - IPv4 (4 bytes)
0x03 - DOMAINNAME (1 byte length + string without NULL)
0x04 - IPv6 (16 bytes)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; For domain names, the first byte contains the name length, followed by the string WITHOUT a null terminator.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Server Response&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+-----+-------+------+----------+----------+
|VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
+----+-----+-------+------+----------+----------+
| 1  |  1  | 0x00  |  1   | Variable |    2     |
+----+-----+-------+------+----------+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Response Codes (REP)&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x00 - succeeded
0x01 - general SOCKS server failure
0x02 - connection not allowed by ruleset
0x03 - Network unreachable
0x04 - Host unreachable
0x05 - Connection refused
0x06 - TTL expired
0x07 - Command not supported
0x08 - Address type not supported
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Special Scenarios&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;BIND Request (for FTP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Client sends a BIND request&lt;/li&gt;
&lt;li&gt;Server creates a socket and sends the first response with &lt;code&gt;BND.ADDR/BND.PORT&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Client communicates this data to the FTP server&lt;/li&gt;
&lt;li&gt;When the FTP server connects, the SOCKS server sends a second response&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;UDP ASSOCIATE&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Creates an association for UDP traffic&lt;/li&gt;
&lt;li&gt;Client must send UDP packets to &lt;code&gt;BND.ADDR:BND.PORT&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Each datagram has a header:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+------+------+----------+----------+----------+
|RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
+----+------+------+----------+----------+----------+
| 2  |  1   |  1   | Variable |    2     | Variable |
+----+------+------+----------+----------+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  💡 &lt;strong&gt;Critically Important Features&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. DNS Support at the Protocol Level&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;SOCKS5 can resolve domain names on the server side (ATYP=0x03), which prevents DNS leaks.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. UDP Fragmentation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Supports fragmentation via the FRAG field:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0x00&lt;/code&gt; — standalone datagram&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1-127&lt;/code&gt; — fragment position&lt;/li&gt;
&lt;li&gt;High bit = 1 — last fragment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Security through Authentication&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Unlike SOCKS4, it has a built-in authentication system.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Practical Significance Today&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Bypassing blocks&lt;/strong&gt; — the basis of most proxy services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tor Network&lt;/strong&gt; — uses SOCKS5 as an interface for applications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mobile applications&lt;/strong&gt; — many VPN applications use SOCKS5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data parsing&lt;/strong&gt; — IP rotation via SOCKS5 proxies&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Limitations and Caveats&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No encryption by default&lt;/strong&gt; — traffic between client and SOCKS server is not encrypted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebRTC leaks&lt;/strong&gt; — SOCKS5 does not protect against WebRTC leaks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requires client-side support&lt;/strong&gt; — the application must be SOCKS-aware&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;SOCKS5 is an elegant, minimalist protocol that solves a specific task: transparently tunneling traffic through an intermediate node. Its strength lies in its simplicity and extensibility, which explains its longevity in the ever-changing world of network technologies.&lt;/p&gt;

&lt;p&gt;The protocol is perfectly suited for situations where you need to change the source of an outgoing connection without modifying the application itself at the protocol level.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Transparent Proxies — The Coercive Network Overseer
&lt;/h3&gt;

&lt;p&gt;This is &lt;strong&gt;not a protocol, but an implementation method&lt;/strong&gt;, invisible to the end user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Principle of Operation:&lt;/strong&gt; Proxy settings are &lt;strong&gt;not configured&lt;/strong&gt; in the OS or applications. The network gateway (router, firewall) using mechanisms like &lt;strong&gt;NAT (Destination NAT)&lt;/strong&gt; or &lt;strong&gt;Policy-Based Routing (PBR)&lt;/strong&gt; automatically redirects all outgoing traffic (ports 80, 443) to an internal proxy server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What the end server sees:&lt;/strong&gt; The configuration varies, but in the classic case, the proxy, while remaining "invisible" to the client, &lt;strong&gt;adds a special header&lt;/strong&gt; (most often &lt;code&gt;X-Forwarded-For&lt;/code&gt;) to the HTTP request, in which it passes the user's real IP address. Thus, the server knows both the fact of proxying and the original client IP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Where it's found:&lt;/strong&gt; Corporate and ISP networks, public Wi-Fi (airports, hotels). Goals — caching, content filtering, monitoring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard Verdict:&lt;/strong&gt; Absolutely useless for anonymity and bypassing geographical blocks, as it &lt;strong&gt;does not hide, and often directly reveals your IP address&lt;/strong&gt;. Its purpose is control, not freedom.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎯 Final Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A proxy is a tool for changing your IP, not for anonymity.&lt;/strong&gt; It solves specific problems: bypassing blocks, parsing, and account management. True anonymity requires a combination of measures: Tor, anti-tracking browsers, and behavior modification (and a whole bunch of other things). Choose the proxy type based on your specific needs—there are no one-size-fits-all solutions.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>What is SIMD? The Basic Concept</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:45:38 +0000</pubDate>
      <link>https://dev.to/dima853/what-is-simd-the-basic-concept-39cp</link>
      <guid>https://dev.to/dima853/what-is-simd-the-basic-concept-39cp</guid>
      <description>&lt;p&gt;&lt;strong&gt;SIMD (Single Instruction, Multiple Data)&lt;/strong&gt; is a type of parallel data processing in which &lt;strong&gt;a single instruction is executed simultaneously on multiple data elements&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A simple analogy:&lt;/strong&gt; Imagine you have 4 pairs of numbers, and you need to add each pair.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalar approach (SISD):&lt;/strong&gt; You take the first pair (A1, B1), execute the "add" command and get C1. Then you take the second pair (A2, B2), execute "add" again and get C2, and so on. &lt;strong&gt;4 instructions for 4 operations.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency:&lt;/strong&gt; The text emphasizes that SIMD can provide an "order of magnitude increase in efficiency" (work per instruction) compared to scalar code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SIMD approach:&lt;/strong&gt; You load all 4 pairs of numbers into a special wide register. Then you execute &lt;strong&gt;ONE&lt;/strong&gt; command "add all pairs simultaneously". As a result, you immediately get 4 results (C1, C2, C3, C4). &lt;strong&gt;1 instruction for 4 operations.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows for significant acceleration in tasks where it is necessary to apply the same operation to a large array of data.&lt;/p&gt;




&lt;h4&gt;
  
  
  1. Key Characteristics of SIMD
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Level Parallelism:&lt;/strong&gt; SIMD does not create parallel command threads (like in multithreading), but uses the &lt;strong&gt;parallelism&lt;/strong&gt; of the data itself. All processed elements go through the same operation "pipeline".&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Let's go into more detail if it's not clear ⬇️⬇️⬇️
&lt;/h5&gt;




&lt;h3&gt;
  
  
  Data Parallelism (SIMD) vs Task Parallelism (Multithreading)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;TASK Parallelism:&lt;/strong&gt;&lt;br&gt;
Different workers do &lt;strong&gt;DIFFERENT&lt;/strong&gt; things with different data.&lt;br&gt;
&lt;em&gt;Example:&lt;/em&gt; 4 chefs cook 4 different dishes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DATA Parallelism:&lt;/strong&gt;&lt;br&gt;
One boss gives &lt;strong&gt;ONE&lt;/strong&gt; command, which everyone executes &lt;strong&gt;SIMULTANEOUSLY&lt;/strong&gt; on their own data.&lt;br&gt;
&lt;em&gt;Example:&lt;/em&gt; Coach to the team: "EVERYONE squat!" — 20 people squat at once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Essence:&lt;/strong&gt; SIMD is not "divide and conquer", but "do everything at once".&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Not to be confused with ISA:&lt;/strong&gt; SIMD is an architectural principle that can be implemented as an instruction set (like SSE or AVX), but it is not the entire instruction set architecture as a whole.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  What is ISA? ⬇️⬇️⬇️
&lt;/h4&gt;




&lt;p&gt;&lt;strong&gt;ISA (Instruction Set Architecture) is the language of communication between the processor and the programmer.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell: &lt;strong&gt;ISA is the list of commands that the processor understands.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Essence in Three Points:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;"Contract" or "Interface"&lt;/strong&gt;&lt;br&gt;
ISA is not the processor itself, but its "instruction manual". It says: "Here are the commands (addition, copy, branch) I understand and here are the fast memory cells (registers) I have".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Separates "What" from "How"&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ISA defines &lt;em&gt;WHAT&lt;/em&gt; the processor can do&lt;/strong&gt; (which set of commands is available).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microarchitecture defines &lt;em&gt;HOW&lt;/em&gt; it does it&lt;/strong&gt; (the internal design, which can be different for different manufacturers).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt; Intel and AMD processors have the &lt;em&gt;same&lt;/em&gt; ISA (x86), but &lt;em&gt;different&lt;/em&gt; internal design.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compatibility Guarantee&lt;/strong&gt;&lt;br&gt;
Thanks to ISA, the same program will work on any processor that understands this "language", even if these processors are made by different companies or have different performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt;&lt;br&gt;
ISA is like traffic rules. All drivers (programs) know that a "Stop" sign (processor command) means to stop. It doesn't matter which exact car (microarchitecture) you are driving - Mercedes or BMW - the rule is the same.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;but, let's see how exactly ISA defines something there&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;ISA = Table of Operation Codes (OPcode)&lt;/strong&gt;
&lt;/h3&gt;




&lt;h3&gt;
  
  
  OPcode is a number that tells the processor &lt;strong&gt;WHAT TO DO&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;(simplified, figuratively)&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary OPcode&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;001000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;addi&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add Immediate (constant)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;add&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add registers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;100011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Load Word (from memory)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  How it works:
&lt;/h3&gt;

&lt;p&gt;The processor sees &lt;code&gt;001000&lt;/code&gt; → looks up the ISA table → understands: "need to add a register with a number"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ISA is a dictionary:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;001000&lt;/code&gt; = "addi"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;000000&lt;/code&gt; = "add"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;100011&lt;/code&gt; = "lw"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;let's say there is also this&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/image.png" alt="alt text" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The processor doesn't think — it simply executes actions according to a pre-defined table (ISA)&lt;/strong&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  2. Hardware
&lt;/h4&gt;

&lt;p&gt;SIMD is implemented in most modern processors in the form of special execution units and registers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Registers:&lt;/strong&gt; These are special, very wide registers that can store multiple values simultaneously.

&lt;ul&gt;
&lt;li&gt;Example: A 128-bit SSE register can store four 32-bit floating-point numbers or sixteen 8-bit integers.&lt;/li&gt;
&lt;li&gt;Modern standards, such as &lt;strong&gt;AVX-512&lt;/strong&gt;, use 512-bit registers, allowing work with 16 floating-point numbers simultaneously.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Examples of SIMD extensions:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Intel:&lt;/strong&gt; MMX, SSE, SSE2, SSE3, AVX, AVX2, &lt;strong&gt;AVX-512&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AMD:&lt;/strong&gt; 3DNow!, and now also supports AVX.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ARM:&lt;/strong&gt; &lt;strong&gt;NEON&lt;/strong&gt; (very common in mobile processors), Scalable Vector Extension (SVE).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PowerPC/IBM:&lt;/strong&gt; AltiVec.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other devices:&lt;/strong&gt; SIMD is actively used in GPUs (graphics processing units), specialized processors for video processing (for example, the Cell processor in PlayStation 3) and AI accelerators (for example, the Neural Engine in Apple chips).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fms.codes%2Fcdn%2Fshop%2Farticles%2FIntel-mmx-sse-sse2-avx-AVX-512_949x.png%3Fv%3D1707850038" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fms.codes%2Fcdn%2Fshop%2Farticles%2FIntel-mmx-sse-sse2-avx-AVX-512_949x.png%3Fv%3D1707850038" width="949" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Software / Programmer Interface
&lt;/h4&gt;

&lt;p&gt;A programmer does not need to write code in assembly to use SIMD. There are more convenient ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Intrinsics:&lt;/strong&gt; These are special functions in C/C++ languages that compile directly into SIMD instructions. They look like ordinary functions but give the programmer low-level control.

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;_mm_add_ps()&lt;/code&gt; for adding four floating-point numbers in SSE.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auto-vectorization:&lt;/strong&gt; Modern compilers (GCC, Clang, MSVC, ICC) can &lt;strong&gt;automatically&lt;/strong&gt; analyze loops and transform scalar code into SIMD code. Compilation flags are often used for this, for example, &lt;code&gt;-O3&lt;/code&gt; or &lt;code&gt;-ftree-vectorize&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language Extensions and Libraries:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In C/C++ there is an experimental header &lt;code&gt;&amp;lt;std::simd&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In Rust there is the &lt;code&gt;packed_simd&lt;/code&gt; crate.&lt;/li&gt;
&lt;li&gt;In .NET there is the &lt;code&gt;System.Numerics.Vector&lt;/code&gt; namespace.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenMP&lt;/strong&gt; provides the &lt;code&gt;#pragma omp simd&lt;/code&gt; directive to hint to the compiler.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Multi-versioning (SIMD Multi-versioning)
&lt;/h4&gt;

&lt;p&gt;Since different generations of SIMD instructions exist, a program must work on different processors. For this, a method is used where several versions of the same code are created:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;FMV (Function Multi-versioning):&lt;/strong&gt; The same function is compiled multiple times for different instruction sets (for example, one version for SSE4, another for AVX2). During runtime, the program checks the CPU capabilities and selects the appropriate version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LMV (Library Multi-versioning):&lt;/strong&gt; An entire library is compiled in several variants, and the operating system or program loads the required version.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. Disadvantages
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Not for all tasks:&lt;/strong&gt; SIMD is effective only for tasks with a high degree of data parallelism. Algorithms with strong dependencies between steps (for example, traversing a linked list) are poorly vectorized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complexity of manual optimization:&lt;/strong&gt; Although compilers handle auto-vectorization well, achieving maximum performance often requires manual tuning using intrinsics, which requires deep knowledge &lt;strong&gt;(but this is a plus, not a minus)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  6. Where is it used? (Commercial Applications)
&lt;/h4&gt;

&lt;p&gt;SIMD is critically important for tasks requiring intensive computations on large data arrays:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Image and Video Processing:&lt;/strong&gt; Changing brightness, contrast, applying filters, encoding/decoding (MPEG, H.264).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audio Processing:&lt;/strong&gt; Mixing, applying effects, equalizers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scientific Computing:&lt;/strong&gt; Numerical modeling, matrix and vector calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Games:&lt;/strong&gt; 3D vertex transformations, physics calculations, artificial intelligence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cryptography:&lt;/strong&gt; Many encryption and hashing algorithms can be accelerated using SIMD.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Artificial Intelligence:&lt;/strong&gt; Matrix multiplication and convolutions in neural networks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  7. Confusion with SIMT
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SIMD:&lt;/strong&gt; A single instruction controls multiple processing elements in a CPU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SIMT (Single Instruction, Multiple Threads):&lt;/strong&gt; An architecture used in GPUs (NVIDIA). Here, a single instruction is also executed on multiple data elements, but it is delivered to multiple independent threads, which can have their own execution path (for example, conditional &lt;code&gt;if/else&lt;/code&gt; statements are handled differently). The text from &lt;a href="//en.wikipedia.org/wiki/Instruction_set_architecture"&gt;Wikipedia&lt;/a&gt; clarifies that SIMT is a subcategory of SIMD according to Flynn's taxonomy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;SIMD&lt;/strong&gt; is a fundamental acceleration technology that has become an integral part of modern processors. It allows for a significant increase in performance in multimedia, scientific, and engineering tasks by parallelizing the execution of the same operation on a set of data. Despite some programming complexities, thanks to automatic vectorization in compilers and convenient libraries, the benefits of SIMD are becoming available to a wider range of developers.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>SNMP Guide: What, Why, and How?</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:44:15 +0000</pubDate>
      <link>https://dev.to/dima853/snmp-guide-what-why-and-how-1fjg</link>
      <guid>https://dev.to/dima853/snmp-guide-what-why-and-how-1fjg</guid>
      <description>&lt;h2&gt;
  
  
  🌐 &lt;strong&gt;What SNMP Actually Does:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SNMP = "WhatsApp for Network Devices"&lt;/strong&gt; 💬&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You (Manager) --WhatsApp--&amp;gt; Friend (Network Device)
"Hey, what's your status?" = GET request
"BRB, battery 5%" = TRAP notification
"Change your profile pic" = SET request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Real World Examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📱 &lt;strong&gt;Your phone&lt;/strong&gt; monitoring Wi-Fi router signal strength&lt;/li&gt;
&lt;li&gt;🖥️ &lt;strong&gt;IT department&lt;/strong&gt; checking if servers are alive&lt;/li&gt;
&lt;li&gt;🚨 &lt;strong&gt;Data center&lt;/strong&gt; getting alerts when temperature spikes&lt;/li&gt;
&lt;li&gt;📈 &lt;strong&gt;Monitoring system&lt;/strong&gt; tracking network bandwidth usage&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 &lt;strong&gt;SNMP + MIB + LLDP = The Dream Team&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You're absolutely right! Here's how they work together:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;SNMP 🕵️‍♂️ - The Detective&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Asks questions&lt;/strong&gt;: "What's your CPU usage?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gives commands&lt;/strong&gt;: "Shut down port 5!"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Listens for gossip&lt;/strong&gt;: "Hey, I just crashed!"&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MIB 📚 - The Phone Book&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Contains all "questions" you can ask&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Every device has its own phone book&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standardized "contact list" for all vendors&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;LLDP 📢 - The Town Crier&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shouts&lt;/strong&gt;: "I'm Router-X, connected to Switch-Y!"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Like WhatsApp status&lt;/strong&gt;: "Here's who I am and who I know"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SNMP reads LLDP info&lt;/strong&gt; to map network topology&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎪 &lt;strong&gt;The "WhatsApp" Analogy:&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SNMP Manager = You texting friends
SNMP Agent = Your friends' phones
MIB = Your contacts list 📞
OID = Phone numbers 📱
GET = "What's up?" ❓
SET = "Change your status!" ✏️
TRAP = "OMG emergency!" 🚨
LLDP = "I'm at Starbucks with Bob" 📍
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🚀 &lt;strong&gt;Why You Should Care:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Without SNMP:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🕵️‍♂️ &lt;strong&gt;Manual checking&lt;/strong&gt; of every device&lt;/li&gt;
&lt;li&gt;😴 &lt;strong&gt;No alerts&lt;/strong&gt; when things break&lt;/li&gt;
&lt;li&gt;🤷 &lt;strong&gt;No historical data&lt;/strong&gt; for troubleshooting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;With SNMP:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📊 &lt;strong&gt;Dashboard&lt;/strong&gt; showing everything&lt;/li&gt;
&lt;li&gt;📱 &lt;strong&gt;Phone alerts&lt;/strong&gt; when problems occur&lt;/li&gt;
&lt;li&gt;📈 &lt;strong&gt;Trend analysis&lt;/strong&gt; to predict issues&lt;/li&gt;
&lt;li&gt;🤖 &lt;strong&gt;Automated responses&lt;/strong&gt; to common problems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💡 &lt;strong&gt;TL;DR:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SNMP lets you remotely monitor and control network devices like you're texting friends on WhatsApp!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Manager&lt;/strong&gt; = You texting&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agent&lt;/strong&gt; = Your friends&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIB&lt;/strong&gt; = Contacts list&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OID&lt;/strong&gt; = Phone numbers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLDP&lt;/strong&gt; = Location sharing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; You can manage thousands of devices without leaving your chair! 🪑✨&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now go impress your coworkers with your SNMP knowledge!&lt;/strong&gt; 😎&lt;/p&gt;




&lt;h2&gt;
  
  
  📋 Header Overview
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#pragma once
#ifndef SNMP_H
#define SNMP_H
&lt;/span&gt;
&lt;span class="cp"&gt;#ifdef __cplusplus
&lt;/span&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Standard C header guards to prevent multiple inclusions and ensure C++ compatibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This approach is widely considered a best practice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The combination of &lt;code&gt;#pragma once&lt;/code&gt; and the classic &lt;code&gt;#ifndef&lt;/code&gt; provides a balanced approach to protect against multiple inclusion of header files, though the actual benefits may vary depending on the specific use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this approach is commonly recommended:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;#pragma once&lt;/code&gt; - Potential speed and convenience&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;May be faster in some scenarios&lt;/strong&gt;: On projects with complex include hierarchies, some compilers can optimize file processing using filesystem metadata.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convenience factor&lt;/strong&gt;: Avoids the need to maintain unique macro names across the codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced naming conflicts&lt;/strong&gt;: Eliminates potential macro name collisions between different headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;#ifndef&lt;/code&gt; / &lt;code&gt;#define&lt;/code&gt; - Portability and reliability&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wide portability&lt;/strong&gt;: The &lt;code&gt;#ifndef&lt;/code&gt; directive is a C/C++ language standard that should work on any compliant compiler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallback protection&lt;/strong&gt;: Provides a reliable alternative when &lt;code&gt;#pragma once&lt;/code&gt; support is limited or encounters unusual filesystem situations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  but macros are not safe.
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Performance Observations from Testing:
&lt;/h3&gt;

&lt;p&gt;Our limited testing showed mixed results that may not be representative of all scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Small to medium files&lt;/strong&gt;: Performance differences were generally minor (0.01-0.05s)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Larger inclusion scenarios&lt;/strong&gt;: Some tests showed &lt;code&gt;#pragma once&lt;/code&gt; with modest improvements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combined approach&lt;/strong&gt;: Performance was comparable to either method alone in most cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The actual performance impact likely depends on many factors including compiler implementation, filesystem characteristics, project structure, and build environment. The benefits may be more significant in very large projects with complex include graphs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Considerations:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modern compiler behavior&lt;/strong&gt;: Most contemporary compilers (GCC, Clang, MSVC) recognize &lt;code&gt;#pragma once&lt;/code&gt; while maintaining &lt;code&gt;#ifndef&lt;/code&gt; compatibility&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compiler variations&lt;/strong&gt;: Some compilers may optimize the combined approach differently than others&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case handling&lt;/strong&gt;: The dual approach may provide additional safety in unusual environments like network filesystems or with symbolic links&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conservative Recommendation:
&lt;/h3&gt;

&lt;p&gt;This combined approach represents a reasonable compromise that prioritizes compatibility while potentially offering performance benefits in some situations. For maximum portability across diverse build environments, the additional &lt;code&gt;#ifndef&lt;/code&gt; guard provides insurance against &lt;code&gt;#pragma once&lt;/code&gt; limitations in edge cases.&lt;/p&gt;

&lt;p&gt;The minimal overhead of including both directives appears to be acceptable for most practical purposes, though individual projects should consider their specific requirements and target environments.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation Details &amp;amp; Testing - &lt;a href="https://github.com/dima853/self_university/tree/main/network/c/compatibility/ifndef_pragmaonce" rel="noopener noreferrer"&gt;https://github.com/dima853/self_university/tree/main/network/c/compatibility/ifndef_pragmaonce&lt;/a&gt;
&lt;/h1&gt;




&lt;h1&gt;
  
  
  SNMP Header Documentation (Modernized Version)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  📋 Header Overview
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#pragma once
#ifndef SNMP_H
#define SNMP_H
&lt;/span&gt;
&lt;span class="cp"&gt;#ifdef __cplusplus
&lt;/span&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Standard C header guards to prevent multiple inclusions and ensure C++ compatibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This approach is widely considered a best practice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The combination of &lt;code&gt;#pragma once&lt;/code&gt; and the classic &lt;code&gt;#ifndef&lt;/code&gt; provides a balanced approach to protect against multiple inclusion of header files, though the actual benefits may vary depending on the specific use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this approach is commonly recommended:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;#pragma once&lt;/code&gt; - Potential speed and convenience&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;May be faster in some scenarios&lt;/strong&gt;: On projects with complex include hierarchies, some compilers can optimize file processing using filesystem metadata.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convenience factor&lt;/strong&gt;: Avoids the need to maintain unique macro names across the codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced naming conflicts&lt;/strong&gt;: Eliminates potential macro name collisions between different headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;#ifndef&lt;/code&gt; / &lt;code&gt;#define&lt;/code&gt; - Portability and reliability&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wide portability&lt;/strong&gt;: The &lt;code&gt;#ifndef&lt;/code&gt; directive is a C/C++ language standard that should work on any compliant compiler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallback protection&lt;/strong&gt;: Provides a reliable alternative when &lt;code&gt;#pragma once&lt;/code&gt; support is limited or encounters unusual filesystem situations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Performance Observations from Testing:
&lt;/h3&gt;

&lt;p&gt;Our limited testing showed mixed results that may not be representative of all scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Small to medium files&lt;/strong&gt;: Performance differences were generally minor (0.01-0.05s)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Larger inclusion scenarios&lt;/strong&gt;: Some tests showed &lt;code&gt;#pragma once&lt;/code&gt; with modest improvements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combined approach&lt;/strong&gt;: Performance was comparable to either method alone in most cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The actual performance impact likely depends on many factors including compiler implementation, filesystem characteristics, project structure, and build environment. The benefits may be more significant in very large projects with complex include graphs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Considerations:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modern compiler behavior&lt;/strong&gt;: Most contemporary compilers (GCC, Clang, MSVC) recognize &lt;code&gt;#pragma once&lt;/code&gt; while maintaining &lt;code&gt;#ifndef&lt;/code&gt; compatibility&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compiler variations&lt;/strong&gt;: Some compilers may optimize the combined approach differently than others&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case handling&lt;/strong&gt;: The dual approach may provide additional safety in unusual environments like network filesystems or with symbolic links&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conservative Recommendation:
&lt;/h3&gt;

&lt;p&gt;This combined approach represents a reasonable compromise that prioritizes compatibility while potentially offering performance benefits in some situations. For maximum portability across diverse build environments, the additional &lt;code&gt;#ifndef&lt;/code&gt; guard provides insurance against &lt;code&gt;#pragma once&lt;/code&gt; limitations in edge cases.&lt;/p&gt;

&lt;p&gt;The minimal overhead of including both directives appears to be acceptable for most practical purposes, though individual projects should consider their specific requirements and target environments.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation Details &amp;amp; Testing - &lt;a href="https://github.com/dima853/self_university/tree/main/network/c/compatibility/ifndef_pragmaonce" rel="noopener noreferrer"&gt;https://github.com/dima853/self_university/tree/main/network/c/compatibility/ifndef_pragmaonce&lt;/a&gt;
&lt;/h1&gt;




&lt;h2&gt;
  
  
  🌐 Network Constants
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Network Constants ====================&lt;/span&gt;
&lt;span class="cp"&gt;#define SNMP_PORT 161      // SNMP agent request port
#define SNMP_TRAP_PORT 162 // SNMP manager notification port
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Standard IANA-assigned ports for SNMP operations.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;th&gt;Direction&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;161&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Manager → Agent&lt;/td&gt;
&lt;td&gt;GET/SET requests to agents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;162&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Agent → Manager&lt;/td&gt;
&lt;td&gt;Traps/Informs to managers&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  ✅ &lt;strong&gt;OFFICIALLY CONFIRMED BY IANA:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Service Name: &lt;a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=161" rel="noopener noreferrer"&gt;snmp (Port 161)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Port Number&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;161&lt;/span&gt;
&lt;span class="na"&gt;Transport Protocol&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tcp, udp&lt;/span&gt;
&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;SNMP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Service Name: &lt;a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=162" rel="noopener noreferrer"&gt;snmptrap (Port 162)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Port Number&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;162&lt;/span&gt;
&lt;span class="na"&gt;Transport Protocol&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tcp, udp&lt;/span&gt;
&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;SNMPTRAP&lt;/span&gt;
&lt;span class="na"&gt;Assignee&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Marshall Rose&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define SNMP_MAX_MSG_SIZE 1500          // Ethernet MTU
#define SNMP_MIN_MSG_SIZE 484           // RFC 3416 minimum
#define SNMP_MAX_PACKET_SIZE 0x7FFFFFFF // Maximum theoretical size
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it's a shit code, here's the correct code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// ==================== SNMP Port Constants ====================&lt;/span&gt;
    &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;SNMP_PORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// SNMP agent request port&lt;/span&gt;
    &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;SNMP_TRAP_PORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// SNMP manager notification port&lt;/span&gt;

    &lt;span class="c1"&gt;// ==================== SNMP Size Constants ====================&lt;/span&gt;
    &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;SNMP_MAX_MSG_SIZE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Ethernet MTU&lt;/span&gt;
    &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;SNMP_MIN_MSG_SIZE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// RFC 3416 minimum&lt;/span&gt;
    &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;SNMP_MAX_PACKET_SIZE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Maximum theoretical size&lt;/span&gt;

    &lt;span class="c1"&gt;// ==================== Protocol Version ====================&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;SNMP_VERSION_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// SNMPv3&lt;/span&gt;

    &lt;span class="c1"&gt;// ==================== PDU Type Constants ====================&lt;/span&gt;
    &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_GET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// 160 - GetRequest&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_GETNEXT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// 161 - GetNextRequest&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_RESPONSE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 162 - Response&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_SET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// 163 - SetRequest&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_GETBULK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// 165 - GetBulkRequest&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_INFORM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// 166 - InformRequest&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_TRAP2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;// 167 - SNMPv2-Trap&lt;/span&gt;
        &lt;span class="n"&gt;SNMP_PDU_REPORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA8&lt;/span&gt;    &lt;span class="c1"&gt;// 168 - Report&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpPduType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Cheat sheet: Macro vs Enum vs Constant
&lt;/h1&gt;

&lt;p&gt;IMPORTANT ! &lt;a href="https://gist.github.com/dima853/73ba55e23210efe880349a99fb6b904c" rel="noopener noreferrer"&gt;https://gist.github.com/dima853/73ba55e23210efe880349a99fb6b904c&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  use it as a REMINDER, not as a guide!
&lt;/h2&gt;




&lt;h2&gt;
  
  
  1) 📄 From RFC 894 (IP over Ethernet):
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Page 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The maximum length of an IP datagram sent over an Ethernet is 1500 octets."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;This corresponds to:&lt;/strong&gt; &lt;code&gt;#define SNMP_MAX_MSG_SIZE 1500&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2) 📄 From RFC 3416 (SNMP Protocol Operations):
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Section 4.2:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The maximum size of an SNMP message is limited to the minimum of: (a) 484 octets (b) the maximum message size that the destination can accept"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;This corresponds to:&lt;/strong&gt; &lt;code&gt;#define SNMP_MIN_MSG_SIZE 484&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3) 📄 From RFC 3416 (SNMP Protocol Operations):
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Page 9, section 4.1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A compliant implementation must support as many variable bindings in a PDU or BulkPDU as fit into the overall maximum message size limit of the SNMP engine, but no more than 2147483647 variable bindings."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;This corresponds to:&lt;/strong&gt; &lt;code&gt;#define SNMP_MAX_PACKET_SIZE 0x7FFFFFFF&lt;/code&gt; = 2,147,483,647&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Message size limits for memory allocation and buffer management.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 SNMP Version
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Protocol Version ====================&lt;/span&gt;
&lt;span class="cp"&gt;#define SNMP_VERSION_3 3 // SNMPv3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; This header focuses on &lt;strong&gt;SNMPv3&lt;/strong&gt; (the modern, secure version).&lt;/p&gt;




&lt;h2&gt;
  
  
  📨 PDU Types - SNMP Protocol Data Units
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modern Enum-Based Approach
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== PDU Type Constants ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_GET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// 160 - GetRequest&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_GETNEXT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// 161 - GetNextRequest&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_RESPONSE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 162 - Response&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_SET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// 163 - SetRequest&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_GETBULK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// 165 - GetBulkRequest&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_INFORM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// 166 - InformRequest&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_TRAP2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;// 167 - SNMPv2-Trap&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_PDU_REPORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xA8&lt;/span&gt;    &lt;span class="c1"&gt;// 168 - Report&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpPduType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ASN.1 Encoding Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Context-specific tags&lt;/strong&gt;: 0xA0-0xA8 range&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constructed types&lt;/strong&gt;: All PDU types are constructed ASN.1 sequences&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDU type identifier&lt;/strong&gt;: Lower nibble indicates specific PDU type&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PDU Type&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;GET&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA0&lt;/td&gt;
&lt;td&gt;Retrieve specific variable values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;GETNEXT&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA1&lt;/td&gt;
&lt;td&gt;Retrieve next variable in sequence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RESPONSE&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA2&lt;/td&gt;
&lt;td&gt;Response to any request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SET&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA3&lt;/td&gt;
&lt;td&gt;Modify variable values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;GETBULK&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA5&lt;/td&gt;
&lt;td&gt;Efficient bulk data retrieval&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;INFORM&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA6&lt;/td&gt;
&lt;td&gt;Acknowledged notification&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;TRAPv2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA7&lt;/td&gt;
&lt;td&gt;Unacknowledged notification&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;REPORT&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0xA8&lt;/td&gt;
&lt;td&gt;Error reporting between engines&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ⚙️ Internal Processing States
&lt;/h2&gt;

&lt;p&gt;These are &lt;strong&gt;NOT&lt;/strong&gt; network protocol values - they're internal state machine states for SET operation processing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Internal Processing States ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_BEGIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_RESERVE1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_RESERVE2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_ACTION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_COMMIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_FREE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_SET_UNDO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="n"&gt;SNMP_STATE_CHECK_VALUE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_ROW_CREATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STATE_UNDO_SETUP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// ... etc&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpProcessingState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;SET Operation State Flow:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RESERVE1 → RESERVE2 → ACTION → COMMIT → FREE
                    ↘ UNDO (if error)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advanced Internal States:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CHECK_VALUE&lt;/strong&gt;: Validate data before processing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ROW_CREATE&lt;/strong&gt;: Dynamic row creation in tables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UNDO_SETUP&lt;/strong&gt;: Prepare for potential rollback&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IRREVERSIBLE_COMMIT&lt;/strong&gt;: Changes cannot be rolled back&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; These implement the &lt;strong&gt;transaction-like behavior&lt;/strong&gt; for SNMP SET operations, allowing rollback on failure.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Confirmed PDU Detection
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Confirmed PDU Detection ====================&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;snmp_is_confirmed_pdu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pdu_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdu_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SNMP_PDU_INFORM&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SNMP_PDU_GETBULK&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SNMP_PDU_GETNEXT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SNMP_PDU_GET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SNMP_PDU_SET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;#define SNMP_CMD_CONFIRMED(c) snmp_is_confirmed_pdu(c)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Identifies PDUs that &lt;strong&gt;require a response&lt;/strong&gt; from the receiver.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Confirmed&lt;/th&gt;
&lt;th&gt;Unconfirmed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET, GETNEXT, GETBULK&lt;/td&gt;
&lt;td&gt;TRAP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SET, INFORM&lt;/td&gt;
&lt;td&gt;RESPONSE&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(Expect response)&lt;/td&gt;
&lt;td&gt;(No response expected)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ❌ Exception Values (SNMPv2/v3)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Exception Values ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_EXCEPTION_NO_SUCH_OBJECT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// 128&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_EXCEPTION_NO_SUCH_INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x81&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 129&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_EXCEPTION_END_OF_MIB_VIEW&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x82&lt;/span&gt;   &lt;span class="c1"&gt;// 130&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpExceptionType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Special return values for variable binding exceptions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exception&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;noSuchObject&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Requested object doesn't exist&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;noSuchInstance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Requested instance doesn't exist&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;endOfMibView&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No more variables in MIB tree&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🚨 Error Status Codes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modern Enum-Based Error Codes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Error Codes ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_NO_ERROR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_TOO_BIG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_NO_SUCH_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_BAD_VALUE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_READ_ONLY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_GENERIC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_NO_ACCESS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ERROR_WRONG_TYPE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// ... up to SNMP_ERROR_INCONSISTENT_NAME = 18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpErrorCode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cp"&gt;#define MAX_SNMP_ERROR SNMP_ERROR_INCONSISTENT_NAME
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Error Validation Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Error Validation ====================&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;SnmpErrorCode&lt;/span&gt; &lt;span class="nf"&gt;snmp_validate_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;error_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;SNMP_ERROR_NO_ERROR&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAX_SNMP_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;SNMP_ERROR_GENERIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SnmpErrorCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;error_code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;#define SNMP_VALIDATE_ERR(x) snmp_validate_error(x)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Error Explanations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;NO_ACCESS&lt;/code&gt;: Authentication/authorization failure&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;AUTHORIZATION&lt;/code&gt;: VACM access control denial&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;INCONSISTENT_NAME&lt;/code&gt;: Row creation semantic error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Sanitizes error codes to prevent invalid values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Equivalent logic:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;SnmpErrorCode&lt;/span&gt; &lt;span class="nf"&gt;validate_snmp_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;error_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAX_SNMP_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;      &lt;span class="c1"&gt;// More than 18&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;SNMP_ERROR_GENERIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// → 5&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;SNMP_ERROR_NO_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// Less than 0&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;SNMP_ERROR_GENERIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// → 5&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SnmpErrorCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;error_code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// → as is&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🗃️ Row Status Values (For Table Operations)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Row Management ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_NONEXISTENT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_ACTIVE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_NOT_IN_SERVICE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_NOT_READY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_CREATE_AND_GO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_CREATE_AND_WAIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_ROW_DESTROY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpRowStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Implements &lt;strong&gt;dynamic row creation/deletion&lt;/strong&gt; in SNMP tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage Flow:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;graph LR
    A[createAndGo] --&amp;gt; B[active]
    C[createAndWait] --&amp;gt; D[notInService] --&amp;gt; B
    B --&amp;gt; E[destroy] --&amp;gt; F[nonExistent]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💾 Storage Types
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_NONE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_OTHER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_VOLATILE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_NONVOLATILE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_PERMANENT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_STORAGE_READONLY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpStorageType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Defines data persistence characteristics for MIB objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛡️ Security Definitions (SNMPv3)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modern Security Framework
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Security Framework ====================&lt;/span&gt;
&lt;span class="cp"&gt;#define SNMP_MP_MODEL_SNMPv3 3
&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_MODEL_ANY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_MODEL_USM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// User-based Security Model&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_MODEL_TSM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;   &lt;span class="c1"&gt;// Transport Security Model&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpSecurityModel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_LEVEL_NO_AUTH_NO_PRIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// noAuthNoPriv&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_LEVEL_AUTH_NO_PRIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c1"&gt;// authNoPriv&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_SEC_LEVEL_AUTH_PRIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;         &lt;span class="c1"&gt;// authPriv&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpSecurityLevel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_MSG_FLAG_AUTH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// Message is authenticated&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_MSG_FLAG_PRIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// Message is encrypted&lt;/span&gt;
    &lt;span class="n"&gt;SNMP_MSG_FLAG_REPORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x04&lt;/span&gt;  &lt;span class="c1"&gt;// Message is reportable&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpMessageFlags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Security Combinations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// No security&lt;/span&gt;
&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;// Authentication only&lt;/span&gt;
&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SNMP_MSG_FLAG_AUTH&lt;/span&gt;

&lt;span class="c1"&gt;// Authentication + Encryption&lt;/span&gt;
&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SNMP_MSG_FLAG_AUTH&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;SNMP_MSG_FLAG_PRIV&lt;/span&gt;

&lt;span class="c1"&gt;// Reportable message&lt;/span&gt;
&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SNMP_MSG_FLAG_REPORT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🎛️ Control Flags (Implementation-Specific)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Control Flags ====================&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;UCD_FLAG_RESPONSE_PDU&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UCD_FLAG_EXPECT_RESPONSE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UCD_FLAG_FORCE_PDU_COPY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UCD_FLAG_ALWAYS_IN_VIEW&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UCD_FLAG_PDU_TIMEOUT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// ... etc&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;SnmpControlFlags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Internal implementation controls for message processing.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌳 OID Base Definitions
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== OID Base Definitions ===================&lt;/span&gt;
&lt;span class="cp"&gt;#define SNMP_OID_INTERNET 1, 3, 6, 1
#define SNMP_OID_ENTERPRISES SNMP_OID_INTERNET, 4, 1
#define SNMP_OID_MIB2 SNMP_OID_INTERNET, 2, 1
#define SNMP_OID_SNMPV2 SNMP_OID_INTERNET, 6
#define SNMP_OID_SNMPMODULES SNMP_OID_SNMPV2, 3
&lt;/span&gt;
&lt;span class="cp"&gt;#define SNMP_ADMIN_STRING_LENGTH 255
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OID Tree Structure:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.3.6.1 (internet)
├── 2.1 (mib-2)
├── 4.1 (enterprises)
└── 6 (snmpV2)
    └── 3 (snmpModules)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔧 Function Prototypes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modern Function Declarations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ==================== Function Declarations ====================&lt;/span&gt;
&lt;span class="c1"&gt;// Utility functions&lt;/span&gt;
&lt;span class="n"&gt;NETSNMP_IMPORT&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;snmp_uptime_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;timeticks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;NETSNMP_IMPORT&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;snmp_uptime_to_string_safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;timeticks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;buffer_size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;NETSNMP_IMPORT&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;snmp_hex_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;data_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ASN.1 BER encoding/decoding&lt;/span&gt;
&lt;span class="n"&gt;NETSNMP_IMPORT&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;snmp_decode_variable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;input_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;object_id_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;type_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;value_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;value_length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;NETSNMP_IMPORT&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;snmp_encode_variable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;output_buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;object_id_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;value_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;value_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;buffer_used&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; These functions implement the &lt;strong&gt;ASN.1 BER encoding/decoding&lt;/strong&gt; for SNMP protocol data units.&lt;/p&gt;




&lt;h2&gt;
  
  
  🏁 Footer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifdef __cplusplus
&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;
&lt;span class="cp"&gt;#endif // SNMP_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Close the C++ extern "C" block and header guard.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 Summary
&lt;/h2&gt;

&lt;p&gt;This modernized header provides:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clean Protocol Constants&lt;/strong&gt; - Enum-based PDU types, error codes, ports&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SNMPv3 Security&lt;/strong&gt; - Type-safe security models, levels, flags&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal Processing&lt;/strong&gt; - State machines for SET operations with proper types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ASN.1 Utilities&lt;/strong&gt; - Encoding/decoding functions with clear parameter names&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIB Management&lt;/strong&gt; - Row status, storage types, OID bases using modern C practices&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Improvements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Replaced magic numbers with typed enums&lt;/li&gt;
&lt;li&gt;✅ Replaced complex macros with inline functions&lt;/li&gt;
&lt;li&gt;✅ Added proper parameter names to function declarations&lt;/li&gt;
&lt;li&gt;✅ Improved readability and type safety&lt;/li&gt;
&lt;li&gt;✅ Maintained backward compatibility with legacy macros&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  How It Works? (Super Briefly)
&lt;/h1&gt;

&lt;p&gt;
  width="18804"&lt;br&gt;
  height="2464"&lt;br&gt;
  alt="Untitled diagram-2025-10-18-115602"&lt;br&gt;
  src="https://github.com/user-attachments/assets/905f9d59-bed6-468b-ab1a-08045f185ea2"&lt;br&gt;
/&amp;gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🛡️ &lt;strong&gt;Security Levels (SNMPv3 Security Levels)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;L31: noAuthNoPriv (Level 1)&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define SNMP_SEC_LEVEL_NOAUTH 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What is it:&lt;/strong&gt; Without authentication and encryption&lt;br&gt;&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🤡 Anonymous access&lt;/li&gt;
&lt;li&gt;Messages are not subscribed&lt;/li&gt;
&lt;li&gt;🔓 Data is transmitted in clear text&lt;/li&gt;
&lt;li&gt;💀 &lt;strong&gt;DANGEROUS!&lt;/strong&gt; Only for test networks&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;L32: authNoPriv (Level 2)&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define SNMP_SEC_LEVEL_AUTHNOPRIV 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What is it:&lt;/strong&gt; Authentication without encryption&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ User verification (login/password)&lt;/li&gt;
&lt;li&gt;🔐 HMAC-MD5 or HMAC-SHA for signature&lt;/li&gt;
&lt;li&gt;📨 Data is NOT encrypted&lt;/li&gt;
&lt;li&gt;Protection against spoofing, but the data is visible&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;L33: authPriv (Level 3)&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define SNMP_SEC_LEVEL_AUTHPRIV 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What is it:&lt;/strong&gt; Authentication + Encryption&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ User verification&lt;/li&gt;
&lt;li&gt;🔐 Message signature&lt;/li&gt;
&lt;li&gt;🚫 Data encryption (DES, AES)&lt;/li&gt;
&lt;li&gt;💪 &lt;strong&gt;MAXIMUM PROTECTION&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  📨 &lt;strong&gt;PDU Types (Message Types)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Core Operations&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;E11: GET (160)&lt;/strong&gt; - "Give the value of this variable"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E12: GETNEXT (161)&lt;/strong&gt; - "Give the following variable in the MIB tree"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E14: SET (163)&lt;/strong&gt; - "Change the value of a variable"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E13: RESPONSE (162)&lt;/strong&gt; - "Reply to any request"&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Advanced Operations&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;E21: GETBULK (165)&lt;/strong&gt; - "Give a lot of data at once" (optimization)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E22: INFORM (166)&lt;/strong&gt; - "Confirmation Notification"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E23: TRAPv2 (167)&lt;/strong&gt; - "Notification without confirmation"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E24: REPORT (168)&lt;/strong&gt; - "Error message between engines"&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  ⚙️ &lt;strong&gt;Internal Processing (Internal SET Processing)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;F1: SET State Machine&lt;/strong&gt; - as a database transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BEGIN → RESERVE1 → RESERVE2 → ACTION → COMMIT → FREE
                               UNDO (if an error occurs)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why do I need to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RESERVE1/RESERVE2&lt;/code&gt; - booking resources&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ACTION&lt;/code&gt; - making the change&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COMMIT&lt;/code&gt; - confirm&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UNDO&lt;/code&gt; - rollback if an error occurs&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FREE&lt;/code&gt; - freeing up resources&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❌ &lt;strong&gt;Exception Values&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When the variable is not found:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;H1: noSuchObject (128)&lt;/strong&gt; - "There is no such object"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;H2: noSuchInstance (129)&lt;/strong&gt; - "There is no such instance"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;H3: endOfMibView (130)&lt;/strong&gt; - "There is nothing further in the tree"&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚨 &lt;strong&gt;Error Status Codes&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Basic Errors&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;I11: noError (0)&lt;/strong&gt; - Everything is ok&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I12: tooBig (1)&lt;/strong&gt; - The response is too big&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I13: noSuchName (2)&lt;/strong&gt; - Variable not found&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I14: BadValue (3)&lt;/strong&gt; - Incorrect value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I15: readOnly (4)&lt;/strong&gt; - Attempt to change read-only&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I16: genErr(5)&lt;/strong&gt; - Common error&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Extended Errors&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;I21: noAccess (6)&lt;/strong&gt; - No access rights&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I22: wrongType (7)&lt;/strong&gt; - Incorrect data type&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I23: wrongLength (8)&lt;/strong&gt; - Wrong length&lt;/li&gt;
&lt;li&gt;... etc. up to 18&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🗃️ &lt;strong&gt;Row Status (Row statuses of tables)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;For dynamic table management:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;nonExistent&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="n"&gt;createAndWait&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="n"&gt;notInService&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="n"&gt;destroy&lt;/span&gt;
               &lt;span class="n"&gt;createAndGo&lt;/span&gt; &lt;span class="err"&gt;────────────────┘&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;J1: nonExistent&lt;/strong&gt; - The row does not exist&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;J4/J5: createAndWait/createAndGo&lt;/strong&gt; - Create a string&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;J3: notInService&lt;/strong&gt; - Created but not active&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;J2: active&lt;/strong&gt; - Active and running&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;J7: destroy&lt;/strong&gt; - Delete line&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💾 &lt;strong&gt;Storage Types&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How long does the data persist?:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;K1: none&lt;/strong&gt; - Is not saved at all&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;K3: volatile&lt;/strong&gt; - In memory only (disappears after reboot)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;K4: nonVolatile&lt;/strong&gt; - Persists between reboots&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;K5: permanent&lt;/strong&gt; - Cannot be deleted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;K6: readonly&lt;/strong&gt; - Read-only&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌳 &lt;strong&gt;OID Structure (Object Tree)&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;1.3.6.1 (internet)&lt;/span&gt;
&lt;span class="s"&gt;├── 2.1 (mib-2)&lt;/span&gt; &lt;span class="c1"&gt;# Standard MIB objects&lt;/span&gt;
&lt;span class="s"&gt;├── 4.1 (enterprises)&lt;/span&gt;     &lt;span class="c1"&gt;# Vendor extensions&lt;/span&gt;
&lt;span class="s"&gt;6 (SNMPv2)&lt;/span&gt; &lt;span class="c1"&gt;# SNMPv2 features&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Each variable in the network has a unique OID!&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔧 &lt;strong&gt;Core Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;N11: uptime_string()&lt;/strong&gt; - Converts time to beautiful text&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;N12: xdump()&lt;/strong&gt; - Shows data in hex format&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;N21: snmp_parse_var_op()&lt;/strong&gt; - Parses ASN.1 data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;N22: snmp_build_var_op()&lt;/strong&gt; - Creates an ASN.1 data&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  But these are all constants, let's go deeper into the work of snmp.
&lt;/h4&gt;

&lt;h1&gt;
  
  
  How SNMP Works ? (Super Briefly)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. &lt;strong&gt;The basic architecture of SNMP&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flowchart TD
    A[SNMP Manager] --&amp;gt;|UDP 161: GET/SET| B[SNMP Agent]
    B --&amp;gt;|UDP 162: TRAP/INFORM| A
    B --&amp;gt; C[MIB Database]
    C --&amp;gt; D[System Variables]
    C --&amp;gt; E[Network Interfaces]
    C --&amp;gt; F[IP Routing Tables]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SNMP Manager&lt;/strong&gt;: The controlling system that initiates requests and receives notifications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SNMP Agent&lt;/strong&gt;: The managed device (router, switch, server) that responds to requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UDP 161&lt;/strong&gt;: Port for Manager → Agent communication (GET/SET requests)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UDP 162&lt;/strong&gt;: Port for Agent → Manager communication (TRAP/INFORM notifications)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIB Database&lt;/strong&gt;: Management Information Base - the database of managed objects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Variables&lt;/strong&gt;: Device information like system description, uptime, contact info&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Interfaces&lt;/strong&gt;: Interface statistics, status, configuration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IP Routing Tables&lt;/strong&gt;: Routing information, neighbor relationships&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. &lt;strong&gt;The GET request process&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sequenceDiagram
    participant M as SNMP Manager
    participant A as SNMP Agent
    participant D as MIB Database

    M-&amp;gt;&amp;gt;A: GET 1.3.6.1.2.1.1.1.0
    A-&amp;gt;&amp;gt;D: Lookup OID
    D-&amp;gt;&amp;gt;A: Return: Linux Server 5.4.0
    A-&amp;gt;&amp;gt;M: RESPONSE: Linux Server 5.4.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SNMP Manager&lt;/strong&gt;: Sends a GET request for a specific OID&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GET 1.3.6.1.2.1.1.1.0&lt;/strong&gt;: Request for system description (sysDescr.0)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SNMP Agent&lt;/strong&gt;: Receives request and processes it&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup OID&lt;/strong&gt;: Agent searches for the OID in its MIB database&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIB Database&lt;/strong&gt;: Contains the actual values for all managed objects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return: Linux Server 5.4.0&lt;/strong&gt;: MIB returns the value for the requested OID&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RESPONSE&lt;/strong&gt;: Agent sends back the value to the Manager&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complete cycle&lt;/strong&gt;: Shows the request-response pattern of SNMP operations&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. &lt;strong&gt;Types of SNMP messages&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flowchart TD
    A[SNMP PDUs] --&amp;gt; B[Request Types]
    A --&amp;gt; C[Response Types]
    A --&amp;gt; D[Notification Types]

    B --&amp;gt; B1[GET&amp;lt;br/&amp;gt;Get specific variable]
    B --&amp;gt; B2[GETNEXT&amp;lt;br/&amp;gt;Get next variable]
    B --&amp;gt; B3[GETBULK&amp;lt;br/&amp;gt;Get multiple variables]
    B --&amp;gt; B4[SET&amp;lt;br/&amp;gt;Modify variable]

    C --&amp;gt; C1[RESPONSE&amp;lt;br/&amp;gt;Reply to request]

    D --&amp;gt; D1[TRAP&amp;lt;br/&amp;gt;Unsolicited notification]
    D --&amp;gt; D2[INFORM&amp;lt;br/&amp;gt;Acknowledged notification]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SNMP PDUs&lt;/strong&gt;: Protocol Data Units - the message types in SNMP&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Types&lt;/strong&gt;: Messages sent from Manager to Agent

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;: Retrieve a specific variable by OID&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GETNEXT&lt;/strong&gt;: Get the next variable in MIB tree (for walking tables)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GETBULK&lt;/strong&gt;: Efficiently retrieve multiple rows from tables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SET&lt;/strong&gt;: Modify or write a variable value&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response Types&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RESPONSE&lt;/strong&gt;: Reply to any request, contains results or errors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notification Types&lt;/strong&gt;: Unsolicited messages from Agent to Manager

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TRAP&lt;/strong&gt;: Unacknowledged notification (fire-and-forget)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;INFORM&lt;/strong&gt;: Acknowledged notification (requires confirmation)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. &lt;strong&gt;SNMP Security Levels&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flowchart TD
    A[SNMPv3 Security] --&amp;gt; B[noAuthNoPriv&amp;lt;br/&amp;gt;No security]
    A --&amp;gt; C[authNoPriv&amp;lt;br/&amp;gt;Authentication only]
    A --&amp;gt; D[authPriv&amp;lt;br/&amp;gt;Auth + Encryption]

    B --&amp;gt; B1[No password&amp;lt;br/&amp;gt;Community string]
    C --&amp;gt; C1[HMAC-MD5/SHA&amp;lt;br/&amp;gt;Message integrity]
    D --&amp;gt; D1[DES/AES&amp;lt;br/&amp;gt;Data encryption]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SNMPv3 Security&lt;/strong&gt;: The security framework introduced in SNMP version 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;noAuthNoPriv&lt;/strong&gt;: No authentication, no encryption (least secure)

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Community string&lt;/strong&gt;: Plain text password like "public" or "private"&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;authNoPriv&lt;/strong&gt;: Authentication but no encryption

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HMAC-MD5/SHA&lt;/strong&gt;: Hash-based Message Authentication Codes for verifying message integrity&lt;/li&gt;
&lt;li&gt;Provides identity verification but data is sent in clear text&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;authPriv&lt;/strong&gt;: Both authentication and encryption (most secure)

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DES/AES&lt;/strong&gt;: Data encryption standards to protect message confidentiality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DES&lt;/strong&gt;: Data Encryption Standard (older)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AES&lt;/strong&gt;: Advanced Encryption Standard (modern, recommended)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 &lt;strong&gt;How does SNMP work in practice:&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Manager&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Device request&lt;/span&gt;
snmpget &lt;span class="nt"&gt;-v&lt;/span&gt; 2c &lt;span class="nt"&gt;-c&lt;/span&gt; public 192.168.1.1 1.3.6.1.2.1.1.1.0

&lt;span class="c"&gt;# Response&lt;/span&gt;
SNMPv2-MIB::sysDescr.0 &lt;span class="o"&gt;=&lt;/span&gt; STRING: Cisco IOS Software, Version 15.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;snmpget&lt;/strong&gt;: Command-line tool to send GET requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-v 2c&lt;/strong&gt;: SNMP version 2c (community-based security)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-c public&lt;/strong&gt;: Community string "public" (like a password)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;192.168.1.1&lt;/strong&gt;: IP address of the SNMP agent&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1.3.6.1.2.1.1.1.0&lt;/strong&gt;: OID for system description&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response format&lt;/strong&gt;: MIB::object = TYPE: value&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Agent - Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The pseudo-code of the SNMP agent&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// Waiting for a request on port 161&lt;/span&gt;
    &lt;span class="n"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;recvfrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;udp_socket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Parsing the SNMP package&lt;/span&gt;
    &lt;span class="n"&gt;pdu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parse_snmp_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Processing the request&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdu&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mib_lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdu&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;send_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;SET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;mib_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdu&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pdu&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;send_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SUCCESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Infinite loop&lt;/strong&gt;: Agent continuously listens for requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;recvfrom(udp_socket)&lt;/strong&gt;: Receives UDP packets on port 161&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;parse_snmp_packet()&lt;/strong&gt;: Decodes SNMP message (ASN.1 BER encoding)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pdu-&amp;gt;type&lt;/strong&gt;: Determines the type of request (GET, SET, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mib_lookup()&lt;/strong&gt;: Searches for OID in the Management Information Base&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mib_update()&lt;/strong&gt;: Modifies variable value for SET operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;send_response()&lt;/strong&gt;: Sends back the response to the manager&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MIB Database&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example of MIB variables&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;mib_entry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;oid:&lt;/span&gt; &lt;span class="s"&gt;"1.3.6.1.2.1.1.1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// sysDescr&lt;/span&gt;
    &lt;span class="nl"&gt;type:&lt;/span&gt; &lt;span class="n"&gt;STRING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;value:&lt;/span&gt; &lt;span class="s"&gt;"Linux Server 5.4.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;access:&lt;/span&gt; &lt;span class="n"&gt;READ_ONLY&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;mib_entry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;oid:&lt;/span&gt; &lt;span class="s"&gt;"1.3.6.1.2.1.2.2.1.10.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// ifInOctets.1&lt;/span&gt;
    &lt;span class="nl"&gt;type:&lt;/span&gt; &lt;span class="n"&gt;COUNTER32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;value:&lt;/span&gt; &lt;span class="mi"&gt;154729&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;access:&lt;/span&gt; &lt;span class="n"&gt;READ_ONLY&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;mib_entry&lt;/strong&gt;: Structure representing one managed object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;oid&lt;/strong&gt;: Object Identifier - unique address in the MIB tree

&lt;ul&gt;
&lt;li&gt;"1.3.6.1.2.1.1.1.0" = system.sysDescr.0 (system description)&lt;/li&gt;
&lt;li&gt;"1.3.6.1.2.1.2.2.1.10.1" = interfaces.ifInOctets.1 (input octets on interface 1)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;type&lt;/strong&gt;: Data type of the object

&lt;ul&gt;
&lt;li&gt;STRING: Text data&lt;/li&gt;
&lt;li&gt;COUNTER32: Monotonically increasing counter (32-bit)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;value&lt;/strong&gt;: Current value of the object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;access&lt;/strong&gt;: Permission level

&lt;ul&gt;
&lt;li&gt;READ_ONLY: Can only be read, not modified&lt;/li&gt;
&lt;li&gt;READ_WRITE: Can be both read and modified&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔥 &lt;strong&gt;Key points:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Manager&lt;/strong&gt; - Initiates requests (GET, SET) and receives notifications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agent&lt;/strong&gt; - Responds to requests, sends traps/informs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIB&lt;/strong&gt; - Database of managed variables with hierarchical OID structure&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OID&lt;/strong&gt; - Unique addresses of variables in dotted decimal notation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDUs&lt;/strong&gt; - Message Formats for different types of SNMP operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt; - Protection Levels (v3) providing authentication and encryption&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In fact:&lt;/strong&gt; SNMP allows remote control of network devices via a standardized protocol! 🌐&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Static Single Assignment Form (SSA)</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:43:24 +0000</pubDate>
      <link>https://dev.to/dima853/static-single-assignment-form-ssa-4gn3</link>
      <guid>https://dev.to/dima853/static-single-assignment-form-ssa-4gn3</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Static Single Assignment Form (SSA) and Its Role in Compiler Optimizations&lt;/strong&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Abstract&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Static Single Assignment (SSA) form is an intermediate representation of programs in which each variable is assigned a value only once. Developed in the 1980s by IBM researchers, this form has become a fundamental basis for most modern optimizing compilers, including LLVM, GNU Compiler Collection, and commercial compilers. This article discusses the principles of SSA, its advantages for compiler optimizations, as well as its connection with Escape Analysis and related optimizations such as stack allocation and scalar replacement.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In traditional intermediate representations, variables can be reassigned multiple times, which complicates data flow analysis and the execution of optimizations. SSA solves this problem by introducing a rule: each variable is assigned a value exactly once. To achieve this, original variables are split into versions, typically denoted by the original name with a subscript. At control flow merge points, where different versions of a variable may reach a single point, special Φ-functions are introduced to select the appropriate version based on the execution path.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Historical Background&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;SSA was developed in the 1980s by IBM researchers, including Kenneth Zadeck. In 1986, the concept of birthpoints and variable renaming to ensure single static assignment was presented. In 1988, Barry Rosen, Mark Wegman, and Kenneth Zadeck introduced the term "Static Single Assignment form" and replaced identity operations with Φ-functions. An efficient algorithm for converting programs to SSA form was presented in 1989.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Advantages of SSA Form&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Simplification of Data Flow Analysis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;SSA form significantly simplifies data flow analysis because each use of a variable has a single definition. For example, in the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y := 1
y := 2
x := y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in SSA form it becomes clear that the value of &lt;code&gt;y&lt;/code&gt; used in the third line comes from the second assignment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y₁ := 1
y₂ := 2
x₁ := y₂
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Optimizations Enhanced by SSA&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Constant folding&lt;/strong&gt; — evaluating expressions at compile time&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Value range propagation&lt;/strong&gt; — determining possible values of variables&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Dead-code elimination&lt;/strong&gt; — removing instructions that do not affect the result&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Global value numbering&lt;/strong&gt; — detecting redundant computations&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Partial-redundancy elimination&lt;/strong&gt; — eliminating duplicate computations&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Connection with Escape Analysis&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Principles of Escape Analysis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Escape Analysis determines whether references to objects can escape beyond a specific execution context. An object is considered "escaped" if it can be referenced from another thread, method, or external context after the method completes.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Interaction of SSA and Escape Analysis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;SSA form simplifies Escape Analysis by making data flows explicit. In SSA, each object is created at a specific point, and all uses refer to a particular version. This allows precise tracking of whether references to an object are passed to potential escape points.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of using SSA for escape analysis:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Original code&lt;/span&gt;
&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Object creation&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// Passing the reference&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// SSA form:&lt;/span&gt;
&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In SSA form, it is explicit that &lt;code&gt;local₁&lt;/code&gt; is passed to the &lt;code&gt;helper&lt;/code&gt; function, which allows analysis of whether &lt;code&gt;helper&lt;/code&gt; can store this reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Optimizations Based on SSA and Escape Analysis&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Stack Allocation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When escape analysis shows that an object does not leave the method, the compiler can place it on the stack frame instead of the heap. This eliminates the overhead of dynamic memory management and garbage collection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transformation to SSA allows precise determination of the object's lifetime:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Without optimization (heap)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Allocation on the heap&lt;/span&gt;
    &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// With optimization (stack)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example_optimized&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// In SSA: p₁.x = 1, p₁.y = 2&lt;/span&gt;
    &lt;span class="c1"&gt;// Fields are placed on the stack&lt;/span&gt;
    &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Scalar Replacement&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Objects that do not escape can be decomposed into individual scalar variables. SSA form facilitates this transformation because each variable already has a single definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of scalar replacement:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Original code&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;compute&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// After scalar replacement (in SSA form):&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;compute&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rect_width&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="n"&gt;rect_height&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rect_width&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;rect_height&lt;/span&gt;&lt;span class="err"&gt;₁&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Algorithmic Aspects of SSA&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Dominance and Dominance Frontiers Calculation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The concept of dominance frontiers is used for correct insertion of Φ-functions. Node A dominates node B if every path from the entry to B passes through A. The dominance frontier of node A is the set of nodes where A does not dominate directly but dominates an immediate predecessor.&lt;/p&gt;

&lt;p&gt;The dominance frontier calculation algorithm proposed by Keith Cooper et al. efficiently determines the insertion points for Φ-functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Minimal and Pruned SSA&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To reduce the number of Φ-functions, variants of SSA are used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Minimal SSA&lt;/strong&gt; — inserts only necessary Φ-functions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pruned SSA&lt;/strong&gt; — considers variable liveness, excluding Φ-functions for dead variables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Semi-pruned SSA&lt;/strong&gt; — excludes Φ-functions for variables that are not live upon entry to a basic block&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Practical Application in Compilers&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Compilers with SSA Support&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;LLVM&lt;/strong&gt; — uses SSA for all scalar values until the register allocation phase&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;GCC&lt;/strong&gt; — applies SSA in the GIMPLE intermediate representation&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;HotSpot JVM&lt;/strong&gt; — uses SSA in its JIT compiler&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;V8 JavaScript Engine&lt;/strong&gt; — implements SSA in the Crankshaft compiler&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example of Optimizations in LLVM&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;LLVM uses SSA form to represent the program until the register allocation phase. This enables aggressive optimizations such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Escape Analysis&lt;/strong&gt; to determine the possibility of stack allocation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalar replacement&lt;/strong&gt; of objects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Synchronization removal&lt;/strong&gt; for objects accessible to only one thread&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Extensions of SSA Form&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Static Single Use Form&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In this form, variables are renamed at each use, which is useful for lazy evaluation analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Static Single Information Form&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Variables are renamed upon assignment and at post-dominance frontiers, providing more precise information about values.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Extensions for Specific Capabilities&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;SSA modifications allow modeling high-level programming language constructs (arrays, objects, pointer aliases) and low-level architectural features (speculative execution, predication).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Static Single Assignment form is a powerful tool for compiler optimization. Its ability to explicitly represent data flows significantly simplifies program analysis and the execution of optimizations, including escape analysis, stack allocation, and scalar replacement. The development of SSA and related technologies continues to be an active area of research in compiler construction, contributing to the creation of more efficient and performant programming systems.&lt;/p&gt;

&lt;p&gt;The interaction of SSA form with modern optimization techniques demonstrates how fundamental concepts of compiler theory find practical application in real systems, providing significant performance gains while maintaining the semantic correctness of programs.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Steganography</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:42:57 +0000</pubDate>
      <link>https://dev.to/dima853/steganography-1ffp</link>
      <guid>https://dev.to/dima853/steganography-1ffp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Steganography&lt;/strong&gt; is the science and practice of &lt;strong&gt;hiding the fact that information is being transmitted&lt;/strong&gt;, not the information itself (that's cryptography).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9c%2FSteganography.png%2F350px-Steganography.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9c%2FSteganography.png%2F350px-Steganography.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The same image viewed by white, blue, green, and red lights reveals different hidden numbers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;If cryptography is like sending a note in a safe that everyone sees but can't open (or in an incomprehensible language that everyone hears but doesn't understand), then &lt;strong&gt;steganography&lt;/strong&gt; is like writing with invisible ink on the back of a regular postcard. &lt;strong&gt;The modern goal is not necessarily to achieve absolute invisibility, but to ensure that the stego-container is statistically indistinguishable from legitimate noise and doesn't attract targeted attention.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works in Practice (Techniques)
&lt;/h3&gt;

&lt;p&gt;The principle is always the same: hide a data container inside another, harmless carrier container without causing statistically significant anomalies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. In Images (Most Popular Method)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Basic Method:&lt;/strong&gt; Replacing Least Significant Bits (LSB).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Each pixel's color is encoded with numbers (e.g., in RGB). Changing the least significant bit alters the color by such a small amount that the human eye &lt;strong&gt;won't notice&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; You want to hide the letter 'A' (ASCII code 65, which is &lt;code&gt;01000001&lt;/code&gt; in binary) in 8 pixels. You replace the least significant bit of each pixel with one bit from the message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naive LSB replacement is a vulnerable and easily detectable method&lt;/strong&gt; through statistical analysis &lt;strong&gt;(histogram analysis, Chi-square tests)&lt;/strong&gt;. Modern adaptive methods (e.g., syndrome coding, spreading across multiple bit planes) actively combat anomalies. Basic LSB is used only for Proof-of-Concept or against incompetent adversaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1) &lt;strong&gt;Histogram Analysis&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it is:&lt;/strong&gt; A graphical representation of the distribution of values (e.g., pixel colors in an image).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Purpose in steganalysis:&lt;/strong&gt; To detect statistical anomalies that arise after data embedding.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A "clean" image has a smooth, continuous histogram.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naive LSB method&lt;/strong&gt; creates characteristic "steps" or paired values in the histogram because when replacing the least significant bit, pairs of values (0 and 1, 2 and 3, etc.) become almost equally probable. This is easily noticeable visually on a graph.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2) &lt;strong&gt;Chi-square Test (χ²)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F8%2F8e%2FChi-square_distributionCDF-English.png%2F1280px-Chi-square_distributionCDF-English.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F8%2F8e%2FChi-square_distributionCDF-English.png%2F1280px-Chi-square_distributionCDF-English.png" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Chi-squared_distribution" rel="noopener noreferrer"&gt;Chi-squared distribution&lt;/a&gt;, showing χ2 on the first axis and p-value (right tail probability) on the second axis.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. What is &lt;code&gt;k&lt;/code&gt; (Degrees of Freedom)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;In simple terms:&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt; is a parameter that indicates the "complexity" or "dimensionality" of our analysis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the context of steganalysis (LSB checking):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine we're analyzing an image's histogram.&lt;/li&gt;
&lt;li&gt;We look at pairs of values (0 and 1, 2 and 3, 4 and 5, etc.) that might have aligned due to LSB replacement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;k&lt;/code&gt; is the number of such pairs&lt;/strong&gt; we're checking.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The larger &lt;code&gt;k&lt;/code&gt;, the more data we're analyzing.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On the graph:&lt;/strong&gt; Each line (&lt;code&gt;k=1&lt;/code&gt;, &lt;code&gt;k=2&lt;/code&gt;, etc.) is a separate Chi-square distribution for a different number of degrees of freedom. Notice that as &lt;code&gt;k&lt;/code&gt; increases, the curve "flattens" and shifts to the right. &lt;strong&gt;(need to look "from the side of zero" (left to right))&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. What is &lt;code&gt;p-value&lt;/code&gt; (Probability / Significance Level)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;In simple terms:&lt;/strong&gt; &lt;code&gt;p-value&lt;/code&gt; is the &lt;strong&gt;probability that the result we observed (alignment of pairs in the histogram) occurred by chance.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the context of steganalysis:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;High &lt;code&gt;p-value&lt;/code&gt; (close to 1):&lt;/strong&gt; High probability that the histogram looks like this simply by chance. &lt;strong&gt;Conclusion: Steganography is likely NOT present.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Low &lt;code&gt;p-value&lt;/code&gt; (close to 0):&lt;/strong&gt; It's extremely unlikely that such perfect alignment of pairs happened by chance. &lt;strong&gt;Conclusion: The file likely contains hidden data embedded using the LSB method.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On the graph:&lt;/strong&gt; &lt;code&gt;p-value&lt;/code&gt; is shown on the vertical axis as &lt;strong&gt;"right tail probability"&lt;/strong&gt;. When our calculated Χ² value falls far into the right "tail" of the distribution (shaded area), the &lt;code&gt;p-value&lt;/code&gt; becomes very small. &lt;strong&gt;(p-value is determined by the horizontal axis (X²) from right to left, not by the vertical axis)&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Simply put:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Histogram analysis&lt;/strong&gt; is "look at the graph and see suspicious patterns".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chi-square test&lt;/strong&gt; is "calculate and get a number proving the file is suspicious".&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3) &lt;strong&gt;Syndrome Coding&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Essence:&lt;/strong&gt; This is not a data &lt;em&gt;embedding&lt;/em&gt; method, but a &lt;em&gt;matching&lt;/em&gt; method. It allows embedding a message &lt;strong&gt;without changing&lt;/strong&gt; some bits of the carrier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fen.lntwww.de%2Fimages%2F6%2F68%2FEN_KC_T_4_2_S2b_v2_neu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fen.lntwww.de%2Fimages%2F6%2F68%2FEN_KC_T_4_2_S2b_v2_neu.png" width="274" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Left Column: Syndromes (S)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;These are all possible &lt;strong&gt;syndromes&lt;/strong&gt; (3-bit checksums) that can be computed from a 7-bit block.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Right Column: "Coset Leaders" (e)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;These are &lt;strong&gt;minimum weight error vectors&lt;/strong&gt; (with the minimum number of ones) corresponding to each syndrome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each vector &lt;strong&gt;e&lt;/strong&gt; contains only one one - this is the minimal possible change!&lt;/p&gt;

&lt;p&gt;The table shows the minimal impact needed to obtain the desired "fingerprint" (syndrome).&lt;/p&gt;




&lt;h3&gt;
  
  
  How This Works in Steganography (briefly):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;STEP 1: Preparation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have a &lt;strong&gt;cover block&lt;/strong&gt; (7 bits from the image): &lt;code&gt;C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We have a &lt;strong&gt;message&lt;/strong&gt; (3 bits) we want to hide: &lt;code&gt;M&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;STEP 2: Compute syndrome of the cover block&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute the syndrome of the cover block: &lt;code&gt;S_cover = H × C&lt;/code&gt; (where H is the parity-check matrix)&lt;/li&gt;
&lt;li&gt;In the table, this corresponds to finding the syndrome &lt;code&gt;S_cover&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;STEP 3: Compare syndromes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;S_cover = M&lt;/code&gt;&lt;/strong&gt; → Already matches! &lt;strong&gt;Change nothing.&lt;/strong&gt; Use &lt;code&gt;e0 = (0,0,0,0,0,0,0)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;S_cover ≠ M&lt;/code&gt;&lt;/strong&gt; → Need to find an error vector &lt;code&gt;e&lt;/code&gt; to change the syndrome&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;STEP 4: Find error vector&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Look in the table for a syndrome &lt;code&gt;S&lt;/code&gt; equal to our target message &lt;code&gt;M&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Take the corresponding &lt;strong&gt;coset leader&lt;/strong&gt; &lt;code&gt;e&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;STEP 5: Embedding&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modify the cover block: &lt;code&gt;C_stego = C + e&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now the syndrome of the new block: &lt;code&gt;S_stego = H × (C + e) = H×C + H×e = S_cover + S = M&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Advantage:&lt;/strong&gt; Drastically reduces the number of carrier modifications. Ideally, only 1 bit per block is changed, making attacks based on LSB replacement statistics practically useless.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Spreading Across Multiple Bit Planes&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Essence:&lt;/strong&gt; Abandoning the naive approach where data is hidden only in the 1st least significant bit (LSB). Instead, the message is "spread" across several least significant bit planes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;

&lt;ol&gt;
&lt;li&gt; A pixel image can be represented as a "stack" of bit planes: from the most significant to the least significant.&lt;/li&gt;
&lt;li&gt; Naive LSB uses only the bottommost plane.&lt;/li&gt;
&lt;li&gt; Modern methods (e.g., &lt;strong&gt;HOLMES&lt;/strong&gt;) embed data simultaneously into the 1st, 2nd, and sometimes 3rd bit planes, adapting the embedding depth to the image texture.&lt;/li&gt;
&lt;li&gt; In complex textures (noise, grass), even higher-order bits can be changed, as the eye won't notice. In smooth areas (sky) — only the least significant ones.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advantage:&lt;/strong&gt; Drastically increases capacity and, more importantly, &lt;strong&gt;resistance to steganalysis&lt;/strong&gt;. Statistical anomalies arising from replacing only the 1st LSB are blurred and become indistinguishable from the image's natural noise.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Bit Planes
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Basic Concept
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;bit plane&lt;/strong&gt; of a digital discrete signal (such as an image or sound) is a set of bits corresponding to a specific &lt;strong&gt;bit position&lt;/strong&gt; in each of the binary numbers representing the signal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Simple example:&lt;/strong&gt;&lt;br&gt;
For 16-bit data representation, there are &lt;strong&gt;16 bit planes&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first&lt;/strong&gt; bit plane contains the set of &lt;strong&gt;most significant bits (MSB)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;sixteenth&lt;/strong&gt; contains the &lt;strong&gt;least significant bits (LSB)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F4%2F48%2FLichtenstein_bitplanes.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F4%2F48%2FLichtenstein_bitplanes.png" width="799" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The 8 bit-planes of a gray-scale image (the one on left). There are eight because the original image uses eight bits per pixel.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Significance of Bit Planes
&lt;/h2&gt;

&lt;p&gt;It can be observed that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first&lt;/strong&gt; bit plane gives the &lt;strong&gt;coarsest but most critical&lt;/strong&gt; approximation of the medium's values&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;higher the number&lt;/strong&gt; of the bit plane, the &lt;strong&gt;less&lt;/strong&gt; its contribution to the final result&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, &lt;strong&gt;adding each subsequent bit plane gives a better approximation&lt;/strong&gt; to the original value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mathematical Contribution of Bit Planes
&lt;/h2&gt;

&lt;p&gt;If a bit in the &lt;strong&gt;n-th&lt;/strong&gt; bit plane in an &lt;strong&gt;m-bit&lt;/strong&gt; dataset is set to 1, it contributes a value of &lt;strong&gt;2^(m−n)&lt;/strong&gt;, otherwise it contributes nothing. Therefore, bit planes can contribute &lt;strong&gt;half the value&lt;/strong&gt; of the previous bit plane.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example with the 8-bit value 10110101 (181 in decimal):&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit Plane&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Contribution&lt;/th&gt;
&lt;th&gt;Cumulative Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1st&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 × 2⁷ = 128&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2nd&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0 × 2⁶ = 0&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3rd&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 × 2⁵ = 32&lt;/td&gt;
&lt;td&gt;160&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4th&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 × 2⁴ = 16&lt;/td&gt;
&lt;td&gt;176&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5th&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0 × 2³ = 0&lt;/td&gt;
&lt;td&gt;176&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6th&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 × 2² = 4&lt;/td&gt;
&lt;td&gt;180&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7th&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0 × 2¹ = 0&lt;/td&gt;
&lt;td&gt;180&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8th&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 × 2⁰ = 1&lt;/td&gt;
&lt;td&gt;181&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Technical Note
&lt;/h2&gt;

&lt;p&gt;The term &lt;strong&gt;"bit plane"&lt;/strong&gt; is sometimes used as a synonym for &lt;strong&gt;"bitmap"&lt;/strong&gt;, however technically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The former refers to the &lt;strong&gt;location of data in memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The latter refers to the &lt;strong&gt;data itself&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Noise Analysis in Bit Planes
&lt;/h2&gt;

&lt;p&gt;One aspect of using bit planes is determining whether a bit plane is &lt;strong&gt;random noise&lt;/strong&gt; or contains &lt;strong&gt;meaningful information&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calculation method:&lt;/strong&gt;&lt;br&gt;
Compare each pixel (X, Y) with three neighboring pixels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(X − 1, Y)&lt;/li&gt;
&lt;li&gt;(X, Y − 1)&lt;/li&gt;
&lt;li&gt;(X − 1, Y − 1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a pixel &lt;strong&gt;matches at least two&lt;/strong&gt; of the three neighboring pixels, it is &lt;strong&gt;not considered noise&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Criterion:&lt;/strong&gt; A noisy bit plane will have between &lt;strong&gt;49% to 51% of pixels&lt;/strong&gt; that are noise.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Applications of Bit Planes
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1) Media File Formats
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Using PCM audio as an example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first bit&lt;/strong&gt; in a sample denotes the &lt;strong&gt;sign of the function&lt;/strong&gt; (determines half of the entire amplitude range)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;last bit&lt;/strong&gt; determines the &lt;strong&gt;exact value&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important principle:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changing &lt;strong&gt;more significant bits&lt;/strong&gt; leads to &lt;strong&gt;greater distortion&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Changing &lt;strong&gt;less significant bits&lt;/strong&gt; is less critical&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In lossy media compression using bit planes, this allows more freedom for encoding &lt;strong&gt;less significant bit planes&lt;/strong&gt;, while &lt;strong&gt;more significant ones must be preserved as accurately as possible&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pulse Code Modulation (PCM)
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Pulse-code modulation (PCM)&lt;/strong&gt; is used to digitize analog signals. Virtually all types of analog data &lt;strong&gt;(video, audio (voice, music), telemetry)&lt;/strong&gt; allow the use of &lt;strong&gt;PCM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PCM&lt;/strong&gt; is like "digitizing" sound or any other analog signal for use in the digital world. Without &lt;strong&gt;PCM&lt;/strong&gt;, we couldn't store music on computers or transmit voice over the internet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fe%2Fed%2FPcm-ru.svg%2F1920px-Pcm-ru.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fe%2Fed%2FPcm-ru.svg%2F1920px-Pcm-ru.svg.png" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example of 4-bit (16-level) PCM. Shows quantization of an analog signal and bursts of impulses encoding the samples. Transmission in the channel is performed with the most significant bits first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Modulation
&lt;/h1&gt;

&lt;p&gt;Example of 4-bit (16-level) PCM. Shows quantization of an analog signal and bursts of impulses encoding the samples. Transmission in the channel is performed with the most significant bits first.&lt;/p&gt;

&lt;h2&gt;
  
  
  PCM Operating Principle
&lt;/h2&gt;

&lt;p&gt;In pulse-code modulation, the analog transmitted signal is converted into digital form through three operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time sampling&lt;/strong&gt; (measuring the analog signal at equal time intervals (obtaining samples))&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Amplitude quantization&lt;/strong&gt; (rounding each sample to the nearest level from a finite set of values)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encoding&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Converting Analog Signal to Digital
&lt;/h3&gt;

&lt;p&gt;An analog-to-digital converter (ADC) is used to convert an analog signal to digital. The ADC measures the amplitude of the analog signal at equal intervals - obtains instantaneous values or signal samples, then converts the samples into binary words.&lt;/p&gt;

&lt;p&gt;The measured instantaneous value (sample) of the analog signal is quantized by levels (rounded to the nearest integer). The number of quantization levels is usually equal to or a multiple of an integer power of 2, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2³ = 8 levels&lt;/li&gt;
&lt;li&gt;2⁴ = 16 levels&lt;/li&gt;
&lt;li&gt;2⁵ = 32 levels&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The level number is encoded with binary words of length 3, 4, 5, etc. bits.&lt;/p&gt;

&lt;h3&gt;
  
  
  Forming Signal for Transmission
&lt;/h3&gt;

&lt;p&gt;Then the ADC's output words in parallel code are encoded by feeding them to a shift register clocked by an auxiliary shift generator. At the output of the shift register, bursts of encoded impulses in serial code are formed. Then the impulse bursts are transmitted into the communication channel.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: An impulse burst is periodically repeating impulses over a fixed time interval.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Sampling Frequency
&lt;/h3&gt;

&lt;p&gt;The signal sampling frequency (or digitization rate, sampling frequency) to avoid information loss, according to the Nyquist–Shannon sampling theorem, must be at least twice the maximum frequency in the analog signal's spectrum.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Implementation
&lt;/h3&gt;

&lt;p&gt;There are specialized integrated circuits designed for PCM, combining ADC, shift register, clock generators, and other devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demodulation
&lt;/h2&gt;

&lt;p&gt;A demodulator is installed at the receiving end of the communication channel. In the demodulator, impulse bursts are fed to the serial input of a shift register. After shifting all bits of the impulse burst into the shift register, the word from the shift register in parallel code is written to the input register of a digital-to-analog converter (DAC).&lt;/p&gt;

&lt;p&gt;The DAC converts the encoded samples of the transmitted analog signal back into analog form. A stepped analog signal is formed at the DAC output. Smoothing of the steps is performed by a low-pass filter (LPF), at the output of which the transmitted analog signal is formed. The LPF cutoff frequency is chosen to be less than or equal to twice the sampling frequency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Digital Codes in PCM
&lt;/h2&gt;

&lt;p&gt;A wide variety of binary codes are used to encode samples in PCM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ordinary representation of numbers in the binary numeral system, with sequential transmission of bits of the binary number can be done either least significant bits first or most significant bits first&lt;/li&gt;
&lt;li&gt;Various codes with error detection and correction in the transmission channel, for example, Hamming code, Reed–Solomon code, etc. The simplest of them is a redundant code with parity bit transmission&lt;/li&gt;
&lt;li&gt;Codes that eliminate the DC component in the encoded two-level impulse signal, for example, self-synchronizing Manchester code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  PCM Variants
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Differential Pulse-Code Modulation (DPCM)
&lt;/h3&gt;

&lt;p&gt;PCM combined with delta encoding, where the signal is encoded as the difference between the current and previous measured values. For audio data, this modulation method reduces the required number of bits per sample by about 25%.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adaptive DPCM (ADPCM)&lt;/strong&gt; — a variant of DPCM with variable quantization step size. Changing the step size allows reducing bandwidth requirements for a given signal-to-noise ratio.&lt;/p&gt;

&lt;h3&gt;
  
  
  LPCM (Linear pulse code modulation)
&lt;/h3&gt;

&lt;p&gt;Linear pulse-code modulation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Application
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In digital and IP telephony, PCM is used to convert voice audio signals into a digital stream transmitted at 64 kbit/s (primary digital channel)&lt;/li&gt;
&lt;li&gt;PCM is used to convert analog audio signals to digital for storing signals on digital devices and media (digital audio recording). File formats: WAV, MP3, WMA, OGG, FLAC, APE&lt;/li&gt;
&lt;li&gt;PCM was previously used in modem communication protocols ITU V.90 (only incoming signal to the client) and V.92 (incoming and outgoing signal) to provide a maximum connection speed of 56 kbit/s&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2) Raster Displays
&lt;/h2&gt;

&lt;p&gt;Some computers displayed graphics in bit plane format, specifically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;PCs with EGA video cards&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amiga&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Atari ST&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This contrasted with the more common packed format. This organization allowed performing certain classes of image operations using &lt;strong&gt;bitwise operations&lt;/strong&gt; (especially with the &lt;strong&gt;blitter&lt;/strong&gt; chip), as well as creating &lt;strong&gt;parallax scrolling&lt;/strong&gt; effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  3) Motion Estimation in Video
&lt;/h2&gt;

&lt;p&gt;Some &lt;strong&gt;motion estimation&lt;/strong&gt; algorithms can be performed using bit planes (e.g., after applying a filter to convert salient edge features into binary values).&lt;/p&gt;

&lt;p&gt;This can sometimes provide a &lt;strong&gt;good enough approximation&lt;/strong&gt; for correlation operations with &lt;strong&gt;minimal computational cost&lt;/strong&gt;. This method is based on the observation that &lt;strong&gt;spatial information is more significant than actual values&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Technical detail:&lt;/strong&gt;&lt;br&gt;
Convolutions can be reduced to &lt;strong&gt;bit shift and popcount operations&lt;/strong&gt;, or performed in specialized hardware.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  4) Neural Networks
&lt;/h2&gt;

&lt;p&gt;Bit plane formats can be used to feed images into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spiking neural networks&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Neural networks/convolutional neural networks with low-precision&lt;/strong&gt; approximations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5) Software
&lt;/h2&gt;

&lt;p&gt;Many image processing packages can &lt;strong&gt;split an image into bit planes&lt;/strong&gt;. Open-source tools include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Pamarith&lt;/code&gt;&lt;/strong&gt; from the &lt;strong&gt;Netpbm&lt;/strong&gt; package&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Convert&lt;/code&gt;&lt;/strong&gt; from &lt;strong&gt;ImageMagick&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;2. In Audio and Video&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Same principle:&lt;/strong&gt; Making changes imperceptible to human hearing/vision in the audio track (masking in critical hearing bands, modifying phase spectrum) or in video frames. For example, adding quiet echo signals with specific delays that encode information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. In Text&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Methods:&lt;/strong&gt; Changing the number of spaces or tabs (difficult to detect visually but trivially revealed by analyzing source code), using invisible Unicode characters (Zero-Width Joiner, Zero-Width Non-Joiner), algorithmic synonymization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most primitive historical example:&lt;/strong&gt; Pricking specific letters in text with a pin that form a secret message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. In Network Traffic (Network Steganography)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Methods:&lt;/strong&gt; Hiding data in service fields of network packet headers (e.g., in IP ID, TTL field, or flags), in the time delay between packets (timing channels), or in "empty" TCP segments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;This is especially dangerous&lt;/strong&gt; because detecting such traffic with standard protection tools (firewalls, IDS) is almost impossible, especially inside an encrypted channel (HTTPS), which hides the stego-carrier itself from deep packet inspection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Steganography vs Cryptography
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter&lt;/th&gt;
&lt;th&gt;Cryptography&lt;/th&gt;
&lt;th&gt;Steganography&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Main Goal&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Hide the CONTENT&lt;/strong&gt; of the message&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Hide the VERY FACT&lt;/strong&gt; of the message's existence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;If Discovered&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Enemy knows about the existence of a secret message. Its content (hopefully) is protected.&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;COMPLETE FAILURE.&lt;/strong&gt; The very concept of secrecy is destroyed.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Attention&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Can attract attention (encrypted correspondence is suspicious by itself)&lt;/td&gt;
&lt;td&gt;Avoids attention (looks like normal traffic)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Protection&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mathematical strength of algorithms and key length&lt;/td&gt;
&lt;td&gt;Statistical indistinguishability from the original carrier and resistance to steganalysis&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Important conclusion:&lt;/strong&gt; In serious systems, they are &lt;strong&gt;combined&lt;/strong&gt;. First, the message is encrypted (cryptography), then hidden in a carrier (steganography). This way, even if the enemy discovers the hidden data, they cannot read it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real Threats and Applications
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Cyber Espionage and APT Attacks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it's used:&lt;/strong&gt; Malware steals data and exfiltrates it, disguising it as normal traffic. Common scheme: data is hidden in an image uploaded to a public resource (forum, GitHub, cloud), and the malware on the compromised machine downloads it. Encrypted HTTPS connection hides the stego-carrier from inspection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Circumventing Censorship in Totalitarian Regimes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it's used:&lt;/strong&gt; Activists and journalists use steganography to transmit information through blocked channels without attracting the attention of censors who look for keywords in plain text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Concealing Criminal Activity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it's used:&lt;/strong&gt; Criminal communities exchange instructions and data by hiding them in files posted on public forums and cloud storage. &lt;strong&gt;Important clarification:&lt;/strong&gt; using social media is problematic as they often recompress images, destroying the stego-container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Digital Watermarks (the other side)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;This is a legal application:&lt;/strong&gt; Copyright holders embed invisible marks into their content to confirm authorship and track leaks. This is steganography where the goal is not to hide the fact of transmission, but to hide the mark itself until the moment of verification.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Detection (Steganalysis)
&lt;/h3&gt;

&lt;p&gt;Steganalysis is the art of detecting steganography.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Methods:&lt;/strong&gt; Statistical analysis of the carrier file for anomalies (histogram analysis, Chi-square tests), entropy analysis, searching for traces of specific steganography tools, machine learning to identify patterns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard truth:&lt;/strong&gt; A universal detector does not exist. Detection is &lt;strong&gt;difficult and often impossible without a hypothesis about the method used.&lt;/strong&gt; However, against specific, known methods (like naive LSB), steganalysis can be highly effective. Channel capacity is low, but it's sufficient for transmitting keys, passwords, or control commands.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Verdict
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Steganography is a powerful, dangerous, and often underestimated tool.&lt;/strong&gt; In the cybersecurity arsenal, it represents a &lt;strong&gt;persistent and hard-to-detect threat&lt;/strong&gt; because an attack based on it is practically invisible against the background of legitimate traffic. This is not a "zero-day vulnerability", but a fundamental technique leading to an eternal battle between concealment and detection methods. For an infosec specialist, understanding steganography is not an option but a necessity, especially during incident investigation and defense against targeted attacks.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Transaction Processing or Analytics?</title>
      <dc:creator>dima853</dc:creator>
      <pubDate>Tue, 18 Aug 2026 17:42:19 +0000</pubDate>
      <link>https://dev.to/dima853/transaction-processing-or-analytics-3flk</link>
      <guid>https://dev.to/dima853/transaction-processing-or-analytics-3flk</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Transactions
&lt;/h2&gt;

&lt;p&gt;In the early days of business data processing, writing to a database typically corresponded to a &lt;strong&gt;commercial transaction&lt;/strong&gt;: selling a product 🛍️, ordering from a supplier 📦, paying employee salaries 💰, etc. Over time, the term &lt;strong&gt;"transaction"&lt;/strong&gt; became established even for operations not related to money and came to mean &lt;strong&gt;a group of read and write operations forming a logical unit&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;💥 &lt;strong&gt;Atomicity&lt;/strong&gt; — the minimum mandatory property of any transaction&lt;/li&gt;
&lt;li&gt;🛡️ &lt;strong&gt;Full ACID&lt;/strong&gt; — part of reliability for critical operations&lt;/li&gt;
&lt;li&gt;⚖️ &lt;strong&gt;In practice&lt;/strong&gt; — different systems choose a balance between reliability (ACID) and performance (weakened guarantees)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🔄 OLTP vs OLAP: Two Different Worlds
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Online Transaction Processing (OLTP)&lt;/strong&gt; — a type of database system used in &lt;em&gt;transaction-oriented applications&lt;/em&gt; &lt;strong&gt;(banks, accounting systems (ERP, CRM), booking systems, payment systems, etc.)&lt;/strong&gt;, such as many operational (core) business systems.&lt;/p&gt;

&lt;h4&gt;
  
  
  Two contexts where the term "transaction" is often used:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Type 1: Computer Transactions&lt;/strong&gt; 💻&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Atomic data modification&lt;/strong&gt; in a database&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;BEGIN TRANSACTION → UPDATE → COMMIT&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Type 2: Business Transactions&lt;/strong&gt; 💰&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Economic exchange&lt;/strong&gt; between parties&lt;/li&gt;
&lt;li&gt;Example: product sale, bank transfer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How they're related:&lt;/strong&gt;&lt;br&gt;
OLTP uses &lt;strong&gt;technical transactions (type 1)&lt;/strong&gt; to record &lt;strong&gt;business operations (type 2)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💳 &lt;em&gt;Store purchase&lt;/em&gt; (business transaction)&lt;/li&gt;
&lt;li&gt;is recorded via 🖥️ &lt;em&gt;SQL transaction&lt;/em&gt; in the database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; Technical transactions = tool for recording business transactions 🔧→📊&lt;/p&gt;
&lt;h3&gt;
  
  
  OLTP (Online Transaction Processing)
&lt;/h3&gt;

&lt;p&gt;Even when databases started being used for various types of data (blog comments 📝, game actions 🎮, contacts in address books 👥, etc.), the main access model remained similar to business transaction processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OLTP Characteristics:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Searching for a small number of records by key 🔑&lt;/li&gt;
&lt;li&gt;Inserting and updating data based on user input&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interactive&lt;/strong&gt; mode of operation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;C Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example OLTP operation: finding customer information by ID&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Customer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Customer&lt;/span&gt; &lt;span class="nf"&gt;find_customer_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// In a real system, there would be a database query here&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Customer&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Ivan Ivanov"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  OLAP (Online Analytic Processing)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Online Analytical Processing (OLAP)&lt;/strong&gt; — an approach for quickly answering multidimensional analytical queries.&lt;/p&gt;

&lt;p&gt;Databases also started being used for &lt;strong&gt;data analytics&lt;/strong&gt;, which implies a completely different access model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.techtarget.com%2Frms%2Fonlineimages%2Fcrm-olap_mobile.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.techtarget.com%2Frms%2Fonlineimages%2Fcrm-olap_mobile.png" width="560" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;*Data Warehousing&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;*OLAP Cube&lt;/strong&gt; (from Online Analytical Processing) — a multidimensional data structure designed for fast and efficient analysis of large volumes of business data from various perspectives.&lt;br&gt;
&lt;strong&gt;In simple terms&lt;/strong&gt;, an OLAP cube can be compared to a pivot table in Excel, but much more powerful and capable of working with huge amounts of information and multiple parameters (dimensions).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Let's Look at OLAP-CUBE
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F5%2F52%2FOLAP_Cube.svg%2F1024px-OLAP_Cube.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F5%2F52%2FOLAP_Cube.svg%2F1024px-OLAP_Cube.svg.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;OLAP data is typically stored in "star" or "snowflake" schemas in a relational data warehouse or specialized data management system. Metrics are derived from records in the fact table, and dimensions are from dimension tables.&lt;br&gt;
&lt;strong&gt;but&lt;/strong&gt;!&lt;br&gt;
Classic OLAP cubes (like in Microsoft Analysis Services) often store data in proprietary multidimensional formats, not in relational "star" tables. The "star" itself is the raw data storage schema on which the cube is built. Additionally, modern MPP systems (Massively Parallel Processing) like ClickHouse or Amazon Redshift often don't use cubes in the classical sense but provide similar speed for OLAP queries directly to relational tables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  🧊 OLAP Cube (Data Cube)
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;🎲 &lt;strong&gt;The term "cube"&lt;/strong&gt; refers to a multidimensional dataset sometimes called a &lt;strong&gt;hypercube&lt;/strong&gt; if the number of dimensions is more than three.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Basic Concepts:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;📦 Cube&lt;/strong&gt; — a multidimensional generalization of a two-dimensional spreadsheet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example: 3D data cube
&lt;/span&gt;&lt;span class="n"&gt;dimensions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Products&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Time&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Regions&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;measures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sales&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Profit&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Budget&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Slice
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fa%2Fa6%2FOLAP_slicing_en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fa%2Fa6%2FOLAP_slicing_en.png" width="800" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Slice&lt;/strong&gt; - the process of selecting a rectangular subset of a cube by choosing a single value for one of its dimensions, creating a new cube with one less dimension.&lt;br&gt;
&lt;strong&gt;The figure shows the slicing operation:&lt;/strong&gt; sales metrics in all sales regions and all product categories of the company for 2005 and 2006 are "cut out" from the data cube.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Dice
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fc%2Fc7%2FOLAP_dicing_en.png%2F500px-OLAP_dicing_en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fc%2Fc7%2FOLAP_dicing_en.png%2F500px-OLAP_dicing_en.png" width="500" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dice:&lt;/strong&gt; The dice operation creates a subcube allowing the analyst to select specific values from multiple dimensions.&lt;br&gt;
&lt;strong&gt;The figure shows the dice operation:&lt;/strong&gt; the new cube displays sales data for a limited number of product categories, with time and region dimensions covering the same range as before.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Drill Down/Up
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9b%2FOLAP_drill_up%2526down_en.png%2F500px-OLAP_drill_up%2526down_en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9b%2FOLAP_drill_up%2526down_en.png%2F500px-OLAP_drill_up%2526down_en.png" width="500" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Drill down/up&lt;/strong&gt; allows the user to navigate between data levels, from the most summarized (up) to the most detailed (down).&lt;br&gt;
&lt;strong&gt;The figure shows the drill-down operation:&lt;/strong&gt; the analyst moves from the summary category "Outdoor Protective Equipment" to sales metrics of individual products.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Roll-Up
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fd%2Fd6%2FOLAP_pivoting_en.png%2F500px-OLAP_pivoting_en.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fd%2Fd6%2FOLAP_pivoting_en.png%2F500px-OLAP_pivoting_en.png" width="500" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Roll-up&lt;/strong&gt; involves summarizing data along a dimension. The summarization rule can be an aggregate function, for example, to calculate totals along a hierarchy or apply a set of formulas such as &lt;strong&gt;"profit = sales - expenses"&lt;/strong&gt;. 📊💰&lt;/p&gt;

&lt;h2&gt;
  
  
  Computational Complexity
&lt;/h2&gt;

&lt;p&gt;Common aggregate functions can be &lt;strong&gt;expensive to compute&lt;/strong&gt; when rolling up: ❌&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🐌 &lt;strong&gt;If they cannot be defined from the cube cells&lt;/strong&gt;, they must be computed from the underlying data&lt;/li&gt;
&lt;li&gt;⏳ Either compute them &lt;strong&gt;online (slowly)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;💾 Or &lt;strong&gt;precompute&lt;/strong&gt; them for possible roll-up &lt;strong&gt;(large volume)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Efficient Aggregate Functions
&lt;/h2&gt;

&lt;p&gt;Aggregate functions that can be defined from cells are known as &lt;strong&gt;decomposable aggregate functions&lt;/strong&gt; and allow &lt;strong&gt;efficient computation&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  💰 Cost of Aggregate Functions When Rolling Up
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ Problematic Functions (expensive to compute):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;📊 Median (MEDIAN)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires complete data sorting for each aggregation level&lt;/li&gt;
&lt;li&gt;Lacks the &lt;strong&gt;composability&lt;/strong&gt; property - median of subgroups ≠ median of entire group&lt;/li&gt;
&lt;li&gt;On-the-fly computation requires storing all original values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🎯 Percentiles (PERCENTILE)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to median, require access to the entire dataset&lt;/li&gt;
&lt;li&gt;90th percentile cannot be computed from 90th percentiles of subgroups&lt;/li&gt;
&lt;li&gt;Must store the complete data distribution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🔢 Mode (MODE)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires counting frequencies of all values&lt;/li&gt;
&lt;li&gt;The most frequent value in subgroups may not match the overall mode&lt;/li&gt;
&lt;li&gt;Requires complete recount for accurate determination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;📈 Standard Deviation (STDDEV)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not an &lt;strong&gt;additive&lt;/strong&gt; function (In the context of aggregate functions, "additivity" means the ability to compute the overall result from partial results.)&lt;/li&gt;
&lt;li&gt;Requires knowing the mean value and number of elements&lt;/li&gt;
&lt;li&gt;For accurate calculation, need ∑x and ∑x² for all data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;✅ Easily supported:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;COUNT&lt;/code&gt; 🔢&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MAXIMUM&lt;/code&gt; ⬆️&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MINIMUM&lt;/code&gt; ⬇️&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SUM&lt;/code&gt; ➕&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Because they can be computed for each OLAP cube cell and then rolled up&lt;/strong&gt;, since the total sum (or count, etc.) is the sum of sub-sums.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;❌ Difficult to support:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MEDIAN&lt;/code&gt; 📊&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Because it must be computed for each view separately&lt;/strong&gt;: the median of a set is &lt;strong&gt;not&lt;/strong&gt; the median of medians of subsets.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔢 Mathematical Definition
&lt;/h2&gt;

&lt;p&gt;(simplified) Mathematically, an OLAP cube is a projection of an RDBMS relation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f: (X, Y, Z) → W
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;X, Y, Z&lt;/strong&gt; — cube axes (dimensions) 📐&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;W&lt;/strong&gt; — data filling each cell 💾&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Projection example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g: (Product, Time) → Sales
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  💾 Data Storage
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OLAP data is typically stored in:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🌟 &lt;strong&gt;Star Schema&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;❄️ &lt;strong&gt;Snowflake Schema&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;🗄️ Specialized data management systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Explanation (briefly)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;🌟 Star Schema&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Denormalized&lt;/strong&gt; schema&lt;/li&gt;
&lt;li&gt;All dimension tables linked &lt;strong&gt;DIRECTLY&lt;/strong&gt; to the fact table&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simpler and faster&lt;/strong&gt; for queries&lt;/li&gt;
&lt;li&gt;Easier to understand&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;❄️ Snowflake Schema&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Normalized&lt;/strong&gt; schema&lt;/li&gt;
&lt;li&gt;Dimension tables &lt;strong&gt;SPLIT&lt;/strong&gt; into sub-tables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More complex&lt;/strong&gt;, but saves space&lt;/li&gt;
&lt;li&gt;Slower due to more JOINs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What this means in practice:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Star&lt;/strong&gt; → for analysis speed 📊⚡&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Snowflake&lt;/strong&gt; → for storage economy 💾📉&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practical Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  💼 Business Intelligence:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Business cube dimensions:
&lt;/span&gt;&lt;span class="n"&gt;dimensions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Products 📦&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Time 📅&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Regions 🌍&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sales_Channels 🏪&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Customers 👥&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Measures (metrics):
&lt;/span&gt;&lt;span class="n"&gt;measures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sales_Volume 💰&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Profit 📈&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Order_Count 🔢&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Average_Check 💵&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🏪 Retail Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cube: "Store Sales"
├── Dimensions:
│   ├── Time: Year → Quarter → Month → Day
│   ├── Products: Category → Brand → Model
│   ├── Stores: Region → City → Address
│   └── Customers: Segment → Demographics
└── Measures:
    ├── Sales Amount
    ├── Quantity Sold
    ├── Profit
    └── Returns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔗 Related Technologies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🌐 Business Intelligence:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;📊 &lt;strong&gt;Data Mining&lt;/strong&gt; — data mining&lt;/li&gt;
&lt;li&gt;🏪 &lt;strong&gt;Data Mart&lt;/strong&gt; — data marts&lt;/li&gt;
&lt;li&gt;📈 &lt;strong&gt;MDX&lt;/strong&gt; — Multidimensional Expressions&lt;/li&gt;
&lt;li&gt;🔍 &lt;strong&gt;XML for Analysis&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🛠️ Popular OLAP Systems:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Microsoft Analysis Services&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle OLAP&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Apache Kylin&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ClickHouse&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💡 Key Advantages of OLAP Cube
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Advantage&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;⚡ Fast Response&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Instant answers to complex analytical queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;🔍 Multidimensionality&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Data analysis from different perspectives&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;📊 Flexibility&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Interactive data exploration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;🎯 Intuitiveness&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Visualization understandable to business users&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  📋 Comparison Table: OLTP vs OLAP
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OLTP&lt;/strong&gt; is typically contrasted with &lt;strong&gt;Online Analytical Processing (OLAP)&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Characteristic&lt;/th&gt;
&lt;th&gt;OLTP 🚀&lt;/th&gt;
&lt;th&gt;OLAP 📊&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Query Types&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;All types (read, insert, update, delete)&lt;/td&gt;
&lt;td&gt;Mostly read-only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Complexity&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Simple queries&lt;/td&gt;
&lt;td&gt;Complex analytical queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Volume&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Large number of simple queries&lt;/td&gt;
&lt;td&gt;Smaller number of complex queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Operation processing&lt;/td&gt;
&lt;td&gt;Business analytics and reporting&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  🏢 Data Warehousing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why do we need a data warehouse?
&lt;/h3&gt;

&lt;p&gt;Large companies can have &lt;strong&gt;dozens of different transaction processing systems&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer websites 🌐&lt;/li&gt;
&lt;li&gt;Point-of-sale systems (cash registers) in stores 🏪&lt;/li&gt;
&lt;li&gt;Warehouse inventory tracking systems 📦&lt;/li&gt;
&lt;li&gt;Transportation route planning systems 🚚&lt;/li&gt;
&lt;li&gt;Human resources management systems 👥&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: OLTP systems must be highly available and process transactions with low latency. Analytical queries often require scanning large volumes of data and can interfere with transaction operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: &lt;strong&gt;Data Warehouse&lt;/strong&gt; — a separate database where analysts can run any queries without affecting OLTP operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔄 ETL Process (Extract–Transform–Load)
&lt;/h3&gt;

&lt;p&gt;Data enters the warehouse through the &lt;strong&gt;ETL&lt;/strong&gt; process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Extract&lt;/strong&gt; - data is extracted from OLTP systems&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transform&lt;/strong&gt; - data is transformed into a schema convenient for analysis&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load&lt;/strong&gt; - data is loaded into the warehouse&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Real-life example&lt;/strong&gt;: A large bank 🏦 might have separate systems for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mobile banking 📱&lt;/li&gt;
&lt;li&gt;Credit operations 💳&lt;/li&gt;
&lt;li&gt;Mortgage products 🏠&lt;/li&gt;
&lt;li&gt;Investment services 📈&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All this data is combined in a data warehouse for end-to-end analytics.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⭐ Star and ❄️ Snowflake: Schemas for Analytics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🌟 Star Schema
&lt;/h3&gt;

&lt;p&gt;At the center of the schema is a &lt;strong&gt;fact table&lt;/strong&gt;, surrounded by &lt;strong&gt;dimension tables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example for retail&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fact_sales&lt;/code&gt; (sales fact table) - at the center&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dim_product&lt;/code&gt; (product dimension) 📦&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dim_store&lt;/code&gt; (store dimension) 🏪&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dim_time&lt;/code&gt; (time dimension) ⏰&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dim_customer&lt;/code&gt; (customer dimension) 👥&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;C Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Structure for sales fact table&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;FactSale&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sale_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// reference to dim_product&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;store_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// reference to dim_store&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;time_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// reference to dim_time&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// reference to dim_customer&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Structure for product dimension table&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;DimProduct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ❄️ Snowflake Schema
&lt;/h3&gt;

&lt;p&gt;A more normalized version of the star schema where dimensions are further broken down into sub-dimensions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantage of star schema&lt;/strong&gt;: simpler for analysts!&lt;/p&gt;

&lt;h2&gt;
  
  
  💾 Data Warehouse Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wide tables&lt;/strong&gt;: fact tables often have more than 100 columns 📋&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rich metadata&lt;/strong&gt;: dimension tables contain all possible information for analysis&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimized for analytical queries&lt;/strong&gt;: special storage structures and algorithms&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Quick Summary: OLTP vs OLAP
&lt;/h1&gt;

&lt;h2&gt;
  
  
  🎯 &lt;strong&gt;Key Differences&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OLTP (Online Transaction Processing)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💰 &lt;strong&gt;Real-time operation processing&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;🔄 &lt;strong&gt;Short transactions&lt;/strong&gt; (read/write)&lt;/li&gt;
&lt;li&gt;⚡ &lt;strong&gt;High availability&lt;/strong&gt;, instant response&lt;/li&gt;
&lt;li&gt;🏦 &lt;strong&gt;Examples&lt;/strong&gt;: bank transfers, orders, bookings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OLAP (Online Analytical Processing)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📊 &lt;strong&gt;Analytics and reporting&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;🔍 &lt;strong&gt;Complex queries&lt;/strong&gt; on big data&lt;/li&gt;
&lt;li&gt;📈 &lt;strong&gt;Multidimensional analysis&lt;/strong&gt; (cubes, slices, drill-down)&lt;/li&gt;
&lt;li&gt;🧠 &lt;strong&gt;Examples&lt;/strong&gt;: business intelligence, forecasting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🏗️ &lt;strong&gt;Architectural Features&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OLTP&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ACID transactions 🛡️&lt;/li&gt;
&lt;li&gt;Normalized schemas&lt;/li&gt;
&lt;li&gt;Write optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OLAP&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Denormalized schemas (🌟 &lt;strong&gt;Star&lt;/strong&gt;/❄️ &lt;strong&gt;Snowflake&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Data warehouses&lt;/li&gt;
&lt;li&gt;ETL processes&lt;/li&gt;
&lt;li&gt;Read optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 &lt;strong&gt;What This Means in Practice&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;OLTP&lt;/th&gt;
&lt;th&gt;OLAP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Business operations 🚀&lt;/td&gt;
&lt;td&gt;Business analysis 📈&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Data&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Current operations&lt;/td&gt;
&lt;td&gt;Historical data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Users&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Operators&lt;/td&gt;
&lt;td&gt;Analysts&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: OLTP ensures the company's daily operations, while OLAP helps understand trends and make strategic decisions. Both approaches are critically important but solve different problems.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>javascript</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
