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 *

Extending Generic Types

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

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

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

  • 10.7 Static Initializer Blocks Java allows static initializer blocks to be defined in a class. Although such blocks can include arbitrary code, they are primarily used for initializing static fields. The code in a static initializer block is executed only once, when the class is loaded and initialized. Local variables in static and instance initializer…

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

  • Generic Interfaces Generic types also include generic interfaces, which are declared analogous to generic classes. The specification of formal type parameters in a generic interface is the same as in a generic class. Example 11.3 declares a generic interface that defines the reference type IMonoLink<E> for objects that store a data value of type E.…

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

  • Subtype Contravariance: ? super Type The wildcard type ? super Type represents all supertypes of Type (including Type itself). The wildcard type ? super Type is called a lower bounded wildcard with Type representing its lower bound. Figure 11.4 Partial Type Hierarchy for Node<? super Integer> The wildcard type ? super Integer denotes all supertypes…

  • Upper Bounded Wildcard References: <? extends Type> The type of the reference s0 is Node<? extends Number>, where Type is Number. This means that the reference s0 refers to a node containing an object whose type is either Number or a subtype of Number, but the specific (sub)type of the object cannot always be determined…

  • Lower Bounded Wildcard References: <? super Type> Using a reference of type Node<? super Number>, where Type is Number, we can only put a Number or a subtype object of Number into the node, as such a number would also be a subtype object of any supertype of Number. Since we cannot guarantee which specific…