Kitware / vtk-js

Visualization Toolkit for the Web
https://kitware.github.io/vtk-js/
BSD 3-Clause "New" or "Revised" License
1.23k stars 371 forks source link

Huge memory leak for lines in vtkPolyData #2737

Closed ghost closed 1 year ago

ghost commented 1 year ago

High-level description

When a vtkPolyData object is manually initialized with line segments and used for rendering, it seems to consume a very large memory space so that not many lines can be supported in the scene.

Steps to reproduce

We use the following code to fill the line segments to the vtkPolyData class. createOnePolyline(polydata, linecolor, vertcoords) { const nv = vertcoords.length / 3; const nlines = nv - 1; const cellarray = new Uint32Array(3 nlines); for (let i = 0; i < nlines; i++) { const j = i 3; cellarray[j] = 2; cellarray[j+1] = i; cellarray[j+2] = i+1; } polydata.getPoints().setData(vertcoords, 3); polydata.getLines().setData(cellarray); }

Alternatively, we also tried a polyline approach, but the result is the same. createOnePolyline(polydata, linecolor, vertcoords) { const nv = vertcoords.length / 3; const cellarray = new Uint32Array(nv+1); cellarray[0] = nv; for (let i = 0; i < nv; i++) { cellarray[i+1] = i; } polydata.getPoints().setData(vertcoords, 3); polydata.getLines().setData(cellarray); }

Detailed behavior

I loaded a customized CAD dataset with 18k triangles and 3210 line segments using vtk.js. Several vtkPolyData objects are created and rendered. When inspecting the javascript heap size, Chrome shows that it takes over 180MB. If I don't fill the vtkPolyData with line segments, the heap memory is at a pretty normal size of 8MB. If I add only the line segments, the heap size consumption remains at about 170M. So, I think there is a memory leak issue in the code for handling line segments.

Our CAD models may contain a very large amount of line segments, so this is a serious problem preventing us from using vtk.js. For example, when loading a dataset with 16M triangles and 8M line segments, this problem caused a failure during loading due to the exhausted memory space. When I disabled loading of the lines, our loader can actually load all the surface models.

We also have a similar problem using vtk wasm. The line elements seem to consume a lot of memory, too. When lines are enabled, we cannot load the above mentioned dataset with memory errors printed in the rendering classes.

Expected behavior

A much smaller memory footprint is expected.

Environment

vtkjs_memorybug

daker commented 1 year ago

Thanks for your report, if you can provide a codesandbox demo that demonstrate the issue it will be good for someone to start debugging.

jourdain commented 1 year ago

Thanks for reporting such issue. We'll try to look into that in the coming days.

ghost commented 1 year ago

Well, I double checked the dataset. It seems to be a dataset issue. Although there are only 3210 line segments, they are treated as 642 polylines, where each polyline contains 6 vertices. For each polyline, it corresponds to a polydata, a mapper, and an actor. Then, the total memory consumption of 180MB might be understandable, although still a bit large. Daker, do you still want to continue to investigate it?

jourdain commented 1 year ago

Are you saying that the main issue is related to the fact that your dataset get actually split across many actors/mapper/polydata rather than a single polydata?

ghost commented 1 year ago

Yes. I've thought that there should be just several polylines. On the contrary, the data is somewhat exported in a strange way. There are 642 polylines so that 642 polydatas/mappers and actors created.

jourdain commented 1 year ago

I'm guessing you can not share your data to test/validate?

ghost commented 1 year ago

Now I think it is mostly a dataset issue. Please close it.

jourdain commented 1 year ago

Ok thanks for the update