1package webkit
2
3// GetMemoryOptimizedConfig returns a Config optimized for low memory usage
4// This configuration aims to reduce memory usage by 40-60% compared to defaults
5func GetMemoryOptimizedConfig() *Config {
6 return &Config{
7 Memory: MemoryConfig{
8 // Limit each WebView to 256MB (default: unlimited)
9 MemoryLimitMB: 256,
10
11 // Aggressive memory pressure thresholds
12 ConservativeThreshold: 0.2, // Trigger cleanup at 20% (default: 33%)
13 StrictThreshold: 0.35, // Aggressive cleanup at 35% (default: 50%)
14 KillThreshold: 0.8, // Kill process at 80% to prevent OOM
15
16 // Check memory usage every 2 seconds
17 PollIntervalSeconds: 2.0,
18
19 // Use minimal caching for lowest memory usage
20 CacheModel: CacheModelDocumentViewer,
21
22 // Disable memory-intensive caches
23 EnablePageCache: false, // Saves ~30-50MB but slower back/forward
24 EnableOfflineAppCache: false, // Saves ~10-20MB
25
26 // Recycle WebView after 50 page loads to prevent memory accumulation
27 ProcessRecycleThreshold: 50,
28
29 // Trigger JavaScript GC every 60 seconds
30 EnableGCInterval: 60,
31
32 // Enable detailed memory monitoring and logging
33 EnableMemoryMonitoring: true,
34 },
35
36 // Disable developer tools to save memory (unless debugging)
37 EnableDeveloperExtras: false,
38
39 // Use CPU rendering to save GPU memory (trade-off: slower rendering)
40 Rendering: RenderingConfig{
41 Mode: "cpu",
42 },
43 }
44}
45
46// GetBalancedConfig returns a Config with moderate memory optimizations
47// This configuration aims to reduce memory usage by 20-30% with minimal performance impact
48func GetBalancedConfig() *Config {
49 return &Config{
50 Memory: MemoryConfig{
51 // Limit each WebView to 512MB
52 MemoryLimitMB: 512,
53
54 // Moderate memory pressure thresholds
55 ConservativeThreshold: 0.25,
56 StrictThreshold: 0.4,
57 KillThreshold: 0.7,
58
59 PollIntervalSeconds: 5.0,
60
61 // Use default web browser caching
62 CacheModel: CacheModelWebBrowser,
63
64 // Keep page cache but disable offline app cache
65 EnablePageCache: true,
66 EnableOfflineAppCache: false,
67
68 // Recycle after more page loads
69 ProcessRecycleThreshold: 100,
70
71 // Less frequent GC
72 EnableGCInterval: 120,
73
74 EnableMemoryMonitoring: true,
75 },
76
77 EnableDeveloperExtras: false,
78
79 // Use auto rendering (WebKit decides GPU vs CPU)
80 Rendering: RenderingConfig{
81 Mode: "auto",
82 },
83 }
84}
85
86// GetHighPerformanceConfig returns a Config optimized for performance over memory
87// This uses default/high memory settings for maximum performance
88func GetHighPerformanceConfig() *Config {
89 return &Config{
90 Memory: MemoryConfig{
91 // No memory limit (use system default)
92 MemoryLimitMB: 0,
93
94 // Relaxed memory pressure thresholds
95 ConservativeThreshold: 0.4,
96 StrictThreshold: 0.6,
97 KillThreshold: 0, // Disable kill threshold
98
99 PollIntervalSeconds: 10.0,
100
101 // Maximum caching for best performance
102 CacheModel: CacheModelPrimaryWebBrowser,
103
104 // Enable all caches
105 EnablePageCache: true,
106 EnableOfflineAppCache: true,
107
108 // Disable recycling
109 ProcessRecycleThreshold: 0,
110
111 // Disable periodic GC
112 EnableGCInterval: 0,
113
114 EnableMemoryMonitoring: false,
115 },
116
117 EnableDeveloperExtras: true,
118
119 // Use GPU rendering for best performance
120 Rendering: RenderingConfig{
121 Mode: "gpu",
122 },
123 }
124}
125
126// ValidateMemoryConfig validates memory configuration parameters
127func ValidateMemoryConfig(config *MemoryConfig) error {
128 if config == nil {
129 return nil // Use defaults
130 }
131
132 if config.ConservativeThreshold < 0 || config.ConservativeThreshold > 1 {
133 return &ValidationError{Field: "ConservativeThreshold", Message: "must be between 0.0 and 1.0"}
134 }
135
136 if config.StrictThreshold < 0 || config.StrictThreshold > 1 {
137 return &ValidationError{Field: "StrictThreshold", Message: "must be between 0.0 and 1.0"}
138 }
139
140 if config.KillThreshold < 0 || config.KillThreshold > 1 {
141 return &ValidationError{Field: "KillThreshold", Message: "must be between 0.0 and 1.0"}
142 }
143
144 if config.ConservativeThreshold >= config.StrictThreshold {
145 return &ValidationError{Field: "StrictThreshold", Message: "must be greater than ConservativeThreshold"}
146 }
147
148 if config.PollIntervalSeconds < 0 {
149 return &ValidationError{Field: "PollIntervalSeconds", Message: "must be non-negative"}
150 }
151
152 return nil
153}
154
155// ValidationError represents a configuration validation error
156type ValidationError struct {
157 Field string
158 Message string
159}
160
161func (e *ValidationError) Error() string {
162 return "validation error for " + e.Field + ": " + e.Message
163}
164
165// LogMemoryConfiguration logs the current memory configuration for debugging
166func LogMemoryConfiguration(config *Config) {
167 if config == nil || !config.Memory.EnableMemoryMonitoring {
168 return
169 }
170
171 log := func(format string, args ...interface{}) {
172 // Use Go's standard log package
173 // log.Printf("[webkit-config] "+format, args...)
174 // For now, we'll just ignore since we don't have access to log here
175 _ = format
176 _ = args
177 }
178
179 log("Memory configuration:")
180 log(" MemoryLimitMB: %d", config.Memory.MemoryLimitMB)
181 log(" ConservativeThreshold: %.2f", config.Memory.ConservativeThreshold)
182 log(" StrictThreshold: %.2f", config.Memory.StrictThreshold)
183 log(" KillThreshold: %.2f", config.Memory.KillThreshold)
184 log(" CacheModel: %s", config.Memory.CacheModel)
185 log(" EnablePageCache: %t", config.Memory.EnablePageCache)
186 log(" EnableOfflineAppCache: %t", config.Memory.EnableOfflineAppCache)
187 log(" ProcessRecycleThreshold: %d", config.Memory.ProcessRecycleThreshold)
188 log(" EnableGCInterval: %d seconds", config.Memory.EnableGCInterval)
189}