Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that blob: URLs will be revoked when pages are cleaned-up/destroyed (JPEG memory usage) #10644

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
} from '../shared/util';
import {
deprecated, DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer,
loadScript, PageViewport, RenderingCancelledException, StatTimer
loadScript, PageViewport, releaseImageResources, RenderingCancelledException,
StatTimer
} from './display_utils';
import { FontFaceObject, FontLoader } from './font_loader';
import { apiCompatibilityParams } from './api_compatibility';
Expand Down Expand Up @@ -1988,11 +1989,14 @@ class WorkerTransport {
resolve(img);
};
img.onerror = function() {
reject(new Error('Error during JPEG image loading'));
// Note that when the browser image loading/decoding fails,
// we'll fallback to the built-in PDF.js JPEG decoder; see
// `PartialEvaluator.buildPaintImageXObject` in the
// `src/core/evaluator.js` file.
reject(new Error('Error during JPEG image loading'));

// Always remember to release the image data if errors occurred.
releaseImageResources(img);
};
img.src = imageData;
}).then((img) => {
Expand Down Expand Up @@ -2095,6 +2099,8 @@ class WorkerTransport {
}
resolve({ data: buf, width, height, });

// Immediately release the image data once decoding has finished.
releaseImageResources(img);
// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
tmpCanvas.width = 0;
Expand All @@ -2104,6 +2110,9 @@ class WorkerTransport {
};
img.onerror = function() {
reject(new Error('JpegDecode failed to load image'));

// Always remember to release the image data if errors occurred.
releaseImageResources(img);
};
img.src = imageUrl;
});
Expand Down Expand Up @@ -2323,6 +2332,14 @@ class PDFObjects {
}

clear() {
for (const objId in this._objs) {
const { data, } = this._objs[objId];

if (typeof Image !== 'undefined' && data instanceof Image) {
// Always release the image data when clearing out the cached objects.
releaseImageResources(data);
}
}
this._objs = Object.create(null);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ function deprecated(details) {
console.log('Deprecated API usage: ' + details);
}

function releaseImageResources(img) {
assert(img instanceof Image, 'Invalid `img` parameter.');

const url = img.src;
if (typeof url === 'string' && url.startsWith('blob:') &&
URL.revokeObjectURL) {
URL.revokeObjectURL(url);
}
img.removeAttribute('src');
}

export {
PageViewport,
RenderingCancelledException,
Expand All @@ -496,4 +507,5 @@ export {
isValidFetchUrl,
loadScript,
deprecated,
releaseImageResources,
};