Skip to content

Commit

Permalink
Merge pull request #1486 from tradingview/Total_canvas_memory_use_exc…
Browse files Browse the repository at this point in the history
…eeds_the_maximum_limit_fix

Total canvas memory use exceeds the maximum limit fix
  • Loading branch information
SlicedSilver authored Jan 2, 2024
2 parents 5fa808f + 6af2a54 commit 61d45db
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ module.exports = [
{
name: 'CJS',
path: 'dist/lightweight-charts.production.cjs',
limit: '47.82 KB',
limit: '47.87 KB',
},
{
name: 'ESM',
path: 'dist/lightweight-charts.production.mjs',
limit: '47.76 KB',
limit: '47.8 KB',
},
{
name: 'Standalone-ESM',
path: 'dist/lightweight-charts.standalone.production.mjs',
limit: '49.47 KB',
limit: '49.52 KB',
},
{
name: 'Standalone',
path: 'dist/lightweight-charts.standalone.production.js',
limit: '49.52 KB',
limit: '49.57 KB',
},
];
10 changes: 10 additions & 0 deletions src/gui/canvas-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ export function createBoundCanvas(parentElement: HTMLElement, size: Size): Canva
binding.resizeCanvasElement(size);
return binding;
}

export function releaseCanvas(canvas: HTMLCanvasElement): void {
// This function fixes the iOS Safari error "Total canvas memory use exceeds the maximum limit".
// Seems that iOS Safari stores canvas elements for some additional time internally.
// So if we create/destroy a lot of canvas elements in a short period of time we can get this error.
// We resize the canvas to 1x1 pixels to force it to release memmory resources.
canvas.width = 1;
canvas.height = 1;
canvas.getContext('2d')?.clearRect(0, 0, 1, 1);
}
4 changes: 3 additions & 1 deletion src/gui/pane-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { TouchMouseEventData } from '../model/touch-mouse-event-data';
import { IPaneRenderer } from '../renderers/ipane-renderer';
import { IPaneView } from '../views/pane/ipane-view';

import { createBoundCanvas } from './canvas-utils';
import { createBoundCanvas, releaseCanvas } from './canvas-utils';
import { IChartWidgetBase } from './chart-widget';
import { drawBackground, drawForeground, DrawFunction, drawSourcePaneViews } from './draw-functions';
import { IPaneViewsGetter } from './ipane-view-getter';
Expand Down Expand Up @@ -152,9 +152,11 @@ export class PaneWidget implements IDestroyable, MouseEventHandlers {
}

this._topCanvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._topCanvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._topCanvasBinding.canvasElement);
this._topCanvasBinding.dispose();

this._canvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._canvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._canvasBinding.canvasElement);
this._canvasBinding.dispose();

if (this._state !== null) {
Expand Down
3 changes: 2 additions & 1 deletion src/gui/price-axis-stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ChartOptionsBase } from '../model/chart-model';
import { InvalidationLevel } from '../model/invalidate-mask';
import { PriceAxisRendererOptionsProvider } from '../renderers/price-axis-renderer-options-provider';

import { createBoundCanvas } from './canvas-utils';
import { createBoundCanvas, releaseCanvas } from './canvas-utils';
import { PriceAxisWidgetSide } from './price-axis-widget';

export interface PriceAxisStubParams {
Expand Down Expand Up @@ -64,6 +64,7 @@ export class PriceAxisStub implements IDestroyable {

public destroy(): void {
this._canvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._canvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._canvasBinding.canvasElement);
this._canvasBinding.dispose();
}

Expand Down
4 changes: 3 additions & 1 deletion src/gui/price-axis-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { PriceAxisRendererOptionsProvider } from '../renderers/price-axis-render
import { IAxisView } from '../views/pane/iaxis-view';
import { IPriceAxisView } from '../views/price-axis/iprice-axis-view';

import { createBoundCanvas } from './canvas-utils';
import { createBoundCanvas, releaseCanvas } from './canvas-utils';
import { IPriceAxisViewsGetter } from './iaxis-view-getters';
import { suggestPriceScaleWidth } from './internal-layout-sizes-hints';
import { MouseEventHandler, MouseEventHandlers, TouchMouseEvent } from './mouse-event-handler';
Expand Down Expand Up @@ -155,9 +155,11 @@ export class PriceAxisWidget implements IDestroyable {
this._mouseEventHandler.destroy();

this._topCanvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._topCanvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._topCanvasBinding.canvasElement);
this._topCanvasBinding.dispose();

this._canvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._canvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._canvasBinding.canvasElement);
this._canvasBinding.dispose();

if (this._priceScale !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/gui/time-axis-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { IPaneRenderer } from '../renderers/ipane-renderer';
import { TimeAxisViewRendererOptions } from '../renderers/itime-axis-view-renderer';
import { IAxisView } from '../views/pane/iaxis-view';

import { createBoundCanvas } from './canvas-utils';
import { createBoundCanvas, releaseCanvas } from './canvas-utils';
import { ChartWidget } from './chart-widget';
import { drawBackground, drawForeground, drawSourcePaneViews } from './draw-functions';
import { ITimeAxisViewsGetter } from './iaxis-view-getters';
Expand Down Expand Up @@ -139,9 +139,11 @@ export class TimeAxisWidget<HorzScaleItem> implements MouseEventHandlers, IDestr
}

this._topCanvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._topCanvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._topCanvasBinding.canvasElement);
this._topCanvasBinding.dispose();

this._canvasBinding.unsubscribeSuggestedBitmapSizeChanged(this._canvasSuggestedBitmapSizeChangedHandler);
releaseCanvas(this._canvasBinding.canvasElement);
this._canvasBinding.dispose();
}

Expand Down

0 comments on commit 61d45db

Please sign in to comment.