انتقل إلى المحتوى
أمين أبوزيد
BEG-038

أساسيات ذاكرة Java

  • 5 درس
  • 50 دقيقة قراءة
  • 1.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • النموذج المفاهيمي للـ stack والـ heap
  • مراجع الكائنات
  • مقدمة في جمع المهملات (garbage collection)

تعلّم أولًا

50 دقيقة قراءة · 1.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Draw the stack and heap for a short program, showing frames, local variables, and objects.
  2. Explain why two variables can refer to the same object and what that implies for mutation.
  3. Explain at a high level when an object becomes eligible for garbage collection.

الدروس

  1. 01Stack وheap: صورة توضيحية10 د قراءة
  2. 02المراجع والأسماء المستعارة (aliasing)10 د قراءة
  3. 03جمع المهملات (Garbage collection) بلطف10 د قراءة
  4. 04OutOfMemoryError وStackOverflowError10 د قراءة
  5. 05عرض عملي: رسم stack وheap ثم التحقق في المصحّح (debugger)10 د قراءة

المختبر والتمارين

Lab

BEG-038-LAB1 · Draw it

Outcomes: 1–3 · Time: ~1 h Draw memory diagrams (stack frames, local variables, heap objects) for four short programs: primitive copy, object aliasing, a method that mutates a parameter, and an object that becomes unreachable. Confirm each with the debugger. Acceptance: diagrams match observed values.

Exercises (assessment: diagram quiz)

IDOutcomeTask
BEG-038-EX011Draw the stack and heap for int x = 5; int y = x; y = 6; StringBuilder sb = new StringBuilder("a"); and state x.
BEG-038-EX022Given StringBuilder a = new StringBuilder("hi"); StringBuilder b = a; b.append("!"); predict a, and explain.
BEG-038-EX032A method void add(List<String> l) { l.add("z"); } is called with a list. Does the caller's list change? Explain.
BEG-038-EX043Mark the moment the object becomes eligible for GC: Person p = new Person(); p = new Person();.
BEG-038-EX053Explain in two sentences why System.gc() does not guarantee anything.
أظهر الحلول
  • EX01 x is 5; y is 6 (copied by value). sb in the frame points to a heap StringBuilder.
  • EX02 a is "hi!": b is another reference to the same object.
  • EX03 Yes. The method receives a copy of the reference, pointing to the same list.
  • EX04 After the second assignment, the first Person has no references and is eligible.
  • EX05 It only suggests a collection; the JVM decides when and whether it runs, and garbage is collected automatically when unreachable.
  • The four-program diagram work in the lab and the diagram quiz (اختبار الوحدة) cover outcome 1 as well.

الاختبار

Outcomes: O1 stack and heap · O2 references and aliasing · O3 garbage collection.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Local variables of a running method live in:O1
  2. 2Objects created with new live:O1
  3. 3When a method returns, its stack frame:O1
  4. 4int a = 5; int b = a; b = 9; then a is:O2
  5. 5List<String> a = new ArrayList<>(); List<String> b = a; b.add("x"); then a.size() is:O2
  6. 6b = a for object variables copies:O2
  7. 7== on two object variables compares:O2
  8. 8An object is eligible for GC when:O3
  9. 9System.gc():O3
  10. 10Infinite recursion causes:O1

الجزء العملي

Five exercises BEG-038-EX01 to EX05 plus the lab diagrams. Pass: at least four exercises correct and all four lab diagrams matching the debugger.

أخطاء شائعة

Thinking objects are copied on assignment; believing System.gc() frees memory on demand.

التقييم

Diagram quiz.

الصلة بالمسار الوظيفي

JVM memory management (MID-002) and leak diagnosis (MID-008).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.