From 1fcf6ea73f5a0412774197e405fa47ad8d5d6143 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Wed, 12 Apr 2017 16:17:07 +0200 Subject: [PATCH] add size hints to NativeImages to make node GC aware of the high amount of memory used by the instances --- atom/common/api/atom_api_native_image.cc | 18 ++++++++++++++---- atom/common/api/atom_api_native_image.h | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 6811ab919303..4e3a37f9e866 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -200,22 +200,29 @@ void Noop(char*, void*) { } // namespace NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image) - : image_(image) { - Init(isolate); + : image_(image), isolate_(isolate) { + Init(isolate_); + isolate_->AdjustAmountOfExternalAllocatedMemory( + image_.Width() * image_.Height() * 4); } #if defined(OS_WIN) NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path) - : hicon_path_(hicon_path) { + : hicon_path_(hicon_path), isolate_(isolate) { // Use the 256x256 icon as fallback icon. gfx::ImageSkia image_skia; ReadImageSkiaFromICO(&image_skia, GetHICON(256)); image_ = gfx::Image(image_skia); Init(isolate); + isolate_->AdjustAmountOfExternalAllocatedMemory( + image_.Width() * image_.Height() * 4); } #endif -NativeImage::~NativeImage() {} +NativeImage::~NativeImage() { + isolate_->AdjustAmountOfExternalAllocatedMemory( + - image_.Width() * image_.Height() * 4); +} #if defined(OS_WIN) HICON NativeImage::GetHICON(int size) { @@ -352,6 +359,9 @@ mate::Handle NativeImage::Resize( bool width_set = options.GetInteger("width", &width); bool height_set = options.GetInteger("height", &height); size.SetSize(width, height); + + isolate_->AdjustAmountOfExternalAllocatedMemory( + (width_set * height_set - width * height) * 4); if (width_set && !height_set) { // Scale height to preserve original aspect ratio diff --git a/atom/common/api/atom_api_native_image.h b/atom/common/api/atom_api_native_image.h index 6ff7a0e29f37..0c2890d86f5c 100644 --- a/atom/common/api/atom_api_native_image.h +++ b/atom/common/api/atom_api_native_image.h @@ -99,6 +99,7 @@ class NativeImage : public mate::Wrappable { #endif gfx::Image image_; + v8::Isolate* isolate_; DISALLOW_COPY_AND_ASSIGN(NativeImage); };