Help! How to release memory?

The code is realy simple:

  1. Create custom data source.
  2. Add 10000 labels.
  3. Remove labels
  4. Result remove from cesium-map, but not from memory, forever!

Code:

var labelDataSource = new Cesium.CustomDataSource(‘my-labels’);

viewer.dataSources.add(labelDataSource);

for( let i = 0; i <10000;i++){
labelDataSource.entities.add({
id: baruch${i} ,
label: {
text: ‘hello world’,
},
position: Cesium.Cartesian3.fronDegrees(35.09 + i * 0.001, 32.835 + i * 0.001, 0),
});

}

Till here is the code.

Now after awhile, remove (use a button for example) the labels:

labelDataSource.entities.removeAll();

Now, do the next steps:

  1. Open devtools.
  2. Navigate to ‘memory’ tab.
  3. Make a snapshot for the heap.
  4. Search in the ‘classifier’ (near the summery) the string : ‘label’
  5. And you will see 10000 labels!!!
    6.open in ‘retainers object’ one of the labels
    You will see:

labelDataSource => _entityCluster => _labelCollection => _spareBillboards.

In summary, removeAll function not remove from memory.

What to do?!?!?

Thanks in advance.

Baruch

I found a function called: Cesium.destroyObject(…).

I dont know how to use it currectly, because i still see the bug in Memory-profiles.

I’m just a user, so I can’t speak for the team (@omar is usually around somewhere?) but I know Cesium does a lot of optimization to avoid new allocations and re-use existing objects where possible. I assume it’s hanging on to the destroyed billboard/label objects so that it can reuse them the next time you make new labels – if you make labels, then remove them, then make new ones, remove again, etc etc, I would expect your memory footprint to remain about the same.

There might be a method that tells Cesium to clean up the “scratch” objects it’s hanging on to, but I don’t know of anything personally.

2 Likes

Thanks James!

The only thing I’d add here is I think there may be a special case for labels, where CesiumJS will hold onto the memory created because in the general case it’s a lot faster than destroying and recreating those resources.

After some investigation, I think this is what you’re running into:

For labels, one billboard is used per character to visualize the label. Once the labels are removed, we keep these billboards around to be reused later when you create another label, because reusing a billboard has better performance than always creating new ones. However, I do agree it doesn’t make sense to keep around such a large number of these objects around indefinitely.

From: Problem with drawing primitives collections of labels, billboards and points, memory leak? · Issue #7184 · CesiumGS/cesium · GitHub

2 Likes

@omar @James_B Thank you very much!