这是indexloc提供的服务,不要输入任何密码
Skip to content

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

@SetorHabona

Description

@SetorHabona

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions