تحليل الأداء واكتشاف تسرّب الذاكرة
تعلّموا قياس استخدام CPU والذاكرة في Node.js، والتقاط ملفات تحليل الأداء، والعثور على تسرّبات الذاكرة قبل أن تتسبب في تعطل بيئة الإنتاج
تحليل الأداء واكتشاف تسرّب الذاكرة درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Node.js Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Profile?
You cannot optimize what you cannot measure. Profiling reveals where your app spends time (CPU) and memory, so you fix the real bottleneck instead of guessing.
A common rule: measure, change one thing, measure again.
Measuring Memory Usage
Node exposes live memory stats via process.memoryUsage(). The key field is heapUsed — the JavaScript heap your code allocates.
const m = process.memoryUsage();
console.log('Heap used MB:', (m.heapUsed / 1048576).toFixed(1));What is a Memory Leak?
A memory leak happens when objects that are no longer needed are still referenced, so the garbage collector cannot free them. Over time heapUsed climbs and the process eventually crashes.
Common Leak Sources
Most Node.js leaks come from a handful of patterns:
- Growing global arrays or caches that never evict
- Forgotten event listeners
- Closures capturing large objects
- Timers that are never cleared
const cache = [];
app.get('/x', (req, res) => {
cache.push(req.body); // never cleared = leak
res.end();
});Timing Code Sections
For quick CPU timing, console.time and console.timeEnd measure how long a block takes.
console.time('work');
for (let i = 0; i < 1e6; i++) {}
console.timeEnd('work');High-Resolution Timing
For precise benchmarks use the Performance API, which gives sub-millisecond resolution unaffected by clock changes.
const { performance } = require('perf_hooks');
const start = performance.now();
doWork();
console.log('Took', performance.now() - start, 'ms');CPU Profiling with --prof
Run Node with the --prof flag to record a V8 CPU profile. It writes a log file you process into a readable report.
// node --prof app.js
// node --prof-process isolate-*.log > report.txtHeap Snapshots
A heap snapshot captures every object in memory at a moment. Take two snapshots over time and compare them to find what keeps growing.
Capture them via Chrome DevTools (connect with --inspect) or programmatically.
// node --inspect app.js
// then open chrome://inspect and take heap snapshotsThe Clinic.js Toolkit
The clinic tool suite visualizes performance problems. Clinic Doctor diagnoses issues, while Clinic Heap Profiler tracks allocations.
// npm install -g clinic
// clinic doctor -- node app.jsDetecting a Leak in Practice
Log heapUsed periodically under steady load. If it trends upward and never returns after garbage collection, you likely have a leak — then snapshot to find the culprit.
setInterval(() => {
const mb = process.memoryUsage().heapUsed / 1048576;
console.log('Heap MB:', mb.toFixed(1));
}, 5000);Fixing Leaks
Once found, common fixes are:
- Cap caches with an LRU strategy
- Remove listeners with
removeListener/off - Clear timers with
clearInterval - Avoid capturing large objects in long-lived closures
Quick Check
Test your profiling knowledge.
Recap
You learned to profile and find leaks:
- Measure memory with
process.memoryUsage() - Time code with
console.timeand the Performance API - Capture CPU profiles with
--profand heap snapshots with--inspect - Spot leaks via climbing
heapUsed; fix with bounded caches and cleaned-up listeners/timers
Measuring first turns optimization from guesswork into engineering.
الأسئلة الشائعة
هل درس «تحليل الأداء واكتشاف تسرّب الذاكرة» مجاني؟
نعم — نص درس «تحليل الأداء واكتشاف تسرّب الذاكرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «تحليل الأداء واكتشاف تسرّب الذاكرة»؟
تعلّموا قياس استخدام CPU والذاكرة في Node.js، والتقاط ملفات تحليل الأداء، والعثور على تسرّبات الذاكرة قبل أن تتسبب في تعطل بيئة الإنتاج تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Node.js Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. Node.js Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تحليل الأداء واكتشاف تسرّب الذاكرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Node.js Backend Development Bootcamp هذا؟
نعم. كل درس في Node.js Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- استراتيجيات التخزين المؤقت لـ Node.js
- موازنة التحميل لتطبيقات Node.js
- تحسين حلقة الأحداث في Node.js
- تحليل الأداء واكتشاف تسرّب الذاكرة