Invoking Garbage Collection Programmatically – Object Lifetime

10.4 Invoking Garbage Collection Programmatically

Although Java provides facilities to invoke the garbage collector explicitly, there are no guarantees that it will be run. The program can request that garbage collection be performed, but there is no way to force garbage collection to be activated.

The System.gc() method can be used to request garbage collection.

static void gc()

Requests that garbage collection be run.

Alternatively, corresponding methods in the Runtime class can be used. A Java application has a unique Runtime object that can be used by the application to interact with the JVM. An application can obtain this object by calling the method Runtime.getRuntime(). The Runtime class provides several methods related to memory issues:

static Runtime getRuntime()

Returns the Runtime object associated with the current application.

void gc()

Requests that garbage collection be run. There are no guarantees that it will be run. It is recommended to use the more convenient static method System.gc().

long freeMemory()

Returns the amount of free memory (bytes) in the JVM that is available for new objects.

long totalMemory()

Returns the total amount of memory (bytes) available in the JVM, including both memory occupied by current objects and memory available for new objects.

The following points regarding automatic garbage collection should be noted:

  • Trying to initiate garbage collection programmatically does not guarantee that it will actually be run.
  • Garbage collection might not even be run if the program execution does not warrant it. Thus any memory allocated during program execution might remain allocated after program termination, but will eventually be reclaimed by the operating system.
  • There are also no guarantees about the order in which the objects will be garbage collected. Therefore, the program should not make any assumptions based on this criteria.
  • Garbage collection does not guarantee that there will be enough memory for the program to run. A program can rely on the garbage collector to run when memory gets very low, and it can expect an OutOfMemoryError to be thrown if its memory demands cannot be met.

Leave a Reply

Your email address will not be published. Required fields are marked *

Instance Initializer Blocks

  • 10.4 Invoking Garbage Collection Programmatically Although Java provides facilities to invoke the garbage collector explicitly, there are no guarantees that it will be run. The program can request that garbage collection be performed, but there is no way to force garbage collection to be activated. The System.gc() method can be used to request garbage collection.…

  • 11.7 Generic Methods and Constructors We first look at how generic methods and constructors are declared, and then at how they can be called—both with and without explicit actual type parameters. Declaring Generic Methods A generic method (also called polymorphic method) is implemented like an ordinary method, except that one or more formal type parameters…

  • Exception Handling and Initializer Expressions Initializer expressions in named classes and interfaces must not result in any uncaught checked exception (§7.2, p.374). If any checked exception is thrown during execution of an initializer expression, it must be caught and handled by code called from the initializer expression. This restriction does not apply to instance initializer…

  • 11.4 Wildcards In this section, we discuss how using wildcards can increase the expressive power of generic types. But first we examine one major difference between array types and parameterized types. The generic class Node<E> used in this subsection is defined in Example 11.2, p. 568. The Subtype Covariance Problem with Parameterized Types The following…

  • Extending Generic Types A non-final generic type can be extended. Example 11.5 shows that the generic interface IBiLink<E> extends the generic interface IMonoLink<E>, and that the generic class BiNode<E> extends the generic class MonoNode<E> and implements the generic interface IBiLink<E> (see Figure 11.1). Click here to view code image interface IBiLink<E> extends IMonoLink<E> {  //…

  • 11.1 Introducing Generics Generics allow classes and interfaces, as well as methods and constructors, to be parameterized with type information. An abstract data type (ADT) defines both the types of objects and the operations that can be performed on these objects. Generics allow us to specify the types used by the ADT so that the…

  • The Diamond Operator (<>) In the object creation expression of the new operator, the actual type parameter was explicitly specified after the class name—in contrast to the constructor declaration. Click here to view code image Node<String> lst = new Node<String>(“Hi”, null); // Explicit actual type parameter The actual type parameters can be omitted, but not…

  • Wildcard Types Wildcard types are type parameters defined using the wildcard symbol ?. The wildcard ? by itself represents all types. The parameterized type List<?> represents a list of all types, whereas the concrete parameterized type List<Integer> only represents a list of Integer. In other words, a wildcard type can represent many types. Therefore, a…

  • Declaration Order of Static Initializers In the class ScheduleV1 below, the static field declaration at (1) has a forward reference to the static field numOfWeeks which has not been declared yet. The simple name of the static field numOfWeeks cannot be used in the initializer expression at (1) before its declaration. Click here to view…

  • Using Parameterized References to Call Set and Get Methods Generic classes are suitable for implementing ADTs called collections (also called containers) where the element type is usually specified by a type parameter. The Java Collections Framework is a prime example of such collections. A collection usually provides two basic operations: a set operation (also called…