0Pricing
Clojure Functional Programming & JVM Backend Development · บทเรียน

การจัดการหน่วยความจำและการลดแรงกดดันต่อ GC

ทำความเข้าใจการเก็บคืนหน่วยความจำของ JVM และเรียนรู้เทคนิค Clojure ที่นำไปใช้ได้จริงเพื่อลดการจัดสรรและช่วงหยุด GC ในเส้นทางโค้ดที่ทำงานบ่อย

การจัดการหน่วยความจำและการลดแรงกดดันต่อ GC เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clojure Functional Programming & JVM Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

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

Why GC Matters

The JVM automatically reclaims unused memory through garbage collection. But GC pauses can hurt latency. Reducing allocations means less GC work and smoother performance.

Generational Heap

The JVM heap is split into generations. Most objects die young, so the young generation is collected often and cheaply; survivors move to the old generation.

Allocation in Clojure

Clojure's immutable data structures share structure efficiently, but transformations still allocate. In tight loops these allocations add up.

; each map call allocates a new seq node
(reduce + (map inc (range 1000000)))

Transients

Transients allow temporary, mutable-but-safe building of collections, avoiding intermediate allocations. Convert back with persistent!.

(defn build [n]
  (persistent!
    (reduce conj! (transient []) (range n))))

Avoiding Boxing

Primitive numbers get boxed into objects when stored in generic collections. Type hints and primitive math avoid this overhead.

(defn sum-sq ^long [^long n]
  (loop [i 0 acc 0]
    (if (< i n)
      (recur (inc i) (+ acc (* i i)))
      acc)))

Reusing Buffers

For I/O and number crunching, reusing arrays instead of allocating per call reduces GC pressure significantly.

(let [buf (byte-array 8192)]
  (loop []
    (let [n (.read in buf)]
      (when (pos? n)
        (.write out buf 0 n)
        (recur)))))

Lazy Seqs and Memory

Lazy sequences allocate seq nodes and can hold the head in memory. For pure transformations, prefer reduce or transducers, which avoid intermediate collections.

(transduce (comp (map inc) (filter even?)) + 0 (range 1000000))

Choosing a Collector

Modern JVMs offer collectors tuned for different goals:

  • G1: balanced default
  • ZGC / Shenandoah: very low pause times
  • Parallel: max throughput, longer pauses
; enable ZGC
; java -XX:+UseZGC -jar app.jar

Measuring GC

Enable GC logging to see pause frequency and duration before tuning. Never guess; measure.

; java -Xlog:gc -jar app.jar

Right-Sizing the Heap

Set min and max heap equal to avoid resize pauses, and size based on real working set. Too small causes frequent GC; too large wastes memory and lengthens old-gen collections.

; java -Xms2g -Xmx2g -jar app.jar

A Tuning Workflow

Practical loop: profile to find hot allocations, apply transients/primitives/transducers, measure GC logs, then tune the collector and heap only if needed.

Quick Check

Test your memory tuning knowledge.

Recap

You learned to reduce GC pressure in Clojure.

  • Use transients, primitives, and transducers to cut allocations
  • Reuse buffers in hot paths
  • Measure with GC logs, then tune collector and heap size

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

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

ใช่ — ข้อความเต็มของ “การจัดการหน่วยความจำและการลดแรงกดดันต่อ GC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

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

ทำความเข้าใจการเก็บคืนหน่วยความจำของ JVM และเรียนรู้เทคนิค Clojure ที่นำไปใช้ได้จริงเพื่อลดการจัดสรรและช่วงหยุด GC ในเส้นทางโค้ดที่ทำงานบ่อย คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการหน่วยความจำและการลดแรงกดดันต่อ GC” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม

ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การวิเคราะห์ประสิทธิภาพแอปพลิเคชัน Clojure
  2. แนวทางปฏิบัติที่ดีที่สุดด้านประสิทธิภาพ JVM
  3. การวัดประสิทธิภาพและการปรับจุดที่เป็นคอขวด
  4. การจัดการหน่วยความจำและการลดแรงกดดันต่อ GC
← กลับไปที่ Clojure Functional Programming & JVM Backend Development