VWhat happens when the JVM runs out of heap memory?
سؤال المقابلJVM
ينتقل المحاورون في وظائف Java كثيرًا من اللغة إلى بيئة التشغيل: كيف تُنظَّم الذاكرة، وكيف تُنظَّف الكائنات، وماذا تفعل عندما تنفد ذاكرة خدمة في بيئة الإنتاج. حتى المرشح متوسط الخبرة يُتوقع منه أن يعرف الأفكار الرئيسة والأدوات المناسبة.
رتّب إجاباتك هكذا: ما الذي يفعله هذا الجزء، وكيف يعمل بعبارات بسيطة، وكيف يمكنك رؤيته في نظام حقيقي، مثلًا في سجل GC أو في heap dump. احذر من التفاصيل المرتبطة بإصدار معيّن، وقل "in modern Java" أو "it depends on the version" عندما لا تكون متأكدًا.
- ✓كلمات → عبارات → جمل → تحدث
- ✓معنى الإجابة
- ✓اللغة
أكثر الأسئلة تكرارًا ونماذج الإجابات
How does garbage collection work in the JVM?
كيف يعمل جمع النفايات (garbage collection) في JVM؟
ما الذي يريد المحاور سماعه. إمكانية الوصول من الجذور (reachability from roots)، وفكرة الأجيال (generational)، والفرق بين التوقفات القصيرة والطويلة، وذكر مجمّع واحد على الأقل باسمه.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍThe JVM frees the memory of objects that can no longer be reached from the roots, such as thread stacks and static fields. Most objects die young, so the heap is usually divided into a young generation and an old generation. New objects go to the young area, and a small, fast collection cleans it. Objects that survive move to the old area, which is collected less often. Some phases stop the application, and we call that a stop-the-world pause. In modern Java, G1 is usually the default collector, and it tries to keep pauses short. ZGC and Shenandoah aim for even shorter pauses. I choose by the pause and throughput requirements, and I check the GC logs.
What are the main memory areas of the JVM?
ما مناطق الذاكرة الرئيسة في JVM؟
ما الذي يريد المحاور سماعه. الكومة (heap) ومكدسات الخيوط (thread stacks) وmetaspace والذاكرة الأصلية (native memory)، وما الذي يُخزَّن أين. ويُحسب لك أن تعرف أي خطأ يشير إلى أي منطقة.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍThe biggest area is the heap, where objects are created and where the garbage collector works. Each thread has its own stack, which holds method frames with local variables and references. When the stack gets too deep, for example with endless recursion, we get a StackOverflowError. Class metadata is stored in metaspace, which uses native memory and replaced the old permanent generation in Java 8. There is also memory outside the heap: thread stacks, direct buffers and the code cache with compiled code. This is important in containers, because the process uses more memory than the heap size alone.
How does class loading work in the JVM?
كيف يعمل تحميل الفئات (class loading) في JVM؟
ما الذي يريد المحاور سماعه. التحميل والربط والتهيئة (loading, linking, initialisation)، ونموذج تفويض الأصل (parent delegation)، والفرق بين ClassNotFoundException وNoClassDefFoundError.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍThe JVM loads classes lazily, when they are first needed. A class loader finds the bytecode and creates the class. Then the class is linked, which means it is verified and prepared, and finally it is initialised: static fields are set and static blocks run. Class loaders form a hierarchy with parent delegation. A loader first asks its parent to load the class, and only then tries by itself. This protects core classes like String from being replaced. ClassNotFoundException happens when code asks for a class by name and it is not found. NoClassDefFoundError usually means that the class existed at compile time but is missing at runtime, or that its static initialisation failed.
How do you investigate a memory leak or an OutOfMemoryError?
كيف تتحرّى تسرّب الذاكرة (memory leak) أو OutOfMemoryError؟
ما الذي يريد المحاور سماعه. تسلسل عملي: التأكد بسجلات GC والمقاييس، وأخذ heap dump، وتحليله، وإيجاد سلسلة المراجع، وإصلاح السبب.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍFirst I check whether it is really a leak. In the GC logs or the metrics, I look at whether the memory after a full collection keeps growing. I make sure the JVM writes a heap dump on OutOfMemoryError, or I take one with jcmd. Then I open it in a tool like Eclipse Memory Analyzer and look for the biggest objects and the path from a GC root that keeps them alive. Typical causes are static collections that only grow, caches without a size limit, listeners that are never removed and ThreadLocal values in long-living threads. After the fix, I watch the memory again under similar load.
How do you tune a JVM for a production service?
كيف تضبط JVM لخدمة في بيئة الإنتاج؟
ما الذي يريد المحاور سماعه. القياس أولًا؛ حجم الكومة، واختيار المجمّع، وحدود الحاوية، والإحماء (warm-up)؛ وتغيير واحد في كل مرة بهدف واضح.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍI start with a goal: low latency, high throughput or small memory use. Then I measure, and I never tune by guessing. I enable GC logging, set the maximum heap with Xmx, and often set the initial heap to the same size to avoid resizing. In a container, I leave room for non-heap memory, so I set the heap as a percentage of the container limit, for example with MaxRAMPercentage. I change one parameter at a time and test under realistic load. I also remember that the JIT compiler needs warm-up, so the first minutes after a start are slower, and a load test must run long enough to show the real picture.
كيف تنطق المصطلحات الأساسية
- JVM
- جَيْ ڤِي إِم
- heap
- هِيب (بحرف مدّ طويل، وليس «هِبْ»)
- metaspace
- مِتَاسْبَيْس
- bytecode
- بَايْتْكُود
- JIT
- جِتْ (على وزن «بِتْ»)، أو جَيْ آيْ تِي
- allocation
- أَلَكَيْشِن
- generation
- جِنَرَيْشِن
عبارات مفيدة للإجابة
- Under the hood, the JVM…خلف الكواليس، تقوم JVM بـ…
- The GC logs show that…تُظهر سجلات GC أنّ…
- I would take a heap dump and look at…كنت سآخذ heap dump وأنظر إلى…
- The trade-off is between pause time and throughput.المفاضلة هنا بين زمن التوقف والإنتاجية.
- It depends on the JVM version and the collector.هذا يعتمد على إصدار JVM والمجمّع (collector).
- The object stays reachable because…يبقى الكائن قابلًا للوصول لأن…
- I would measure under realistic load first.كنت سأقيس أولًا تحت حمل واقعي.
أخطاء شائعة
- الخلط بين نموذج ذاكرة Java (Java memory model) ومناطق ذاكرة JVM. الأول يتعلق بالرؤية (visibility) بين الخيوط، والثاني يتعلق بالكومة والمكدس وmetaspace. قل أيهما تقصد.
- قول "the garbage collector deletes unused objects" والتوقف عند ذلك. اشرح إمكانية الوصول من الجذور وفكرة الأجيال. هذه أكثر نقطة ضعف شيوعًا.
- اقتراح كومة أكبر حلًّا لتسرّب الذاكرة. الكومة الأكبر لا تفعل إلا تأخير الفشل. قل إنك تبحث عن المرجع الذي يُبقي الكائنات حية.
- ذكر الخيارات (flags) من الذاكرة دون ذكر الإصدار. تغيّرت خيارات كثيرة بين إصدارات Java، فقل "in recent versions" وراجع الوثائق.
- نطق "heap" مثل "hip". فيها حرف مدّ طويل كما في "cheap". تدرّب على الكلمات الأساسية بصوت عالٍ، فقد يغيّر حرف مدّ خاطئ الكلمة التي تقصدها.
الأسئلة الشائعة
هل أحتاج إلى معرفة خوارزميات GC بالتفصيل؟
تحتاج إلى الأفكار الرئيسة: إمكانية الوصول، والأجيال، والتوقفات، ومجمّعًا أو اثنين مع أهداف كل منهما. أما تفاصيل مثل أحجام المناطق أو خيارات الضبط فلا تُتوقع إلا في الوظائف العليا أو الموجهة نحو الأداء.
ما أدوات JVM التي أذكرها؟
اذكر ما استخدمته: سجلات GC، وjcmd، وjstack لتفريغات الخيوط (thread dumps)، وJava Flight Recorder، ومحلل كومة مثل Eclipse Memory Analyzer. وإذا لم تستخدم إلا لوحة مراقبة، فقل ذلك واشرح ما الذي كنت تنظر إليه.
هل أذكر إصدار Java؟
نعم. قل أي إصدار استخدمه مشروعك الأخير، وأشِر إلى أن بعض السلوك يختلف بين الإصدارات. هذا يُظهر أنك تعرف أن بيئة التشغيل تتغير، ويحميك من ادعاء شيء صحيح في إصدار واحد فقط.
قراءة الإجابة ليست مثل قولها. تدرّب على كل سؤال بصوت عالٍ، ثم قارن معنى إجابتك ولغتها بالنموذج.
جرّب SpeakRole ↗موضوعات أخرى في هذا المجال