Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular links between structs lead to massive memory consumption and a killed program #278

Open
SetorHabona opened this issue Aug 8, 2024 · 0 comments

Comments

@SetorHabona
Copy link

SetorHabona commented Aug 8, 2024

As the title says, using a struct schema which has circular links (as can happen using an ORM like GORM), the memory usage explodes and the program is killed. I have created a small repo with the minimal code to reproduce the issue. I have also attached some code directly below. https://github.com/SetorHabona/gocsv-mem-leak

NOTE: An easy fix is excluding the inner struct using csv:"-"

package main

import (
	"fmt"
	"os"

	"github.com/gocarina/gocsv"
)

// this setup is not not very unusual when working with gorm (maybe also other ORMs)
type Wrapper struct {
	Name string `csv:"name"`
	Age  uint   `csv:"age"`
	// Inner creates a problem, both as pointer and as value
	Inner *Inner // add `csv:"-"` to avoid rapid increase in consumed memory and your program being killed
}

type Inner struct {
	Position string
	Salary   uint
	Wrappers *Wrapper
}

func main() {
	file, err := os.OpenFile("./data.csv", os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	var wraps []Wrapper
	if err := gocsv.UnmarshalFile(file, &wraps); err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", wraps)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant