أساسيات ذاكرة Java
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- النموذج المفاهيمي للـ stack والـ heap
- مراجع الكائنات
- مقدمة في جمع المهملات (garbage collection)
تعلّم أولًا
50 دقيقة قراءة · 1.5 ساعة تدريب (تقدير)
مخرجات التعلم
- Draw the stack and heap for a short program, showing frames, local variables, and objects.
- Explain why two variables can refer to the same object and what that implies for mutation.
- Explain at a high level when an object becomes eligible for garbage collection.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| BEG-038-EX01 | 1 | Draw the stack and heap for int x = 5; int y = x; y = 6; StringBuilder sb = new StringBuilder("a"); and state x. |
| BEG-038-EX02 | 2 | Given StringBuilder a = new StringBuilder("hi"); StringBuilder b = a; b.append("!"); predict a, and explain. |
| BEG-038-EX03 | 2 | A method void add(List<String> l) { l.add("z"); } is called with a list. Does the caller's list change? Explain. |
| BEG-038-EX04 | 3 | Mark the moment the object becomes eligible for GC: Person p = new Person(); p = new Person();. |
| BEG-038-EX05 | 3 | Explain in two sentences why System.gc() does not guarantee anything. |
أظهر الحلول
- EX01
xis 5;yis 6 (copied by value).sbin the frame points to a heapStringBuilder. - EX02
ais "hi!":bis 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
Personhas 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).