@@ -16,6 +16,7 @@ import (
1616 "go.opentelemetry.io/collector/component"
1717 "go.opentelemetry.io/collector/component/componentstatus"
1818 "go.opentelemetry.io/collector/component/componenttest"
19+ "go.opentelemetry.io/collector/config/configoptional"
1920 "go.opentelemetry.io/collector/internal/memorylimiter/iruntime"
2021)
2122
@@ -39,6 +40,7 @@ func TestMemoryPressureResponse(t *testing.T) {
3940 CheckInterval : 1 * time .Minute ,
4041 MemoryLimitMiB : 1024 ,
4142 MemorySpikeLimitMiB : 0 ,
43+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
4244 }
4345 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
4446 require .NoError (t , err )
@@ -200,6 +202,7 @@ func TestCheckMemLimitsHealthEvents(t *testing.T) {
200202 CheckInterval : 1 * time .Minute ,
201203 MemoryLimitMiB : 50 ,
202204 MemorySpikeLimitMiB : 10 ,
205+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
203206 }, zap .NewNop ())
204207 require .NoError (t , err )
205208 host := & mockHost {}
@@ -238,6 +241,7 @@ func TestCallGCWhenSoftLimit(t *testing.T) {
238241 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
239242 MemoryLimitMiB : 50 ,
240243 MemorySpikeLimitMiB : 10 ,
244+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
241245 },
242246 memAllocMiB : [2 ]uint64 {45 , 45 },
243247 numGCs : 1 ,
@@ -251,6 +255,7 @@ func TestCallGCWhenSoftLimit(t *testing.T) {
251255 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
252256 MemoryLimitMiB : 50 ,
253257 MemorySpikeLimitMiB : 10 ,
258+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
254259 },
255260 memAllocMiB : [2 ]uint64 {45 , 45 },
256261 numGCs : 2 ,
@@ -264,6 +269,7 @@ func TestCallGCWhenSoftLimit(t *testing.T) {
264269 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
265270 MemoryLimitMiB : 50 ,
266271 MemorySpikeLimitMiB : 10 ,
272+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
267273 },
268274 memAllocMiB : [2 ]uint64 {55 , 55 },
269275 numGCs : 1 ,
@@ -277,6 +283,7 @@ func TestCallGCWhenSoftLimit(t *testing.T) {
277283 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
278284 MemoryLimitMiB : 50 ,
279285 MemorySpikeLimitMiB : 10 ,
286+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
280287 },
281288 memAllocMiB : [2 ]uint64 {55 , 55 },
282289 numGCs : 2 ,
@@ -291,10 +298,39 @@ func TestCallGCWhenSoftLimit(t *testing.T) {
291298 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
292299 MemoryLimitMiB : 50 ,
293300 MemorySpikeLimitMiB : 10 ,
301+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
294302 },
295303 memAllocMiB : [2 ]uint64 {45 , 55 },
296304 numGCs : 2 ,
297305 },
306+ {
307+ name : "GC disabled when above soft limit" ,
308+ mlCfg : & Config {
309+ CheckInterval : 1 * time .Minute ,
310+ MinGCIntervalWhenSoftLimited : 0 ,
311+ MaxGCIntervalWhenSoftLimited : 30 * time .Second ,
312+ MaxGCIntervalWhenHardLimited : 30 * time .Second ,
313+ MemoryLimitMiB : 50 ,
314+ MemorySpikeLimitMiB : 10 ,
315+ GarbageCollector : configoptional .None [GarbageCollectorConfig ](),
316+ },
317+ memAllocMiB : [2 ]uint64 {45 , 45 },
318+ numGCs : 0 ,
319+ },
320+ {
321+ name : "GC disabled when above hard limit" ,
322+ mlCfg : & Config {
323+ CheckInterval : 1 * time .Minute ,
324+ MinGCIntervalWhenHardLimited : 0 ,
325+ MaxGCIntervalWhenSoftLimited : 30 * time .Second ,
326+ MaxGCIntervalWhenHardLimited : 30 * time .Second ,
327+ MemoryLimitMiB : 50 ,
328+ MemorySpikeLimitMiB : 10 ,
329+ GarbageCollector : configoptional .None [GarbageCollectorConfig ](),
330+ },
331+ memAllocMiB : [2 ]uint64 {55 , 55 },
332+ numGCs : 0 ,
333+ },
298334 }
299335 for _ , tt := range tests {
300336 t .Run (tt .name , func (t * testing.T ) {
@@ -354,6 +390,7 @@ func TestGCBackoffWhenIneffective(t *testing.T) {
354390 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
355391 MemoryLimitMiB : 50 ,
356392 MemorySpikeLimitMiB : 10 ,
393+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
357394 }
358395 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
359396 require .NoError (t , err )
@@ -406,6 +443,7 @@ func TestGCBackoffResetOnRecovery(t *testing.T) {
406443 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
407444 MemoryLimitMiB : 50 ,
408445 MemorySpikeLimitMiB : 10 ,
446+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
409447 }
410448 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
411449 require .NoError (t , err )
@@ -458,6 +496,7 @@ func TestGCBackoffEarlyResetWhenMemoryBecomesReclaimable(t *testing.T) {
458496 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
459497 MemoryLimitMiB : 50 ,
460498 MemorySpikeLimitMiB : 10 ,
499+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
461500 }
462501 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
463502 require .NoError (t , err )
@@ -509,6 +548,7 @@ func TestGCBackoffEarlyResetAboveHardLimit(t *testing.T) {
509548 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
510549 MemoryLimitMiB : 50 ,
511550 MemorySpikeLimitMiB : 10 ,
551+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
512552 }
513553 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
514554 require .NoError (t , err )
@@ -555,6 +595,7 @@ func TestNextBackoffInterval(t *testing.T) {
555595 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
556596 MemoryLimitMiB : 100 ,
557597 MemorySpikeLimitMiB : 5 ,
598+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
558599 }
559600 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
560601 require .NoError (t , err )
@@ -613,6 +654,7 @@ func TestCheckLimitAndBackoff(t *testing.T) {
613654 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
614655 MemoryLimitMiB : 100 ,
615656 MemorySpikeLimitMiB : 5 ,
657+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
616658 }
617659 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
618660 require .NoError (t , err )
@@ -679,6 +721,7 @@ func TestCheckLimitAndBackoff(t *testing.T) {
679721 MaxGCIntervalWhenHardLimited : 0 ,
680722 MemoryLimitMiB : 100 ,
681723 MemorySpikeLimitMiB : 5 ,
724+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
682725 }
683726 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
684727 require .NoError (t , err )
@@ -701,6 +744,7 @@ func TestBackoffDisabledOptOut(t *testing.T) {
701744 MaxGCIntervalWhenHardLimited : 0 ,
702745 MemoryLimitMiB : 50 ,
703746 MemorySpikeLimitMiB : 10 ,
747+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
704748 }
705749 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
706750 require .NoError (t , err )
@@ -736,6 +780,7 @@ func TestPerPathBackoffIsIndependent(t *testing.T) {
736780 MaxGCIntervalWhenHardLimited : 0 ,
737781 MemoryLimitMiB : 50 ,
738782 MemorySpikeLimitMiB : 10 ,
783+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
739784 }
740785 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
741786 require .NoError (t , err )
@@ -774,6 +819,7 @@ func TestNewDefaultConfigEnablesBackoffCap(t *testing.T) {
774819 cfg := NewDefaultConfig ()
775820 assert .Equal (t , 30 * time .Second , cfg .MaxGCIntervalWhenSoftLimited , "soft backoff cap should default to 30s" )
776821 assert .Equal (t , 30 * time .Second , cfg .MaxGCIntervalWhenHardLimited , "hard backoff cap should default to 30s" )
822+ assert .True (t , cfg .GarbageCollector .HasValue (), "garbage collector should be enabled by default" )
777823}
778824
779825func TestBackoffGrowsBothPathsInLockstep (t * testing.T ) {
@@ -798,6 +844,7 @@ func TestBackoffGrowsBothPathsInLockstep(t *testing.T) {
798844 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
799845 MemoryLimitMiB : 50 ,
800846 MemorySpikeLimitMiB : 10 ,
847+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
801848 }
802849 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
803850 require .NoError (t , err )
@@ -842,6 +889,7 @@ func TestGCEffectivenessWhenPressureResolved(t *testing.T) {
842889 MaxGCIntervalWhenHardLimited : 30 * time .Second ,
843890 MemoryLimitMiB : 100 ,
844891 MemorySpikeLimitMiB : 5 ,
892+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
845893 }
846894 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
847895 require .NoError (t , err )
@@ -900,6 +948,7 @@ func TestGCRecovery(t *testing.T) {
900948 CheckInterval : 1 * time .Minute ,
901949 MemoryLimitMiB : 50 ,
902950 MemorySpikeLimitMiB : 10 ,
951+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
903952 }, zap .NewNop ())
904953 require .NoError (t , err )
905954
@@ -933,6 +982,7 @@ func TestStart(t *testing.T) {
933982 MinGCIntervalWhenSoftLimited : 10 * time .Second ,
934983 MemoryLimitMiB : 50 ,
935984 MemorySpikeLimitMiB : 10 ,
985+ GarbageCollector : configoptional .Some (GarbageCollectorConfig {}),
936986 }
937987 ml , err := NewMemoryLimiter (cfg , zap .NewNop ())
938988 require .NoError (t , err )
0 commit comments