Documentation
¶
Overview ¶
Package memory provides integration examples showing how to refactor existing duplicate code patterns to use the consolidated memory utilities.
This file demonstrates how the duplication patterns identified in similarity analysis can be replaced with shared utilities, reducing code duplication by approximately 40% in affected areas.
Package memory provides consolidated memory management utilities to reduce code duplication across streaming, batch processing, and parallel execution components.
This package addresses the duplication patterns identified in similarity analysis: - GC triggering logic (forceGC pattern) - Memory estimation calculations (estimateMemoryUsage logic) - Resource lifecycle management (create/process/cleanup pattern) - Memory pressure detection and cleanup callbacks - Allocation/deallocation tracking
Index ¶
- func EstimateMemoryUsage(resources ...interface{}) int64
- func EstimateMemoryUsageWithAllocator(_ memory.Allocator, resources ...interface{}) int64
- func ExampleMemoryEstimationRefactored(data1 []int64, data2 []string)
- func ExampleMemoryPressureRefactored()
- func ExampleResourceLifecycleRefactored()
- func ExampleResourceManagerRefactored()
- func ExampleStreamingProcessorRefactored()
- func ForceGC()
- type GCStrategy
- type GCTrigger
- type Option
- type PressureHandler
- func (mph *PressureHandler) RecordAllocation(bytes int64)
- func (mph *PressureHandler) RecordDeallocation(bytes int64)
- func (mph *PressureHandler) SetCleanupCallback(callback func() error)
- func (mph *PressureHandler) SetSpillCallback(callback func() error)
- func (mph *PressureHandler) Start()
- func (mph *PressureHandler) Stop()
- type Resource
- type ResourceLifecycleManager
- func (rlm *ResourceLifecycleManager) CreateResource(id string, factory func(memory.Allocator) (Resource, error)) (Resource, error)
- func (rlm *ResourceLifecycleManager) ProcessResource(resource Resource, processor func(Resource) (Resource, error)) (Resource, error)
- func (rlm *ResourceLifecycleManager) ReleaseAll()
- func (rlm *ResourceLifecycleManager) TrackedCount() int
- type ResourceManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EstimateMemoryUsage ¶
func EstimateMemoryUsage(resources ...interface{}) int64
EstimateMemoryUsage provides centralized memory estimation for various data types. This consolidates the memory calculation logic found in multiple components.
func EstimateMemoryUsageWithAllocator ¶
EstimateMemoryUsageWithAllocator estimates memory usage considering allocator overhead.
func ExampleMemoryEstimationRefactored ¶
ExampleMemoryEstimationRefactored demonstrates replacing estimateMemoryUsage pattern. BEFORE: Multiple components had similar memory estimation logic
In streaming.go:
func (mr *MemoryAwareChunkReader) estimateMemoryUsage(df *DataFrame) int64 {
return int64(df.Len() * df.Width() * BytesPerValue)
}
In batch processing - similar calculations ¶
AFTER: Use consolidated EstimateMemoryUsage utility.
func ExampleMemoryPressureRefactored ¶
func ExampleMemoryPressureRefactored()
ExampleMemoryPressureRefactored demonstrates replacing memory pressure handling patterns. BEFORE: Similar pressure detection and callback patterns
In streaming.go:
if stats.MemoryPressure > HighMemoryPressureThreshold {
sp.forceGC()
}
In memory.go:
if pressure > m.gcPressureThreshold {
// trigger cleanup
}
AFTER: Use MemoryPressureHandler.
func ExampleResourceLifecycleRefactored ¶
func ExampleResourceLifecycleRefactored()
ExampleResourceLifecycleRefactored demonstrates replacing resource lifecycle patterns. BEFORE: Similar create/process/cleanup patterns in multiple places
AFTER: Use ResourceLifecycleManager.
func ExampleResourceManagerRefactored ¶
func ExampleResourceManagerRefactored()
ExampleResourceManagerRefactored demonstrates replacing ResourceManager patterns. BEFORE: Each component managed resources differently
AFTER: Use unified ResourceManager interface.
func ExampleStreamingProcessorRefactored ¶
func ExampleStreamingProcessorRefactored()
ExampleStreamingProcessorRefactored demonstrates replacing forceGC() pattern across multiple components. BEFORE: Each component had its own forceGC implementation
In streaming.go:
func (sp *StreamingProcessor) forceGC() {
// This will be implemented with proper GC triggering
// For now, we'll just mark the need for cleanup
}
In batch processing, parallel execution, etc. - similar patterns
AFTER: Use consolidated ForceGC() utility.
Types ¶
type GCStrategy ¶
type GCStrategy int
GCStrategy represents different garbage collection strategies.
const ( // ConservativeGC triggers GC only under high memory pressure. ConservativeGC GCStrategy = iota // AggressiveGC triggers GC more frequently. AggressiveGC // AdaptiveGC adapts based on system conditions. AdaptiveGC )
type GCTrigger ¶
type GCTrigger struct {
// contains filtered or unexported fields
}
GCTrigger provides configurable GC triggering strategies.
func NewGCTrigger ¶
func NewGCTrigger(strategy GCStrategy, threshold float64) *GCTrigger
NewGCTrigger creates a new GC trigger with the specified strategy.
func (*GCTrigger) ShouldTriggerGC ¶
ShouldTriggerGC determines if GC should be triggered based on memory pressure.
type Option ¶
type Option func(*resourceManager)
Option configures the ResourceManager.
func WithAllocator ¶
WithAllocator sets the memory allocator.
func WithGCPressureThreshold ¶
WithGCPressureThreshold sets the GC pressure threshold (0.0-1.0).
func WithMemoryThreshold ¶
WithMemoryThreshold sets the memory threshold in bytes.
type PressureHandler ¶
type PressureHandler struct {
// contains filtered or unexported fields
}
PressureHandler consolidates memory pressure detection and cleanup logic.
func NewPressureHandler ¶
func NewPressureHandler(threshold int64, gcThreshold float64) *PressureHandler
NewPressureHandler creates a new memory pressure handler.
func (*PressureHandler) RecordAllocation ¶
func (mph *PressureHandler) RecordAllocation(bytes int64)
RecordAllocation records memory allocation.
func (*PressureHandler) RecordDeallocation ¶
func (mph *PressureHandler) RecordDeallocation(bytes int64)
RecordDeallocation records memory deallocation.
func (*PressureHandler) SetCleanupCallback ¶
func (mph *PressureHandler) SetCleanupCallback(callback func() error)
SetCleanupCallback sets the callback for cleanup operations.
func (*PressureHandler) SetSpillCallback ¶
func (mph *PressureHandler) SetSpillCallback(callback func() error)
SetSpillCallback sets the callback for spilling operations.
func (*PressureHandler) Start ¶
func (mph *PressureHandler) Start()
Start starts the memory pressure monitoring.
func (*PressureHandler) Stop ¶
func (mph *PressureHandler) Stop()
Stop stops the memory pressure monitoring.
type Resource ¶
type Resource interface {
EstimateMemory() int64
ForceCleanup() error
SpillIfNeeded() error
Release()
}
Resource represents a manageable memory resource.
type ResourceLifecycleManager ¶
type ResourceLifecycleManager struct {
// contains filtered or unexported fields
}
ResourceLifecycleManager manages the create/process/cleanup lifecycle pattern found across streaming and batch processing components.
func NewResourceLifecycleManager ¶
func NewResourceLifecycleManager(allocator memory.Allocator) *ResourceLifecycleManager
NewResourceLifecycleManager creates a new lifecycle manager.
func (*ResourceLifecycleManager) CreateResource ¶
func (rlm *ResourceLifecycleManager) CreateResource( id string, factory func(memory.Allocator) (Resource, error), ) (Resource, error)
CreateResource creates a new resource using the provided factory function.
func (*ResourceLifecycleManager) ProcessResource ¶
func (rlm *ResourceLifecycleManager) ProcessResource( resource Resource, processor func(Resource) (Resource, error), ) (Resource, error)
ProcessResource applies a processing function to a resource.
func (*ResourceLifecycleManager) ReleaseAll ¶
func (rlm *ResourceLifecycleManager) ReleaseAll()
ReleaseAll releases all tracked resources.
func (*ResourceLifecycleManager) TrackedCount ¶
func (rlm *ResourceLifecycleManager) TrackedCount() int
TrackedCount returns the number of tracked resources.
type ResourceManager ¶
type ResourceManager interface {
Resource
// Track adds a resource to be managed
Track(resource Resource)
}
ResourceManager provides a unified interface for managing memory resources with estimation, cleanup, and spill capabilities.
func NewResourceManager ¶
func NewResourceManager(opts ...Option) (ResourceManager, error)
NewResourceManager creates a new resource manager with the specified options.