Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ

วิเคราะห์การเพิ่มขึ้นของหน่วยความจำ ตรวจสอบฮีปของแต่ละกระบวนการ และปรับแต่งการเก็บคืนหน่วยความจำของ Erlang เพื่อให้โหนดที่ทำงานยาวนานมีเสถียรภาพ

บทเรียน 4 จาก 413 ขั้นตอน

การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Memory Matters

Erlang nodes can run for months. A small per-process leak or unbounded mailbox grows silently until the node is killed by the OS. Observability into memory is essential.

System-Wide Memory

erlang:memory/0 returns a breakdown: total, processes, atom, binary, ETS and more. Watch the categories that grow over time.

erlang:memory().

Per-Process Inspection

process_info/2 reveals a single process heap, message queue length and more — key for finding the culprit.

process_info(Pid, [memory, message_queue_len, heap_size]).

The Mailbox Trap

If a process receives faster than it handles, its mailbox grows without bound. A large message_queue_len is the classic sign of an overloaded consumer.

Binaries and Refc Leaks

Large binaries are reference-counted and shared off-heap. A process holding a tiny sub-binary can pin a huge parent binary. Watch the binary memory category closely.

Finding the Worst Offenders

Sort all processes by memory to find leaks fast.

lists:sort(fun({_,A},{_,B}) -> A > B end,
  [{P, element(2, process_info(P, memory))} || P <- processes()]).

How Erlang GC Works

Each process has its own private heap and is garbage-collected independently — there is no global stop-the-world pause. Collection runs when a process heap fills.

Triggering GC Manually

For a process you suspect is holding garbage, force a collection and re-measure.

erlang:garbage_collect(Pid),
process_info(Pid, memory).

Tuning the Heap

Spawn options like min_heap_size and fullsweep_after tune GC for hot processes, trading memory for fewer collections.

spawn_opt(fun work/0, [{min_heap_size, 1000}, {fullsweep_after, 10}]).

Hibernation

Idle processes can call hibernate to compact their heap to the minimum, freeing memory until the next message arrives — great for many mostly-idle connections.

proc_lib:hibernate(?MODULE, loop, [State]).

Watching ETS Tables

ETS tables live outside process heaps and do not shrink automatically. A growing ets memory category often means stale rows; periodically prune or use tables with a bounded size.

ets:info(my_table, memory).

Quick Check

Test your memory tuning knowledge.

Recap

You learned to analyze and tune memory:

  • erlang:memory/0 gives a system-wide breakdown
  • process_info/2 inspects per-process heap and mailbox
  • Growing mailboxes and pinned binaries are common leak sources
  • Each process is GC-ed independently; no global pause
  • Tune with spawn options and use hibernate for idle processes
เริ่มต้นได้ฟรี

เรียนรู้ Erlang ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ”

วิเคราะห์การเพิ่มขึ้นของหน่วยความจำ ตรวจสอบฮีปของแต่ละกระบวนการ และปรับแต่งการเก็บคืนหน่วยความจำของ Erlang เพื่อให้โหนดที่ทำงานยาวนานมีเสถียรภาพ คุณปฏิบัติ Erlang OTP: Distributed & Fault-Tolerant Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming นี้ได้ไหม

ได้ บทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เทคนิคการวิเคราะห์ประสิทธิภาพ Erlang
  2. การติดตามและแก้ไขข้อบกพร่องของระบบแบบกระจาย
  3. การผสานรวมการเก็บตัวชี้วัดและการตรวจสอบ
  4. การวิเคราะห์หน่วยความจำและการปรับแต่งการเก็บคืนหน่วยความจำ
← กลับไปที่ Erlang OTP: Distributed & Fault-Tolerant Systems Programming