Garbage Collection Flashcards

1
Q

What is Garbage Collection in

Java?

A

Java has an internal mechanism called Garbage collection to reclaim the memory of unused projects at run time.

Garbage collection is also known as automatic memory management.

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

Why Java provides Garbage Collector?

A

In Java, there are no pointers. Memory management and allocation is done by JVM. Since memory allocation is automated, after some time JVM may go low on memory. At that time, JVM has to free memory from unused objects.

To help with the process of reclaiming memory, Java provides an automated process called
Garbage Collector.

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

How does Garbage Collection work in Java?

A

Java has an automated process called Garbage Collector for Memory Management. It is a daemon in JVM that monitors the
memory usage and performs memory cleanup.

Once JVM is low on memory, GC process finds the unused objects that are not referenced by other objects. These unused objects are cleaned up by Garbage Collector daemon in JVM.

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

When does an object become eligible for Garbage Collection in Java?

A

An object can be Garbage Collected by JVM, if it is not reachable. There are two cases for deciding eligibility of objects for Garbage
Collection:

  1. An Object/instance that cannot be reached by a live thread.
  2. A set of circularly referenced instances that cannot be
    reached by any other instance outside that set.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why do we use finalize() method

in Java?

A

Java provides finalize() method to perform any cleanup before
Garbage Collection. This method is in Object class, and it is
invoked by JVM internally. Developers are free to implement this
method for any custom cleanup in case of Garbage Collection.
If an Object is not Garbage Collected, then this method may not be
called.
This method is never invoked more than once by JVM.

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

What are the different types of References in Java?

A

In Java, there are four types of references:

  1. Strong Reference
  2. Soft Reference
  3. Weak Reference
  4. Phantom Reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we reference an

unreferenced object again?

A

We can provide implementation in finalize() method to reference and unreferenced object. For an unreferenced object, finalize()
method is called at the time of Garbage Collection. At this time, Object can pass its reference ‘this’ to finalize() method and revive
itself.

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

What kind of process is the

Garbage collector thread?

A

Garbage Collection is a Daemon process in JVM. It is an internal
process that keep checking Memory usage and cleans up the
memory.

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

What is the purpose of the Runtime class?

A

The purpose of the Runtime class is to provide access to the Java
Runtime system. This class provides certain important methods like:
1. Runtime.freeMemory() – This method returns the value of
free memory in JVM
2. Runtime.maxMemory() - This method returns the value of
maximum memory that JVM can use.
3. Runtime.gc() – This method can invoke garbage collection.

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

How can we invoke an external process in Java?

A

Java provides the method Runtime.getRuntime().exec() to invoke an
external process from JVM.

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

What are the uses of Runtime class?

A

Runtime class in Java provides following benefits:
1. It allows to read data via key board
2. It can use system properties and environment variables
3. It helps in running non-java programs from within a java
application.

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