diff --git a/storage/memory/memory.go b/storage/memory/memory.go index 500ec3e0..4af35c07 100644 --- a/storage/memory/memory.go +++ b/storage/memory/memory.go @@ -49,11 +49,18 @@ func (self *containerStorage) updatePrevStats(stats *info.ContainerStats) { } // make a deep copy. self.prevStats.Timestamp = stats.Timestamp + // copy the slice first. + percpuSlice := self.prevStats.Cpu.Usage.PerCpu *self.prevStats.Cpu = *stats.Cpu - self.prevStats.Cpu.Usage.PerCpu = make([]uint64, len(stats.Cpu.Usage.PerCpu)) - for i, perCpu := range stats.Cpu.Usage.PerCpu { - self.prevStats.Cpu.Usage.PerCpu[i] = perCpu + // If the old slice is enough to hold the new data, then don't allocate + // a new slice. + if len(percpuSlice) != len(stats.Cpu.Usage.PerCpu) { + percpuSlice = make([]uint64, len(stats.Cpu.Usage.PerCpu)) } + for i, perCpu := range stats.Cpu.Usage.PerCpu { + percpuSlice[i] = perCpu + } + self.prevStats.Cpu.Usage.PerCpu = percpuSlice *self.prevStats.Memory = *stats.Memory } diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index 87ae39f7..b27d9abf 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -44,6 +44,7 @@ func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStat stats.Cpu.Usage.Total = cpuTotalUsage stats.Cpu.Usage.User = stats.Cpu.Usage.Total stats.Cpu.Usage.System = 0 + stats.Cpu.Usage.PerCpu = []uint64{cpuUsage} stats.Memory.Usage = mem[i]