Package org.plumelib.util
Class SystemPlume
java.lang.Object
org.plumelib.util.SystemPlume
Utility methods relating to the JVM runtime system: sleep and garbage collection.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA triple of (timestamp, collection time, subsequent timestamp). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static Deque<SystemPlume.GcHistoryItem>The history of recent garbage collection runs.private static RuntimeThe Runtime instance for the current execution. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateThis class is a collection of methods; it does not represent anything. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidgc()Perform garbage collection.static doubleCalls `gcPercentage(60)`.static doublegcPercentage(int seconds) Returns the fraction of time spent garbage collecting, in the pastsecondsseconds.static @Nullable StringgcUsageMessage(double cpuThreshold, int seconds) If the fraction of time spent garbage collecting in the pastsecondsseconds is less thancpuThreshold, returns null.static booleanDetermines whether a system property has a string value that represents true: "true", "yes", or "1".static booleangetBooleanSystemProperty(String key, boolean defaultValue) Determines whether a system property has a string value that represents true: "true", "yes", or "1".private static longReturns the number of garbage collections that have occurred.private static longReturns the cumulative garbage collection time in milliseconds, across all threads.static voidsleep(long millis) Like Thread.sleep, but does not throw any checked exceptions, so it is easier for clients to use.static longReturns the amount of used memory in the JVM.static longusedMemory(boolean forceGc) Returns the amount of used memory in the JVM.
-
Field Details
-
runtime
The Runtime instance for the current execution. -
gcHistory
The history of recent garbage collection runs. The queue is never empty.
-
-
Constructor Details
-
SystemPlume
private SystemPlume()This class is a collection of methods; it does not represent anything.
-
-
Method Details
-
getBooleanSystemProperty
Determines whether a system property has a string value that represents true: "true", "yes", or "1". Errs if the property is set to a value that is not one of "true", "false", "yes", "no", "1", or "0".- Parameters:
key- name of the property to look updefaultValue- the value to return if the property is not set- Returns:
- true iff the property has a string value that represents true
-
getBooleanSystemProperty
Determines whether a system property has a string value that represents true: "true", "yes", or "1". Errs if the property is set to a value that is not one of "true", "false", "yes", "no", "1", or "0".- Parameters:
key- name of the property to look up- Returns:
- true iff the property has a string value that represents true
-
sleep
public static void sleep(long millis) Like Thread.sleep, but does not throw any checked exceptions, so it is easier for clients to use. Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.- Parameters:
millis- the length of time to sleep in milliseconds
-
usedMemory
public static long usedMemory()Returns the amount of used memory in the JVM.To force a garbage collection, which gives a more accurate overapproximation of the memory used, but is also slower, use
usedMemory(boolean)- Returns:
- the amount of used memory
-
usedMemory
public static long usedMemory(boolean forceGc) Returns the amount of used memory in the JVM.- Parameters:
forceGc- if true, force a garbage collection, which gives a more accurate overapproximation of the memory used, but is also slower- Returns:
- the amount of used memory
-
gc
public static void gc()Perform garbage collection. Like System.gc, but waits to return until garbage collection has completed. -
getCollectionCount
private static long getCollectionCount()Returns the number of garbage collections that have occurred.- Returns:
- the number of garbage collections that have occurred
-
getCollectionTime
private static long getCollectionTime()Returns the cumulative garbage collection time in milliseconds, across all threads.- Returns:
- the cumulative garbage collection time in milliseconds
-
gcPercentage
public static double gcPercentage()Calls `gcPercentage(60)`.- Returns:
- the percentage of time spent garbage collecting, in the past minute
- See Also:
-
gcPercentage
public static double gcPercentage(int seconds) Returns the fraction of time spent garbage collecting, in the pastsecondsseconds. This is generally a value between 0 and 1. This method might return a value greater than 1 if multiple threads are spending all their time collecting. Returns 0 ifgcPercentagewas not first called more thansecondsseconds ago.This method also discards all GC history older than
secondsseconds.Instead of calling this method directly, a client program might call
gcUsageMessage(double, int).- Parameters:
seconds- the size of the time window, in seconds- Returns:
- the percentage of time spent garbage collecting, in the past
secondsseconds
-
gcUsageMessage
If the fraction of time spent garbage collecting in the pastsecondsseconds is less thancpuThreshold, returns null. Otherwise, returns a string indicating garbage collection CPU usage and memory statistics. The string is multiple lines long, but does not end with a line separator.A typical use is to put the following in an outer loop that takes a significant amount of time (more than a second) to execute:
String message = gcUsageMessage(.25, 60); if (message != null) { System.err.println(message); }- Parameters:
cpuThreshold- the maximum fraction of CPU that should be spent garbage collecting; a number between 0 and 1seconds- the time window in which to compute the garbage collection CPU usage- Returns:
- a GC usage message string, or null
-