Garbage Collection Flashcards

(186 cards)

1
Q

What is garbage collection in Java?

A

Garbage collection is an automatic memory management process that removes unused objects and frees memory at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is garbage collection needed?

A

To prevent memory leaks and reclaim memory occupied by objects no longer in use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Who performs garbage collection?

A

The JVM’s Garbage Collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What kind of memory does garbage collection manage?

A

Heap memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When does an object become eligible for garbage collection?

A

When no reachable references point to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is garbage collection deterministic?

A

No. JVM decides when to run it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you force garbage collection manually?

A

You can request it using System.gc(), but execution is not guaranteed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does garbage collection delete variables or objects?

A

It reclaims memory of unreachable objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is automatic memory management?

A

A system where the runtime handles allocation and deallocation instead of the programmer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens before an object is garbage collected?

A

Its finalize() method may run (deprecated feature).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the main benefit of garbage collection?

A

It reduces risk of memory errors like dangling pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does garbage collection improve program safety?

A

Yes. It prevents manual memory handling mistakes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can an object resurrect itself during GC?

A

Previously possible using finalize(), but this practice is discouraged.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Does garbage collection eliminate all memory issues?

A

No. Logical memory leaks can still occur if references remain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which memory area is not garbage collected?

A

Stack memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Key rule to remember?

A

Garbage collection = automatic cleanup of unused heap objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why does Java provide a garbage collector?

A

To automatically reclaim memory from unused objects and prevent manual memory management errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is automatic memory management important in Java?

A

It removes the need for programmers to manually allocate and free memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What problem would occur without garbage collection?

A

Applications could run out of memory due to unused objects remaining allocated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Who manages memory allocation in Java?

A

The JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

When does the garbage collector run?

A

When the JVM determines memory needs to be reclaimed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What advantage does GC provide over manual memory management?

A

It prevents common bugs like dangling pointers and memory leaks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Does Java use pointers like C/C++?

A

No. Java uses references instead of direct memory pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why does JVM need to reclaim memory automatically?

A

