Open frittentheke opened 3 months ago
I did a bit of memory profiling. The memory usage peak is probably mostly caused by https://github.com/inovex/CalendarSync/blob/ccdebc534f875ae0c3a6aa2fd3ee185a553e3ea8/internal/auth/storage_encryption.go#L38 . At least on my machine that allocate a whopping 256MB of memory.
The higher memory usage with the non-release builds is probably caused by https://github.com/inovex/CalendarSync/pull/195 .
Bluemonday doesn't use particularly much memory. And as the transformers are called sequentially, this allows go to run GC during the transformations if necessary. https://github.com/inovex/CalendarSync/pull/197 should reduce the peak memory usage by running a GC after the storage decryption is completed. That should at least limit the memory peak to around 300MB.
Btw, I've used the following patch for memory profiling (run go mod tidy
to get the library)
diff --git a/cmd/calendarsync/main.go b/cmd/calendarsync/main.go
index 3cb4ac6..de2b3cc 100644
--- a/cmd/calendarsync/main.go
+++ b/cmd/calendarsync/main.go
@@ -10,6 +10,7 @@ import (
"github.com/inovex/CalendarSync/internal/models"
"github.com/charmbracelet/log"
+ "github.com/pkg/profile"
"github.com/urfave/cli/v2"
"github.com/inovex/CalendarSync/internal/adapter"
@@ -103,6 +104,8 @@ func main() {
}
func Run(c *cli.Context) error {
+ defer profile.Start(profile.MemProfile, profile.MemProfileRate(1)).Stop()
+
if c.Bool(flagVersion) {
fmt.Println("Version:", Version)
os.Exit(0)
@@ -211,5 +214,8 @@ func Run(c *cli.Context) error {
log.Fatalf("we had some errors during synchronization:\n%v", err)
}
}
+
+ // this looks weird, but without it the profile was incomplete
+ runtime.GC()
+ time.Sleep(1 * time.Second)
return nil
}
I looked a little closer at the output of systemd and noticed the large amount of memory CalendarSync apparently uses: It is fluctuating around 300 to < 600MB with the current 0.10.0 release.
During validation of https://github.com/inovex/CalendarSync/pull/177 I noticed an even bigger memory footprint, especially for the first run:
CalendarSync.service: Consumed 10.940s CPU time, 1.5G memory peak.
But later runs appear to be using more memory than before: destination):
CalendarSync.service: Consumed 3.885s CPU time, 954.7M memory peak.
This is without any changes to the source (or updates being pushed to the sink).
Maybe the cause therefore lies somewhere in the use of the https://github.com/microcosm-cc/bluemonday sanitizer? There was a change allowing bluemonday to stream its output to a writer directly: https://github.com/microcosm-cc/bluemonday/pull/110. Maybe that would help some?
Let me now if you want me to run some pprof or to provide any other debug info.
^^ @alxndr13 FYI