Skip to content

Instantly share code, notes, and snippets.

View gkampitakis's full-sized avatar
🙉
Debugging

Georgios Kampitakis gkampitakis

🙉
Debugging
View GitHub Profile
# apt-get update && apt-get install -y curl host dnsutils nano netcat
apiVersion: v1
kind: Pod
metadata:
name: toolbox-pod
namespace: client
spec:
restartPolicy: Always
containers:
- name: toolbox
@gkampitakis
gkampitakis / time-ticker.go
Created February 18, 2024 14:20
Memory leaks in Go - Not stopping time.Ticker
func main() {
for {
ticker := time.NewTicker(1 * time.Second)
// do something with the ticker
_ = <-ticker.C
// stop the ticker to release associated resources
ticker.Stop()
}
}
@gkampitakis
gkampitakis / leaking-ticker-resources.go
Created February 18, 2024 14:19
Memory leaks in Go - Not stopping time.Ticker
func main() {
for {
ticker := time.NewTicker(1 * time.Second)
// do something with the ticker
_ = <-ticker.C
}
}
@gkampitakis
gkampitakis / for-loop-defers.go
Created February 18, 2024 14:16
Memory leaks in Go - Deferring function calls
func processManyFiles(files []string) error {
for _, file := range files {
err := process(file)
if err != nil {
return err
}
}
return nil
}
@gkampitakis
gkampitakis / for-loop-defers.go
Created February 18, 2024 14:14
Memory leaks in Go - Deferring function calls
func processManyFiles(files []string) error {
for _, file := range files {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
process(f)
}
@gkampitakis
gkampitakis / hanging-goroutines.go
Created February 18, 2024 14:07
Memory leaks in Go - Goroutines
func runJobs(cancel <-chan struct{}) {
for {
go func() {
// create a 1Gb slice
bigSlice := make([]byte, 1_000_000_000)
// do some work with the data
_ = bigSlice
// wait until a cancellation signal is received
@gkampitakis
gkampitakis / reslicing-reference.go
Created February 18, 2024 14:03
Memory leaks in Go - Long lived references
func readFileDetails(name string) []byte{
data, err := os.ReadFile(name)
if err != nil {
return err
}
return data[5:10]
}
// not holding reference to underlying memory anymore
@gkampitakis
gkampitakis / bounded-cache.go
Created February 18, 2024 13:59
Memory leaks in Go - Unbounded resource creation
var cache = map[int]int{}
func main() {
for i:=0; ; i++ {
// max items cache can hold is 1_000_000
if len(cache) >= 1_000_000 {
delete(cache, i-len(cache))
}
cache[i] = i
@gkampitakis
gkampitakis / unbounded-cache.go
Last active February 18, 2024 13:56
Memory leaks in Go - Unbounded resource creation
var cache = map[int]int{}
func main() {
// keep allocating memory indifinitely
for i:=0; ; i++ {
cache[i] = i
}
}
@gkampitakis
gkampitakis / main.go
Last active October 21, 2023 13:02
HTTP Connection churn in GO Demo
package main
import (
"fmt"
"io"
"log"
"net/http"
"sync/atomic"
"time"
)