Because programs continuously create objects during execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What triggers garbage collection?
Low memory conditions or JVM internal heuristics.
26
Does GC free memory immediately after object becomes unused?
No. It runs when JVM decides it is necessary.
27
Does garbage collection improve application reliability?
Yes. It reduces memory-related crashes and corruption.
28
Does GC eliminate all memory issues?
No. Logical memory leaks can still occur if references remain.
29
Is garbage collection part of Java’s platform design philosophy?
Yes. It supports safety, portability, and developer productivity.
30
What type of memory does GC manage?
Heap memory.
31
Key rule to remember?
Garbage collector exists to automatically clean unused objects and maintain memory availability.
32
What is the purpose of gc() in Java?
It requests the JVM to run garbage collection to reclaim unused memory.
33
Which methods can request garbage collection?
System.gc() and Runtime.getRuntime().gc().
34
Does calling gc() guarantee garbage collection runs?
No. It is only a request; the JVM may ignore it.
35
Why doesn’t JVM guarantee gc() execution?
Because JVM decides the optimal time for garbage collection.
36
When might developers call gc()?
During testing, benchmarking, or before memory-intensive operations.
37
Is it recommended to call gc() frequently?
No. Overusing it can hurt performance.
38
What actually triggers garbage collection?
JVM heuristics like memory pressure, not gc() calls alone.
39
What is the difference between System.gc() and Runtime.gc()?
They effectively do the same thing; System.gc() internally calls Runtime.gc().
40
Does gc() free specific objects?
No. It only suggests JVM perform general garbage collection.
41
Can gc() improve performance?
Rarely. Improper use often reduces performance.
42
Is gc() synchronous?
Not necessarily. GC may run asynchronously.
43
Can gc() be disabled?
Yes. JVM options can disable explicit GC requests.
44
Is gc() needed in normal programs?
No. JVM automatically manages memory.
45
Why does Java provide gc()?
To allow optional hints for memory cleanup.
46
Key rule to remember?
gc() requests GC but never forces it.
47
How does garbage collection work in Java?
The JVM automatically finds unreachable objects and frees their memory using the garbage collector.
48
What is the basic principle behind Java garbage collection?
Objects that are no longer reachable from any live reference are eligible for collection.
49
What does “unreachable object” mean?
An object that cannot be accessed directly or indirectly from any active thread.
50
What component performs garbage collection?
The JVM’s garbage collector thread.
51
Is garbage collection a background process?
Yes. It runs as a low-priority daemon process.
52
Does GC monitor memory continuously?
It periodically checks heap usage and decides when to run.
53
What algorithm concept is used by GC?
Reachability analysis (object graph traversal).
54
What are GC roots?
References from stack variables, static fields, and active threads.
55
What happens during garbage collection?
The JVM identifies unused objects and reclaims their heap memory.
56
Does GC run only when memory is low?
Usually triggered by memory pressure, but JVM may run it proactively.
57
Can GC move objects in memory?
Yes. Many collectors compact memory to reduce fragmentation.
58
What is heap compaction?
Rearranging live objects to create contiguous free space.
59
Does GC delete objects immediately after they become unused?
No. Collection timing is determined by JVM.
60
Can GC pause program execution?
Yes. Some GC phases cause brief “stop-the-world” pauses.
61
Why are modern GCs efficient?
They use generational collection, collecting short-lived objects more frequently.
62
Key rule to remember?
GC works by detecting unreachable objects and reclaiming their memory automatically.
63
When does an object become eligible for garbage collection in Java?
An object becomes eligible when it is no longer reachable by any live reference.
64
What does “reachable” mean in GC terms?
An object is reachable if it can be accessed directly or indirectly from a GC root.
65
What are GC roots?
Active thread stacks, static fields, JNI references, and system references.
66
When is an object considered unreachable?
When no reference chain exists from any GC root to that object.
67
Can circularly referenced objects be garbage collected?
Yes. If the entire cycle is unreachable from outside, it becomes eligible.
68
Example of unreachable object?
Object obj = new Object(); obj = null; → object eligible.
69
Does assigning null always make object eligible?
Yes, if no other references point to it.
70
Can objects be eligible even if references exist?
Yes, if those references themselves are unreachable.
71
Does scope affect GC eligibility?
Yes. Local variables become eligible after method execution ends.
72
Are objects eligible immediately after becoming unreachable?
They are eligible, but collection timing depends on JVM.
73
Can GC collect objects still in use?
No. Reachable objects are never collected.
74
Do static references prevent GC?
Yes. Static references keep objects reachable.
75
Does JVM check reference count?
No. Java uses reachability analysis, not reference counting.
76
Can finalization affect eligibility?
Finalize() may delay collection, but it is deprecated and unreliable.
77
What is the key condition for GC eligibility?
No live references from GC roots.
78
Key rule to remember?
Unreachable object = eligible for garbage collection.
79
Why was finalize() used in Java?
It was intended to allow objects to perform cleanup actions before being garbage collected.
80
Where is finalize() defined?
In java.lang.Object class.
81
Who calls finalize()?
The JVM calls it automatically before reclaiming an object’s memory.
82
Can developers override finalize()?
Yes. A class can override finalize() to add cleanup logic.
83
Is finalize() guaranteed to run?
No. The JVM may never call it if GC does not occur.
84
Is finalize() guaranteed to run immediately before GC?
No. Its execution timing is unpredictable.
85
Can finalize() run more than once for an object?
No. JVM calls it at most once per object.
86
Why is finalize() considered unreliable?
Because execution is not guaranteed and timing is nondeterministic.
87
Is finalize() recommended in modern Java?
No. It is deprecated and discouraged.
88
What replaced finalize() for cleanup tasks?
try-with-resources, AutoCloseable, and explicit cleanup methods.
89
Why is finalize() deprecated?
It causes performance issues, unpredictability, and security risks.
90
Can finalize() resurrect an object?
Historically yes, but this is unsafe and discouraged.
91
Does finalize() delay garbage collection?
Yes. Objects with finalize() may take longer to be collected.
92
What is the safer alternative to finalize()?
Explicit resource management like close() methods.
93
Key rule to remember?
finalize() was for cleanup before GC but is now deprecated and should be avoided.
94
What are the different types of references in Java?
Strong, Soft, Weak, and Phantom references.
95
What is a strong reference?
A normal reference that prevents an object from being garbage collected.
96
When is an object with only strong references collected?
Only when all strong references to it are removed.
97
What is a soft reference?
A reference that allows GC to reclaim the object only when memory is low.
98
Where are soft references commonly used?
Caching systems where objects can be discarded if memory is needed.
99
What is a weak reference?
A reference that does not prevent GC; the object is collected as soon as no strong references exist.
100
Where are weak references used?
Memory-sensitive structures like WeakHashMap.
101
What is a phantom reference?
A reference used to track object finalization after it is ready for GC.
102
Can phantom references access the object?
No. get() always returns null.
103
What is the purpose of phantom references?
To perform cleanup actions after an object is finalized.
104
Which reference type has strongest reachability?
Strong reference.
105
Which reference type has weakest reachability?
Phantom reference.
106
Which references are in java.lang.ref package?
SoftReference, WeakReference, PhantomReference.
107
Do soft and weak references prevent GC?
No. They allow GC to reclaim objects.
108
Which reference is best for caches?
Soft references.
109
Key rule to remember?
Reference strength order → Strong > Soft > Weak > Phantom.
110
Can an unreferenced object become reachable again in Java?
Yes, but only in rare cases like object resurrection during finalize(), which is discouraged.
111
What is object resurrection?
It is when an object becomes reachable again after being eligible for garbage collection.
112
How can an object resurrect itself?
By assigning its reference (this) to a static or external reference inside finalize().
113
Why is object resurrection possible?
Because finalize() runs before memory is reclaimed, giving the object a last chance to be referenced.
114
Is object resurrection reliable?
No. finalize() execution is not guaranteed and is deprecated.
115
Why is using finalize() for resurrection discouraged?
It leads to unpredictable behavior, memory leaks, and performance issues.
116
Can finalize() be called more than once for resurrection?
No. JVM calls finalize() at most once per object.
117
What happens if object is not resurrected in finalize()?
It becomes eligible for garbage collection again.
118
Is finalize() recommended in modern Java?
No. It is deprecated and should be avoided.
119
What is the modern alternative to finalize()?
Explicit cleanup methods or AutoCloseable with try-with-resources.
120
Does resurrection guarantee object survival?
No. It only delays garbage collection temporarily.
121
Can GC reclaim resurrected object later?
Yes. If it becomes unreachable again, it will be collected.
122
Is object resurrection common practice?
No. It is considered bad design and rarely used.
123
What is the main risk of resurrecting objects?
Memory leaks and unpredictable lifecycle behavior.
124
Key rule to remember?
Object resurrection is possible but unsafe and deprecated practice.
125
What kind of process is the garbage collector thread in Java?
The garbage collector runs as a daemon thread inside the JVM.
126
What is a daemon thread?
A background thread that runs to support other threads and does not prevent JVM shutdown.
127
Why is the GC thread a daemon thread?
Because it performs background memory cleanup and should not block program termination.
128
Does the GC thread run continuously?
It runs periodically based on JVM memory management needs.
129
Who manages the garbage collector thread?
The JVM runtime system.
130
Does GC thread require manual start?
No. It starts automatically when JVM starts.
131
Can a daemon thread keep JVM alive?
No. JVM exits when only daemon threads remain.
132
Is GC considered a user thread or system thread?
It is a system-level background thread.
133
What is the main responsibility of GC thread?
Monitoring heap memory and reclaiming unused objects.
134
Does GC thread run at fixed intervals?
No. It runs based on JVM heuristics and memory pressure.
135
Can developers directly control GC thread?
No. Only indirect hints like System.gc() can be given.
136
Does GC thread affect application performance?
Yes. It may cause pauses during collection cycles.
137
Is there only one GC thread?
Not always. Modern JVMs can use multiple GC threads.
138
Why does GC run in background?
To minimize disruption to application execution.
139
Key rule to remember?
Garbage collector = JVM-managed daemon background process.
140
What is the purpose of the Runtime class in Java?
The Runtime class provides access to the Java runtime environment and allows interaction with JVM resources.
141
Which package contains Runtime class?
java.lang.
142
How do you get a Runtime instance?
Runtime.getRuntime().
143
Why can’t Runtime be instantiated directly?
Its constructor is private to ensure only one runtime instance exists.
144
What does Runtime.freeMemory() return?
The amount of free memory available in the JVM.
145
What does Runtime.maxMemory() return?
The maximum memory the JVM can attempt to use.
146
What does Runtime.totalMemory() return?
The total memory currently allocated to the JVM.
147
What does Runtime.gc() do?
It requests the JVM to perform garbage collection.
148
Does Runtime.gc() guarantee GC execution?
No. It only sends a request.
149
What is Runtime.exec() used for?
To execute external system processes or commands.
150
Example use of Runtime.exec()?
Running a system command like "notepad" or "ls".
151
Can Runtime provide CPU info?
Yes. availableProcessors() returns number of processor cores.
152
Why is Runtime useful for system monitoring?
It exposes memory and environment information.
153
Is Runtime class commonly used in production code?
Mostly for diagnostics, monitoring, or system-level utilities.
154
What design pattern does Runtime follow?
Singleton pattern.
155
Key rule to remember?
Runtime = programmatic access to JVM environment and system resources.
156
How can you invoke an external process in Java?
By using Runtime.getRuntime().exec() or the newer ProcessBuilder class.
157
What method is traditionally used to run external commands?
Runtime.getRuntime().exec().
158
Example of invoking external process?
Runtime.getRuntime().exec("notepad");
159
What is the modern preferred way to start processes?
ProcessBuilder.
160
Why is ProcessBuilder preferred over Runtime.exec()?
It provides better control over environment variables, working directory, and input/output streams.
161
Example of ProcessBuilder usage?
new ProcessBuilder("notepad").start();
162
What does exec() return?
A Process object representing the running process.
163
How can you read output from external process?
Using process.getInputStream().
164
How can you check if process finished?
process.waitFor().
165
Can you pass arguments to external command?
Yes. Provide them as separate strings or array parameters.
166
Does exec() run asynchronously?
Yes. It starts the process immediately and continues execution.
167
How do you run synchronously?
Call waitFor() on the Process object.
168
Can external processes interact with Java program?
Yes. Through input, output, and error streams.
169
What exception may occur when executing process?
IOException if command fails.
170
Is invoking external processes platform dependent?
Yes. Commands differ across operating systems.
171
Key rule to remember?
External process = Runtime.exec() or ProcessBuilder.start().
172
What are the main uses of the Runtime class in Java?
Runtime provides access to JVM environment details, system resources, and process execution features.
173
Can Runtime read system environment variables?
Yes. It can retrieve environment variables using Runtime methods.
174
Can Runtime read system properties?
Yes. It works with JVM system properties and environment settings.
175
Can Runtime execute external programs?
Yes. It can launch non-Java programs using exec().
176
Is Runtime used for memory monitoring?
Yes. It can report free, total, and maximum JVM memory.
177
Which methods provide memory information?
freeMemory(), totalMemory(), and maxMemory().
178
Can Runtime trigger garbage collection?
Yes. Using gc() method.
179
Can Runtime detect processor count?
Yes. availableProcessors() returns number of CPU cores.
180
Does Runtime allow interaction with OS processes?
Yes. It can start and manage external processes.
181
Can Runtime control JVM shutdown?
Yes. It can add shutdown hooks using addShutdownHook().
182
What is a shutdown hook?
A thread that runs when JVM is shutting down.
183
Is Runtime used for keyboard input?
No. Keyboard input is handled by classes like Scanner or BufferedReader.
184
What is a real-world use case of Runtime?
Monitoring system resources or launching external scripts.
185
Is Runtime important for system-level operations?
Yes. It provides low-level interaction with JVM execution environment.
186
Key rule to remember?
Runtime gives programs controlled access to JVM and system resources.