Tutte le specialitàTecnologia · Colloquio di lavoro

JVM

Le domande sulla JVM mostrano se sai che cosa gira sotto il tuo codice Java. Spiegare il garbage collection e la memoria in un inglese semplice è un'abilità che si può allenare.

Parole → espressioni → frasi → parlato

Prova SpeakRole ↗
Colloquio di lavoroJVM
V

What happens when the JVM runs out of heap memory?

0:18
Domanda dell’intervistatore

JVM

Chi intervista per i ruoli Java passa spesso dal linguaggio al runtime: come è organizzata la memoria, come vengono ripuliti gli oggetti e che cosa fai quando un servizio in produzione esaurisce la memoria. Anche a un candidato di livello intermedio si chiede di conoscere le idee principali e gli strumenti giusti.

Rispondi in quest'ordine: che cosa fa quella parte, come funziona in termini semplici e come la vedresti in un sistema reale, per esempio in un log del GC o in un heap dump. Fai attenzione ai dettagli legati alla versione e di' "in modern Java" oppure "it depends on the version" quando non sei sicuro.

  • ✓Parole → espressioni → frasi → parlato
  • ✓Senso della risposta
  • ✓Lingua

Domande comuni e risposte di esempio

How does garbage collection work in the JVM?

Come funziona il garbage collection nella JVM?

Che cosa vuole sentire l'intervistatore. La raggiungibilità dalle radici, l'idea delle generazioni, la differenza tra pause brevi e lunghe e almeno un collector citato per nome.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Quali sono le principali aree di memoria della JVM?

Che cosa vuole sentire l'intervistatore. Heap, stack dei thread, metaspace e memoria nativa, e che cosa viene conservato dove. Punto in più: quale errore indica quale area.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Come funziona il caricamento delle classi (class loading) nella JVM?

Che cosa vuole sentire l'intervistatore. Caricamento, collegamento (linking) e inizializzazione, il modello di delega al parent e la differenza tra ClassNotFoundException e NoClassDefFoundError.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Come indaghi su un memory leak o su un OutOfMemoryError?

Che cosa vuole sentire l'intervistatore. Una sequenza pratica: confermare con log del GC e metriche, fare un heap dump, analizzarlo, trovare la catena di riferimenti e correggere la causa.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Come fai il tuning di una JVM per un servizio in produzione?

Che cosa vuole sentire l'intervistatore. Prima misurare; dimensione dell'heap, scelta del collector, limiti del container e warm-up; una modifica alla volta con un obiettivo chiaro.

Risposta di esempio in inglese da esercitare ad alta voce

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.

Come pronunciare i termini chiave

JVM
gèi-vi-èm
heap
hiip (la «ii» è lunga, non «hip»)
metaspace
mèt-a-spèis
bytecode
bàit-coud
JIT
gìt (rima con «bit») oppure gèi-ai-ti
allocation
al-o-chèi-shon
generation
gen-e-rèi-shon

Frasi utili per rispondere

  • Under the hood, the JVM…Sotto il cofano, la JVM…
  • The GC logs show that…I log del GC mostrano che…
  • I would take a heap dump and look at…Farei un heap dump e guarderei…
  • The trade-off is between pause time and throughput.Il compromesso è tra tempo di pausa e throughput.
  • It depends on the JVM version and the collector.Dipende dalla versione della JVM e dal collector.
  • The object stays reachable because…L'oggetto resta raggiungibile perché…
  • I would measure under realistic load first.Prima misurerei sotto un carico realistico.

Errori comuni da evitare

  • Confondere il modello di memoria di Java con le aree di memoria della JVM. Il primo riguarda la visibilità tra thread; le seconde sono heap, stack e metaspace. Di' quale intendi.
  • Dire «il garbage collector elimina gli oggetti inutilizzati» e fermarsi lì. Spiega la raggiungibilità dalle radici e l'idea delle generazioni. È il punto debole più comune.
  • Proporre un heap più grande come risposta a un memory leak. Un heap più grande rimanda soltanto il guasto. Di' che trovi il riferimento che tiene in vita gli oggetti.
  • Citare a memoria i flag senza dire la versione. Molte opzioni sono cambiate tra le versioni di Java, quindi di' "in recent versions" e controlla la documentazione.
  • Pronunciare "heap" come "hip". Ha una vocale lunga, come in "cheap". In italiano la differenza tra vocale lunga e breve non esiste, quindi allena a voce alta le parole chiave: una vocale sbagliata può cambiare la parola che intendi.

Domande frequenti

Devo conoscere nei dettagli gli algoritmi del GC?

Servono le idee principali: raggiungibilità, generazioni, pause e uno o due collector con i loro obiettivi. Dettagli come la dimensione delle regioni o i flag di tuning si chiedono di solito solo per ruoli senior o orientati alle prestazioni.

Quali strumenti della JVM devo citare?

Nomina quelli che hai usato: log del GC, jcmd, jstack per i thread dump, Java Flight Recorder e un analizzatore di heap come Eclipse Memory Analyzer. Se hai usato solo una dashboard di monitoraggio, dillo e spiega che cosa guardavi.

Devo citare la versione di Java?

Sì. Di' quale versione usava il tuo ultimo progetto e ricorda che alcuni comportamenti cambiano da una versione all'altra. Dimostra che sai che il runtime evolve e ti evita di affermare qualcosa che vale solo per una versione.

Leggere una risposta non è la stessa cosa che dirla. Esercita ogni domanda ad alta voce, poi confronta il significato e la lingua della tua risposta.

Prova SpeakRole ↗

Altri argomenti in quest’area