mapbox / geojson-vt

Slice GeoJSON into vector tiles on the fly in the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak

aparzi opened this issue · comments

I have integrated the library with openlayers but there seems to be a memory leak when I add the layer to the map.
Can someone help me?

Can you be more specific? Why do you think there's a leak? Can you provide any details / evidence of this? Are you sure the leak is on the geojson-vt side and not OpenLayers or other app code? Have you done any memory profiling of the problem?

Hi @mourner,
In a function (renderOnMap) I have the following code:
const` tileIndex = geojsonvt(json, {
extent: 4096,
debug: 1,
});
const format = new GeoJSON({
// Data returned from geojson-vt is in tile pixel units
dataProjection: new Projection({
code: 'TILE_PIXELS',
units: 'tile-pixels',
extent: [0, 0, 4096, 4096],
}),
});
const vectorSource = new VectorTileSource({
tileUrlFunction: function (tileCoord) {
// Use the tile coordinate as a pseudo URL for caching purposes
return JSON.stringify(tileCoord);
},
tileLoadFunction: function (tile, url) {
const tileCoord = JSON.parse(url);
const data = tileIndex.getTile(
tileCoord[0],
tileCoord[1],
tileCoord[2]
);
const geojson = JSON.stringify(
{
type: 'FeatureCollection',
features: data ? data.features : [],
},
replacer
);
const features = format.readFeatures(geojson, {
extent: vectorSource.getTileGrid().getTileCoordExtent(tileCoord),
featureProjection: map.getView().getProjection(),
});
tile.setFeatures(features);
},
});
layer.setSource(vectorSource);
openlayers_service.map.addLayer(layer);

my geojson contains 50K figures and the memory rises a bit when I run this function, once the execution is finished the memory doesn't go back to the way it was before, indeed if I run the function several times, it continues to increase. I noticed that by setting tileIndex as undefined the memory goes back to the way it was before. This function can be performed multiple times by the user, using a simple checkbox

I noticed that by setting tileIndex as undefined the memory goes back to the way it was before.

If that's the case, this is not a memory leak by definition. GeoJSON-VT index allocates a certain amount of memory for processed data, and it's released when the index is no longer used. This is working as designed — I don't see any way this could be a problem on the projection side.

specifically when the index is no longer used?
consider that the renderToMap function is called from an angular html component from a checkbox

@mourner can i use the library by loading a geojson without file? I have a local method which builds a geojson format

I found my problem. My figures have a great deal of properties. Do you have an idea how to fix it?

@mourner
the properties of my figures (45K) are very complex, specifically they are keys with arrays, objects and native types. removing the properties it seems that the problem does not arise

@mourner any idea?

The properties key, for each feature, contains an array of 50 elements plus other keys. By setting the empty properties the memory does not increase. what do you think about it?

@aparzi sorry, I don't have any pointers for your app aside from simplifying your GeoJSON properties schema to the bare minimum, preferably a flat one without nested structures (arrays or objects).