Class ResourceTracker
java.lang.Object
express.mvp.myra.transport.memory.ResourceTracker
Tracks native memory allocations for debugging and leak detection.
This tracker maintains a registry of all active allocations with their metadata, enabling detection of memory leaks and analysis of allocation patterns.
Design Goals
- Low overhead: Thread-safe but minimal synchronization
- Rich metadata: Captures allocation site, size, time
- Leak detection: Identifies unreleased allocations at shutdown
- Configurable: Can be disabled in production
Usage Example
// Enable tracking
ResourceTracker tracker = ResourceTracker.getInstance();
tracker.setEnabled(true);
tracker.setCaptureStackTraces(true); // Expensive, use in dev only
// Track allocation
long id = tracker.trackAllocation("BufferPool", 65536);
// ... use resource ...
// Track release
tracker.trackRelease(id);
// At shutdown, check for leaks
Collection<AllocationRecord> leaks = tracker.getActiveAllocations();
if (!leaks.isEmpty()) {
logger.warn("Memory leak detected: {} unreleased allocations", leaks.size());
for (AllocationRecord leak : leaks) {
logger.warn(" Leaked: {} ({} bytes, allocated at {})",
leak.getSource(), leak.getSize(), leak.getAllocationTime());
}
}
Performance Considerations
- Stack trace capture is expensive - disable in production
- ConcurrentHashMap provides good concurrent performance
- ID generation uses AtomicLong for lock-free operation
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRecord of a tracked allocation. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all tracking data.longReturns the total bytes currently allocated.intReturns the number of active allocations.Returns all active (unreleased) allocations.longReturns total allocation count.static ResourceTrackerReturns the singleton instance.longReturns total release count.Returns a summary of tracking statistics.longReturns cumulative allocated bytes.longReturns cumulative released bytes.booleanChecks if tracking is enabled.voidsetCaptureStackTraces(boolean capture) Enables or disables stack trace capture.voidsetEnabled(boolean enabled) Enables or disables tracking.longtrackAllocation(String source, long sizeBytes) Tracks a new allocation.booleantrackRelease(long allocationId) Tracks release of an allocation.
-
Method Details
-
getInstance
Returns the singleton instance.- Returns:
- the tracker instance
-
setEnabled
public void setEnabled(boolean enabled) Enables or disables tracking.- Parameters:
enabled- true to enable tracking
-
isEnabled
public boolean isEnabled()Checks if tracking is enabled.- Returns:
- true if tracking is enabled
-
setCaptureStackTraces
public void setCaptureStackTraces(boolean capture) Enables or disables stack trace capture.Warning: Stack trace capture is expensive and should only be enabled during development or debugging.
- Parameters:
capture- true to capture stack traces
-
trackAllocation
Tracks a new allocation.- Parameters:
source- identifier for the allocation source (e.g., "BufferPool")sizeBytes- the allocation size in bytes- Returns:
- allocation ID for later release tracking
-
trackRelease
public boolean trackRelease(long allocationId) Tracks release of an allocation.- Parameters:
allocationId- the ID returned from trackAllocation- Returns:
- true if the allocation was found and removed
-
getActiveAllocations
Returns all active (unreleased) allocations.- Returns:
- collection of active allocation records
-
getActiveAllocationCount
public int getActiveAllocationCount()Returns the number of active allocations.- Returns:
- active allocation count
-
getActiveAllocationBytes
public long getActiveAllocationBytes()Returns the total bytes currently allocated.- Returns:
- total active allocation size
-
getTotalAllocated
public long getTotalAllocated()Returns cumulative allocated bytes.- Returns:
- total allocated bytes
-
getTotalReleased
public long getTotalReleased()Returns cumulative released bytes.- Returns:
- total released bytes
-
getAllocationCount
public long getAllocationCount()Returns total allocation count.- Returns:
- allocation count
-
getReleaseCount
public long getReleaseCount()Returns total release count.- Returns:
- release count
-
clear
public void clear()Clears all tracking data. -
getSummary
Returns a summary of tracking statistics.- Returns:
- formatted statistics string
-