GC Interview Questions Answer inwards Java
Garbage collection interview questions are real pop inwards both substance Java too advanced Java Interviews. Apart from Java Collection too Thread many tricky Java questions stems Garbage collections which are tough to answer. In this Java Interview article I volition part about questions from GC which is asked inwards diverse substance Java interviews.These questions are based upon concept of How Garbage collection works, Different kinds of Garbage collector and JVM parameters used for garbage collection monitoring too tuning. As I said GC is an of import purpose of whatever Java interview too then brand certain yous receive got proficient command inwards GC. One to a greater extent than affair which is getting real of import is might to encompass too understand Garbage collection Output, to a greater extent than too to a greater extent than interviewer are checking whether candidate tin sympathise GC output or not. During Java interview they may render a snippet of GC output too inquire diverse questions based on that e.g. Which Garbage collector is used, whether output is from major collection or tiddler collection, How much retentivity is costless from GC, What is size of novel generation too former generation afterwards GC etc. I receive got included few Garbage collection interview questions too answers from GC output to assist amongst that. It’s recommended to ready questions from Java collection, multithreading too programming along amongst Garbage collection to create good inwards Java interviews at whatever stage.
Interview questions on Java Garbage collection
Here is about Garbage collection Interview questions from my personal collection, which I receive got created from my sense too amongst the assist of diverse friends too colleagues which has shared GC interview questions amongst me. Actually at that topographic point are lot many questions than What I am sharing hither but to acquire out along this transportation little I idea to entirely part about questions, I tin yell upward of minute purpose of GC interview inquiry if yous guys notice this useful.
Question 1 - What is construction of Java Heap ? What is Perm Gen infinite inwards Heap ?
Answer : In club to amend perform inwards Garbage collection questions inwards whatever Java interview, It’s of import to receive got basic agreement of Java Heap space. To larn to a greater extent than almost heap, meet my post 10 points on Java heap space. By the way Heap is divided into unlike generation e.g. novel generation, former generation too PermGen space.PermGen infinite is used to shop class’s metadata too filling of PermGen infinite tin cause java.lang.OutOfMemory:PermGen space. Its also worth noting to remember JVM selection to configure PermGen space inwards Java.
Question 2 - How create yous position tiddler too major garbage collection inwards Java?
Answer: Minor collection prints “GC” if garbage collection logging is enable using –verbose:gc or -XX:PrintGCDetails, acre Major collection prints “Full GC”. This Garbage collection interview inquiry is based on agreement of Garbage collection output. As to a greater extent than too to a greater extent than Interviewer are asking inquiry to depository fiscal establishment friction match candidate’s might to sympathise GC output, this topic acquire out fifty-fifty to a greater extent than important.
Question iii - What is divergence betwixt ParNew too DefNew Young Generation Garbage collector?
Answer : This Garbage Collection interview questions is of late asked to ane of my friend. It require to a greater extent than than average cognition on GC to reply this question. By the way ParNew too DefNew is 2 immature generation garbage collector. ParNew is a multi-threaded GC used along amongst concurrent Mark Sweep acre DefNew is unmarried threaded GC used along amongst Serial Garbage Collector.
Question iv - How create yous notice GC resulted due to calling System.gc()?
Answer : Another GC interview inquiry which is based on GC output. Similar to major too tiddler collection, at that topographic point volition live on a give-and-take “System” included inwards Garbage collection output.
Question v - What is divergence betwixt Serial too Throughput Garbage collector?
Answer : Serial Garbage collector is a halt the earth GC which stops application thread from running during both minor too major collection. Serial Garbage collector tin live on enabled using JVM selection -XX:UseSerialGC too it's designed for Java application which doesn't receive got intermission fourth dimension requirement too receive got customer configuration. Serial Garbage collector was also default GC inwards JDK 1.4 earlier ergonomics was introduced inwards JDK 1.5. Serial GC is most suited for little application amongst less number of thread while throughput GG is to a greater extent than suited for large applications. On the other mitt Throughput garbage collector is parallel collector where tiddler too major collection happens inwards parallel taking sum wages of all the organisation resources available similar multiple processor. Though both major too tiddler collection runs on stop-the-world fashion too introduced intermission inwards application. Throughput Garbage collector tin live on enable using -XX:UseParallelGC or -XX:UseOldParallelGC. It increases overall throughput of application my minimizing fourth dimension spent inwards Garbage collection but notwithstanding has long pauses during sum GC.This is a variety of Garbage collection interview questions which gives yous an chance to exhibit your cognition inwards special acre answering. I e'er advise to reply these variety of questions inwards detail. See Java Performance The Definitive Guide for to a greater extent than details on this topic.
Question half-dozen – When does an Object becomes eligible for Garbage collection inwards Java ?
Answer : An object becomes eligible for garbage collection when at that topographic point is no alive reference for that object or it tin non live on reached yesteryear whatever alive thread. Cyclic reference doesn’t count every bit alive reference too if 2 objects are pointing to each other too at that topographic point is no alive reference for whatever of them, than both are eligible for GC. Also Garbage collection thread is a daemon thread which volition run yesteryear JVM based upon GC algorithm too when runs it collects all objects which are eligible for GC.
Question seven - What is finalize method inwards Java ? When does Garbage collector calls finalize method inwards Java ?
Answer : Finalize method inwards Java also called finalizer is a method defined inwards java.lang.Object too called yesteryear Garbage collector earlier collecting whatever object which is eligible for GC. Finalize() method provides finally jeopardy to object to create cleanup too costless whatever remaining resource, to larn to a greater extent than almost finalizers, read What is finalize method inwards Java.
Question 8 - If Object Influenza A virus subtype H5N1 has reference to Object B too Object B refer to Object A, apart from that at that topographic point is no alive reference to either object Influenza A virus subtype H5N1 or B, Does they are eligible to Garbage collection ?
This Garbage collection interview questions is related inquiry v “When object acquire out eligible for Garbage collection”. An object becomes eligible for Garbage collection if at that topographic point is no alive reference for it. It tin non live on accessible from whatever Thread too cyclic dependency doesn’t forestall Object from beingness Garbage collected. Which agency inwards this example both Object Influenza A virus subtype H5N1 too Object B are eligible of Garbage collection. See How Garbage collection industrial plant inwards Java for more details.
Question ix -Can nosotros strength Garbage collector to run at whatever fourth dimension ?
Answer : No, yous tin non strength Garbage collection inwards Java. Though yous tin asking it yesteryear calling Sytem.gc() or its cousin Runtime.getRuntime().gc(). It’s non guaranteed that GC volition run right away every bit outcome of calling these method.
Question 10 - Does Garbage collection occur inwards permanent generation infinite inwards JVM?
Answer : This is a tricky Garbage collection interview inquiry every bit many programmers are non certain whether PermGen infinite is purpose of Java heap space or non too since it maintains cast Meta information too String pool, whether its eligible for garbage collection or not. By the way Garbage Collection does occur inwards PermGen infinite too if PermGen infinite is sum or cross a threshold, it tin trigger Full GC. If yous hold back at output of GC yous volition notice that PermGen infinite is also garbage collected. This is why right sizing of PermGen infinite is of import to avoid frequent sum GC. You tin command size of PermGen infinite yesteryear JVM options -XX:PermGenSize too -XX:MaxPermGenSize.
Question eleven : How to yous monitor garbage collection activities?
Answer : One of my favorite interview questions on Garbage collection, merely to check whether candidate has ever monitored GC activities or not. You tin monitor garbage collection activities either offline or real-time. You tin usage tools similar JConsole too VisualVM VM amongst its Visual GC plug-in to monitor existent fourth dimension garbage collection activities too retentivity condition of JVM or yous tin redirect Garbage collection output to a log file for offline analysis yesteryear using -XlogGC=<PATH> JVM parameter. Anyway yous should e'er enable GC options similar -XX:PrintGCDetails -X:verboseGC too -XX:PrintGCTimeStamps every bit it doesn't impact application performance much but render useful states for surgical physical care for monitoring.
Question 12: Look at below Garbage collection output too reply next inquiry :
[GC
[ParNew: 1512K->64K(1512K), 0.0635032 secs]
15604K->13569K(600345K), 0.0636056 secs]
[Times: user=0.03 sys=0.00, real=0.06 secs]
1. Is this output of Major Collection or Minor Collection ?
2. Which immature Generation Garbage collector is used ?
3. What is size of Young Generation, Old Generation too sum Heap Size?
4. How much retentivity is freed from Garbage collection ?
5. How much fourth dimension is taken for Garbage collection ?
6. What is electrical flow Occupancy of Young Generation ?
This Garbage collection Interview questions is completely based on GC output. Following are answers of to a higher house GC questions which volition non entirely assist yous to reply these inquiry but also assist yous to sympathise too translate GC output.
Answer 1: It's Minor collection because of "GC" word, In example of Major collection, yous would meet "Full GC".
Answer 2: This output is of multi-threaded Young Generation Garbage collector "ParNew", which is used along amongst CMS concurrent Garbage collector.
Answer 3: [1512K] which is written inwards bracket is sum size of Young Generation, which include Eden too 2 survivor space. 1512K on left of arrow is occupancy of Yong Generation earlier GC too 64K is occupancy afterwards GC. On the adjacent describe of piece of job value if bracket is sum heap size which is (600345K). If nosotros subtract size of immature generation to sum heap size nosotros tin calculate size of Old Generation. This describe of piece of job also shows occupancy of heap earlier too afterwards Garbage collection.
Answer 4: As answered inwards previous garbage collection interview question, minute describe of piece of job shows heap occupancy earlier too afterwards Garbage collection. If nosotros subtract value of right side 13569K, to value on left side 15604K, nosotros tin acquire sum retentivity freed yesteryear GC.
Answer 5: 0.0636056 secs on minute describe of piece of job denotes sum fourth dimension it took to collect dead objects during Garbage collection. It also include fourth dimension taken to GC immature generation which is shown inwards get-go describe of piece of job (0635032 secs).
Answer 6: 64K
Here are few to a greater extent than interesting Garbage collection Interview question for your practice, I haven’t provided answers of all garbage collection interview questions. If yous know the reply than yous tin post via comments.
Question - What is divergence betwixt -XX:ParallelGC too -XX:ParallelOldGC?
Question - When create yous ConcurrentMarkSweep Garbage collector too Throughput GC?
Question - What is divergence betwixt ConcurrentMarkSweep and G1 garbage collector?
Question - Have yous done whatever garbage collection tuning? What was your approach?
Further Learning
Java Memory Management
20 pattern pattern too software pattern interview inquiry – Answered