Source of memory increase
Opened this issue · 0 comments
I've written a program that reads a csv (2.1m records) and builds sql insert statements into mariadb instance. It takes about 7minutes to complete the cpu level on my macbook pro (sierra) go to about 65% and the activity monitor shows upwards of 1GB to even 3.7 GB sometimes. I added the profiler to my project and tried to analyze the results but it seems to show that the program used about 25MB. I'm not sure what to read from this. here is a contrived version of my code and some pictures of the activity monitor.
func main(){
//Setup and Connect to database
//Set variables
//Read CSV File
dbConnect()
defer db.Close()
db.DB().SetMaxOpenConns(90)
db.DB().SetMaxIdleConns(10)
db.DB().SetConnMaxLifetime(time.Second * 14400)
filehandle, err := os.Open(physicianCSV)
checkErr(err)
defer filehandle.Close()
reader := csv.NewReader(filehandle)
_, err = reader.Read()
checkErr(err)
for i := 0; i <= readLimit; i++ {
record, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
physician = convertCSVRecordToPhysician(record)
//..Do stuff with physician, edit object properties via pointer
physicians = append(physicians, physician)
if math.Mod(float64(i), float64(bulkAmount)) == 0 && i != 0 {
fmt.Println(i, "Records: From ", i-bulkAmount, "to", i)
wg.Add(1)
sliceOfPhys := make([]Physician, bulkAmount)
copy(sliceOfPhys, physicians)
go bulkSavePhysicians(sliceOfPhys)
physicians = physicians[:0]
}
}
fmt.Println(readLimit, "records inserted")
wg.Wait()
}
func bulkSavePhysicians(_physicians []Physician) {
defer func() {
if x := recover(); x != nil {
fmt.Println(x)
}
}()
defer wg.Done()
sqlStringArray := buildSQLStatements(_physicians)
batchSQL := fmt.Sprintf("insert into physicians values %s ;", strings.Join(sqlStringArray, ","))
tx := db.Begin()
errors := tx.Exec(batchSQL).GetErrors()
if len(errors) > 0 {
panic(errors)
}
tx.Commit()
}
func buildSQLStatements(_physicians []Physician) []string {
var valueStr string
var valueArr []string
for _, phys := range _physicians {
valueStr = fmt.Sprintf(`( "%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s" )`, phys.NPI,
phys.PACID,
phys.ProfessionalEnrollmentID,
strings.Replace(phys.LastName, "'", "\\'", -1),
phys.FirstName,
phys.MiddleName,
phys.Suffix,
phys.Gender,
phys.Credential,
strings.Replace(phys.MedicalSchoolName, "'", "\\'", -1),
phys.GraduationYear,
phys.PrimarySpecialty,
phys.SecondarySpecialty1,
phys.SecondarySpecialty2,
phys.SecondarySpecialty3,
phys.SecondarySpecialty4,
phys.AllSecondarySpecialties,
strings.Replace(phys.OrganizationLegalName, "'", "\\'", -1),
phys.GroupPracticePACID,
phys.NumberOfGroupPracticeMembers,
strings.Replace(phys.Line1StreetAddress, "'", "\\'", -1),
phys.Line2StreetAddress,
phys.MarkerOfAddressLine2Suppression,
phys.City,
phys.State,
phys.ZipCode,
phys.PhoneNumber,
phys.HospitalAffiliationCCN1,
phys.HospitalAffiliationLBN1,
phys.HospitalAffiliationCCN2,
phys.HospitalAffiliationLBN2,
phys.HospitalAffiliationCCN3,
phys.HospitalAffiliationLBN3,
phys.HospitalAffiliationCCN4,
phys.HospitalAffiliationLBN4,
phys.HospitalAffiliationCCN5,
phys.HospitalAffiliationLBN5,
phys.ProfessionalAcceptsMedicareAssignment,
phys.ReportedQualityMeasures,
phys.UsedElectronicHealthRecords,
phys.ParticipatedInTheMedicareMaintenance,
phys.CommittedToHeartHealth,
phys.SpecialtyID)
valueArr = append(valueArr, valueStr)
}
return valueArr
}
Here is zip of mem.pprof and cpu.pprof
proffs.zip
An image of activity monitor taken without the profiler inclusion.
I watched your talk and you mentioned that heapSys is the correct value to indicate the amount of memory used in bytes. Am I correct in saying this?
203751424 bytes = 203MB
Calling web from go tool pprof mem.pprof renders the following
I'm confused as to which values to trust.