Skip to content

Commit be4dbe3

Browse files
committed
Allow GC to collect unneeded slice elements
1 parent ff07838 commit be4dbe3

File tree

7 files changed

+8
-5
lines changed

7 files changed

+8
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4141
- Support scope attributes and make them as identifying for `Meter` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/metric`. (#5926)
4242
- Support scope attributes and make them as identifying for `Logger` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/log`. (#5925)
4343
- Make schema URL and scope attributes as identifying for `Tracer` in `go.opentelemetry.io/otel/bridge/opentracing`. (#5931)
44+
- Clear unneeded slice elements to allow GC to collect the objects in `go.opentelemetry.io/otel/sdk/log`, `go.opentelemetry.io/otel/metric`, and `go.opentelemetry.io/otel/trace`. (#5804)
4445

4546
### Removed
4647

sdk/log/record.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (r *Record) AddAttributes(attrs ...log.KeyValue) {
234234
//
235235
// Do not use head(attrs, r.attributeCountLimit - n) here. If
236236
// (r.attributeCountLimit - n) <= 0 attrs needs to be emptied.
237-
last := max(0, (r.attributeCountLimit - n))
237+
last := max(0, r.attributeCountLimit-n)
238238
r.addDropped(len(attrs) - last)
239239
attrs = attrs[:last]
240240
}

sdk/metric/internal/aggregate/drop.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ func (r *dropRes[N]) Offer(context.Context, N, []attribute.KeyValue) {}
2222

2323
// Collect resets dest. No exemplars will ever be returned.
2424
func (r *dropRes[N]) Collect(dest *[]exemplar.Exemplar) {
25+
clear(*dest) // Erase elements to let GC collect objects
2526
*dest = (*dest)[:0]
2627
}

sdk/metric/internal/aggregate/exemplar.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var exemplarPool = sync.Pool{
1717
func collectExemplars[N int64 | float64](out *[]metricdata.Exemplar[N], f func(*[]exemplar.Exemplar)) {
1818
dest := exemplarPool.Get().(*[]exemplar.Exemplar)
1919
defer func() {
20+
clear(*dest) // Erase elements to let GC collect objects
2021
*dest = (*dest)[:0]
2122
exemplarPool.Put(dest)
2223
}()

sdk/metric/pipeline.go

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (p *pipeline) produce(ctx context.Context, rm *metricdata.ResourceMetrics)
115115
}
116116
if err := ctx.Err(); err != nil {
117117
rm.Resource = nil
118+
clear(rm.ScopeMetrics) // Erase elements to let GC collect objects
118119
rm.ScopeMetrics = rm.ScopeMetrics[:0]
119120
return err
120121
}
@@ -128,6 +129,7 @@ func (p *pipeline) produce(ctx context.Context, rm *metricdata.ResourceMetrics)
128129
if err := ctx.Err(); err != nil {
129130
// This means the context expired before we finished running callbacks.
130131
rm.Resource = nil
132+
clear(rm.ScopeMetrics) // Erase elements to let GC collect objects
131133
rm.ScopeMetrics = rm.ScopeMetrics[:0]
132134
return err
133135
}

sdk/trace/batch_span_processor.go

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func (bsp *batchSpanProcessor) exportSpans(ctx context.Context) error {
280280
//
281281
// It is up to the exporter to implement any type of retry logic if a batch is failing
282282
// to be exported, since it is specific to the protocol and backend being sent to.
283+
clear(bsp.batch) // Erase elements to let GC collect objects
283284
bsp.batch = bsp.batch[:0]
284285

285286
if err != nil {

sdk/trace/span.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,7 @@ func (s *recordingSpan) dedupeAttrsFromRecord(record map[attribute.Key]int) {
639639
record[a.Key] = len(unique) - 1
640640
}
641641
}
642-
// s.attributes have element types of attribute.KeyValue. These types are
643-
// not pointers and they themselves do not contain pointer fields,
644-
// therefore the duplicate values do not need to be zeroed for them to be
645-
// garbage collected.
642+
clear(s.attributes[len(unique):]) // Erase unneeded elements to let GC collect objects
646643
s.attributes = unique
647644
}
648645

0 commit comments

Comments
 (0)