Package com.lucimber.dbus.util
Class MemoryOptimizer
java.lang.Object
com.lucimber.dbus.util.MemoryOptimizer
Utility class for memory optimization and monitoring in D-Bus Client Java.
This class provides tools for:
- Monitoring memory usage and detecting memory pressure
- Providing guidance on when to trigger cleanup operations
- Collecting statistics about object allocation patterns
- Suggesting optimal collection sizes
This is primarily intended for internal use by the library to make intelligent decisions about memory management, such as when to clean up cached objects or resize collections.
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
Gets the current cache hit ratio.static double
Gets the current heap memory usage as a ratio (0.0 to 1.0).static String
Returns current memory usage statistics as a formatted string.static boolean
Checks if the system is in critical memory state.static boolean
Checks if the system is currently under memory pressure.static void
recordAllocation
(long bytes) Records an allocation for statistics tracking.static void
Records a cache hit for statistics tracking.static void
Records a cache miss for statistics tracking.static void
Resets all internal statistics counters.static boolean
shouldPerformCleanup
(long operationCount, long cleanupThreshold) Suggests whether a cleanup operation should be performed based on current memory conditions and operation frequency.static int
suggestCollectionCapacity
(int expectedSize, int elementSize) Suggests an optimal initial capacity for collections based on memory pressure and expected size.
-
Method Details
-
isUnderMemoryPressure
public static boolean isUnderMemoryPressure()Checks if the system is currently under memory pressure. This can be used to trigger cleanup operations or reduce caching.- Returns:
- true if memory usage is above the high threshold
-
isInCriticalMemoryState
public static boolean isInCriticalMemoryState()Checks if the system is in critical memory state. This should trigger immediate cleanup and reduce allocations.- Returns:
- true if memory usage is above the critical threshold
-
getHeapUsageRatio
public static double getHeapUsageRatio()Gets the current heap memory usage as a ratio (0.0 to 1.0).- Returns:
- current heap usage ratio
-
suggestCollectionCapacity
public static int suggestCollectionCapacity(int expectedSize, int elementSize) Suggests an optimal initial capacity for collections based on memory pressure and expected size.- Parameters:
expectedSize
- expected number of elementselementSize
- approximate size per element in bytes- Returns:
- suggested initial capacity
-
recordAllocation
public static void recordAllocation(long bytes) Records an allocation for statistics tracking.- Parameters:
bytes
- number of bytes allocated
-
recordCacheHit
public static void recordCacheHit()Records a cache hit for statistics tracking. -
recordCacheMiss
public static void recordCacheMiss()Records a cache miss for statistics tracking. -
getCacheHitRatio
public static double getCacheHitRatio()Gets the current cache hit ratio.- Returns:
- cache hit ratio (0.0 to 1.0)
-
getMemoryStatistics
Returns current memory usage statistics as a formatted string. Useful for logging and debugging.- Returns:
- memory statistics string
-
shouldPerformCleanup
public static boolean shouldPerformCleanup(long operationCount, long cleanupThreshold) Suggests whether a cleanup operation should be performed based on current memory conditions and operation frequency.- Parameters:
operationCount
- number of operations since last cleanupcleanupThreshold
- normal threshold for cleanup- Returns:
- true if cleanup is recommended
-
resetStatistics
public static void resetStatistics()Resets all internal statistics counters. Useful for testing or periodic stats reporting.
-