Class MemoryMonitor
- java.lang.Object
-
- org.apache.beam.fn.harness.status.MemoryMonitor
-
- All Implemented Interfaces:
java.lang.Runnable
public class MemoryMonitor extends java.lang.Object implements java.lang.RunnableA runnable which monitors a server for GC thrashing.Note: Only one instance of this should be initialized per server and it should be done when the server starts running.
This runnable works as follows:
- It wakes up periodically and determines how much time was spend on garbage collection since the last time it woke up.
- If the time spent in garbage collection in the last period of time exceeds a certain threshold, that period is marked as "being in GC thrashing"
- It keeps track of the GC thrashing status of the last few periods.
- Every time the runnable's thread wakes up, it computes the ratio
(# monitored periods in GC thrashing) / (# monitored periods). - If this ratio exceeds a certain threshold, it is assumed that the server is in GC thrashing.
- It can also shutdown the current jvm runtime when a threshold of consecutive gc thrashing count is met. A heap dump is made before shutdown.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMemoryMonitor.GCStatsProviderAbstract interface for providing GC stats (for testing).
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_SLEEP_TIME_MILLISAmount of time (in ms) this thread must sleep between two consecutive iterations.static java.lang.StringLOCAL_HEAPDUMP_DIR_SYSTEM_PROPERTY_NAME
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringdescribeMemory()Return a string describing the current memory state of the server.java.io.FiledumpHeap()Dump the current heap profile to a file in the given directory and return its name.static MemoryMonitorfromOptions(org.apache.beam.sdk.options.PipelineOptions options)booleanisThrashing()voidrun()Runs this thread.voidstop()Request the memory monitor stops.@Nullable java.io.FiletryToDumpHeap()Dumps the heap to a file and return the name of the file, or null if the heap should not or could not be dumped.voidwaitForResources(java.lang.String context)Return only when the server is not in the GC thrashing state.voidwaitForThrashingState(boolean desiredThrashingState)For testing only: Wait for thrashing status to be updated to given value.
-
-
-
Field Detail
-
DEFAULT_SLEEP_TIME_MILLIS
public static final long DEFAULT_SLEEP_TIME_MILLIS
Amount of time (in ms) this thread must sleep between two consecutive iterations.- See Also:
- Constant Field Values
-
LOCAL_HEAPDUMP_DIR_SYSTEM_PROPERTY_NAME
public static final java.lang.String LOCAL_HEAPDUMP_DIR_SYSTEM_PROPERTY_NAME
- See Also:
- Constant Field Values
-
-
Method Detail
-
fromOptions
public static MemoryMonitor fromOptions(org.apache.beam.sdk.options.PipelineOptions options)
-
waitForThrashingState
public void waitForThrashingState(boolean desiredThrashingState)
For testing only: Wait for thrashing status to be updated to given value.
-
stop
public void stop()
Request the memory monitor stops.
-
isThrashing
public boolean isThrashing()
-
tryToDumpHeap
public @Nullable java.io.File tryToDumpHeap()
Dumps the heap to a file and return the name of the file, or null if the heap should not or could not be dumped.- Returns:
- The name of the file the heap was dumped to, otherwise null.
-
run
public void run()
Runs this thread.- Specified by:
runin interfacejava.lang.Runnable
-
waitForResources
public void waitForResources(java.lang.String context)
Return only when the server is not in the GC thrashing state.
-
dumpHeap
public java.io.File dumpHeap() throws javax.management.MalformedObjectNameException, javax.management.InstanceNotFoundException, javax.management.ReflectionException, javax.management.MBeanException, java.io.IOExceptionDump the current heap profile to a file in the given directory and return its name.NOTE: We deliberately don't salt the heap dump filename so as to minimize disk impact of repeated dumps. These files can be of comparable size to the local disk.
- Throws:
javax.management.MalformedObjectNameExceptionjavax.management.InstanceNotFoundExceptionjavax.management.ReflectionExceptionjavax.management.MBeanExceptionjava.io.IOException
-
describeMemory
public java.lang.String describeMemory()
Return a string describing the current memory state of the server.
-
-