-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory_test.go
More file actions
42 lines (34 loc) · 998 Bytes
/
memory_test.go
File metadata and controls
42 lines (34 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package zlog
import (
"runtime"
"testing"
)
func TestNoLargeMemoryAllocation(t *testing.T) {
// Get initial memory stats
var m1 runtime.MemStats
runtime.ReadMemStats(&m1)
// Create multiple UltimateLogger instances
for i := 0; i < 10; i++ {
logger := NewUltimateLogger()
logger.SetWriter(DiscardWriter())
logger.Info("test")
}
// Force GC to clean up any temporary allocations
runtime.GC()
// Get final memory stats
var m2 runtime.MemStats
runtime.ReadMemStats(&m2)
// Calculate memory growth (handle potential underflow)
var growth int64
if m2.Alloc >= m1.Alloc {
growth = int64(m2.Alloc - m1.Alloc)
} else {
// Memory was freed, growth is negative
growth = -int64(m1.Alloc - m2.Alloc)
}
// Should be much less than 64MB (allow some KB for normal allocations)
if growth > 1024*1024 { // 1MB threshold
t.Errorf("Memory grew by %d bytes, expected much less than 64MB", growth)
}
t.Logf("Memory growth: %d bytes (%.2f KB)", growth, float64(growth)/1024)
}