add size hints to NativeImages to make node GC aware of the high amount of memory used by the instances

This commit is contained in:
Heilig Benedek 2017-04-12 16:17:07 +02:00
parent 89277dda90
commit 1fcf6ea73f
2 changed files with 15 additions and 4 deletions

View file

@ -200,22 +200,29 @@ void Noop(char*, void*) {
} // namespace } // namespace
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image) NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
: image_(image) { : image_(image), isolate_(isolate) {
Init(isolate); Init(isolate_);
isolate_->AdjustAmountOfExternalAllocatedMemory(
image_.Width() * image_.Height() * 4);
} }
#if defined(OS_WIN) #if defined(OS_WIN)
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path) 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. // Use the 256x256 icon as fallback icon.
gfx::ImageSkia image_skia; gfx::ImageSkia image_skia;
ReadImageSkiaFromICO(&image_skia, GetHICON(256)); ReadImageSkiaFromICO(&image_skia, GetHICON(256));
image_ = gfx::Image(image_skia); image_ = gfx::Image(image_skia);
Init(isolate); Init(isolate);
isolate_->AdjustAmountOfExternalAllocatedMemory(
image_.Width() * image_.Height() * 4);
} }
#endif #endif
NativeImage::~NativeImage() {} NativeImage::~NativeImage() {
isolate_->AdjustAmountOfExternalAllocatedMemory(
- image_.Width() * image_.Height() * 4);
}
#if defined(OS_WIN) #if defined(OS_WIN)
HICON NativeImage::GetHICON(int size) { HICON NativeImage::GetHICON(int size) {
@ -353,6 +360,9 @@ mate::Handle<NativeImage> NativeImage::Resize(
bool height_set = options.GetInteger("height", &height); bool height_set = options.GetInteger("height", &height);
size.SetSize(width, height); size.SetSize(width, height);
isolate_->AdjustAmountOfExternalAllocatedMemory(
(width_set * height_set - width * height) * 4);
if (width_set && !height_set) { if (width_set && !height_set) {
// Scale height to preserve original aspect ratio // Scale height to preserve original aspect ratio
size.set_height(width); size.set_height(width);

View file

@ -99,6 +99,7 @@ class NativeImage : public mate::Wrappable<NativeImage> {
#endif #endif
gfx::Image image_; gfx::Image image_;
v8::Isolate* isolate_;
DISALLOW_COPY_AND_ASSIGN(NativeImage); DISALLOW_COPY_AND_ASSIGN(NativeImage);
}; };