diff --git a/CHANGELOG.md b/CHANGELOG.md index b0fa9fa..b0f7d5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +### Added +- `helper.FreeOSMemory()` to explicitly call the GC and release the memory to the OS + +### Changed +- added new calls to `runtime.GC()` in the low memory attachment processor + ## [2.1.2] 2020-12-01 ### Added - `SetKeyGenerationOffset` to add an offset in key generation time and prevent not-yet-valid keys. ### Changed - Improved canonicalization performance +- Reduced attachment memory allocation ## [2.1.1] 2020-11-16 ### Changed diff --git a/crypto/attachment.go b/crypto/attachment.go index 0de4f36..c3f1634 100644 --- a/crypto/attachment.go +++ b/crypto/attachment.go @@ -29,6 +29,9 @@ func (ap *AttachmentProcessor) Process(plainData []byte) { if _, err := (*ap.w).Write(plainData); err != nil { panic(err) } + if ap.garbageCollector > 0 { + defer runtime.GC() + } } // Finish closes the attachment and returns the encrypted data. @@ -36,19 +39,29 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) { if ap.err != nil { return nil, ap.err } + if err := (*ap.w).Close(); err != nil { return nil, errors.Wrap(err, "gopengpp: unable to close writer") } + if ap.garbageCollector > 0 { + ap.w = nil + runtime.GC() + } + if err := (*ap.pipe).Close(); err != nil { return nil, errors.Wrap(err, "gopengpp: unable to close pipe") } ap.done.Wait() + splitMsg := ap.split + if ap.garbageCollector > 0 { - runtime.GC() + ap.pipe = nil + ap.split = nil + defer runtime.GC() } - return ap.split, nil + return splitMsg, nil } // newAttachmentProcessor creates an AttachmentProcessor which can be used to encrypt diff --git a/helper/mobile.go b/helper/mobile.go index b72ccf0..b319682 100644 --- a/helper/mobile.go +++ b/helper/mobile.go @@ -3,6 +3,7 @@ package helper import ( "encoding/json" goerrors "errors" + "runtime/debug" "github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/pkg/errors" @@ -124,3 +125,10 @@ func EncryptSignBinaryDetachedMobile( EncryptedSignatureArmored: encryptedSignature, }, nil } + +// FreeOSMemory can be used to explicitly +// call the garbage collector and +// return the unused memory to the OS. +func FreeOSMemory() { + debug.FreeOSMemory() +}