InsertShape function causes a memory leak
1 view (last 30 days)
Show older comments
My current project requires drawing map data (.shp files) of buildings and roads to export images. The function works as desired however, upon running the memory usage of matlab increases and stays increased after the function has complete (clearing workspace values does not reduce this).
The main method is here
shapeFile = shaperead("shapeFiles/London.shp");
shapeFileR = shaperead("shapeFiles/LondonRoads.shp");
%get total bounding box for all buildings
totalBox = getBoundingBox(shapeFile);
boxSize = 1e3; %1km
avgLat = mean(totalBox(3:4));
avgLong = mean(totalBox(1,2));
%km's in lat and long.
longDist = CoorToDist(totalBox(1),totalBox(2),avgLat,avgLat);
latDist = CoorToDist(avgLong,avgLong,totalBox(3),totalBox(4));
% percentage of image roughly equal to 1km
kmToLat = 1/(latDist/1e3);
kmToLong = 1/(longDist/1e3);
boxScale = [kmToLong,kmToLat,kmToLong,kmToLat];
xSize = 1000;
ySize = 1000;
%Select road/buildings inside the desired bounding box.
boundingBox = [3,3,1,1].*boxScale;
polygons = GenerateMap(boundingBox,shapeFile,1000,[xSize,ySize],totalBox);
polygonsR = GenerateMap(boundingBox,shapeFileR,200,[xSize,ySize],totalBox);
%Create blank image and draw roads and buildings
img = ones(xSize,ySize,3);
img = insertShape(img,'FilledPolygon',polygons,'Opacity',1);
img = insertShape(img,'Line',polygonsR,'LineWidth',3,'Opacity',1);
imshow(img);
Running this multiple times will eventually cause matlab to crash (~20runs, 16GB RAM). Commenting out the insertShape lines fixes the memory leak.
- Is there anything that can be changed with the code to fix this?
- Is there another method to use similar to the insertShape function?
If you wish to run the code the shape files can be found here: https://gofile.io/d/dO67a3
1 Comment
Gaszton
on 23 Mar 2021
I have noticed the same problem.
Was making videos of cell movement tracking, overlaying their trajectories, Matlab even crashed after running up to 44GB memory useage.
I attached a minimal example to reproduce. Run the script and watch with the task manager the memory usage. The taken memory is not freed until matlab is closed. (clear all does nothing)
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!