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 *

  • 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.…

  • Unbounded Type References: <Type> The type of the reference s0 is Node<Number>, where Type is Number. The actual type parameter for each method call is determined to be Number. Thus the type of the parameter in the setData() method and the return value of the getData() method is Number. Therefore, we can pass the reference…

  • 11.6 Bounded Type Parameters In the declaration of the generic class Node<E>, the type parameter E is unbounded— that is, it can be any reference type. However, sometimes it may be necessary to restrict what type the type parameter E can represent. The canonical example is restricting that the type parameter E is Comparable<E> so…

  • Multiple Bounds A bounded type parameter can have multiple bounds, B1 & B2 & … & Bn, which must be satisfied by the actual type parameter: Click here to view code image class CmpNode<E extends Number & Serializable> … An extra bound, the Serializable interface, has been added using the ampersand (&). The formal type…

  • 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…

  • 11.3 Collections and Generics Before the introduction of generics in Java 1.5, a collection in the Java Collections Framework could hold references to objects of any type. For example, any object which is an instance of the java.lang.Object class or its subclasses could be maintained in an instance of the java.util.ArrayList class, which implements the…

  • Raw Types and Unchecked Warnings A generic type without its formal type parameters is called a raw type. The raw type is the supertype of all parameterized types of the generic type. For example, the raw type Node is the supertype of the parameterized types Node<String>, Node<Integer>, and Node<Node<String>>. The last parameterized type is an…

  • Exception Handling and Instance Initializer Blocks Exception handling in instance initializer blocks is similar to that in static initializer blocks. Example 10.8 shows an instance initializer block at (3) that catches and handles a checked exception in the try-catch block at (4). Another instance initializer block at (5) throws an unchecked exception at (6). The…

  • 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…

  • 10.8 Instance Initializer Blocks Just as static initializer blocks can be used to initialize static fields in a named class, Java provides the ability to initialize fields during object creation using instance initializer blocks. In this respect, such blocks serve the same purpose as constructors during object creation. The syntax of an instance initializer block…