# 4.6.1 UInstancedStaticMeshComponent-\>RemoveInstance Performance Issues

**URL:** <https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 20, 2015, 8:08am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459 "2015-01-20T08:08:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mojobojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mojobojo](https://forums.unrealengine.com/u/mojobojo)\
**Post date:** [January 20, 2015, 8:08am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/1 "2015-01-20T08:08:08Z")

</div>

I am using RemoveInstance for a UInstancedStaticMeshComponent to remove something real time while my game is being played. But there is a big performance hit when RemoveInstance is called. I need to be able to change the game board real time while playing the game. Is there something else I should be using or is this a bug?

I recorded an example of what is happening.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/a/7/1a776e0112b626bc73378cca8a31e5f985981939.jpeg "2015 01 20 0312 38") ](https://www.youtube.com/watch?v=iamhbdIZg7c)

Note in this video there is another instance added in its place however I confirmed it was the RemoveInstance by removing the other code for testing. On top of that I call GetInstanceTransform and AddInstance in the function and they both don’t see the dip that happens with RemoveInstance.

---

<div class="post-metadata">

**Author:** ![mojobojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mojobojo](https://forums.unrealengine.com/u/mojobojo)\
**Post date:** [January 22, 2015, 7:15am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/2 "2015-01-22T07:15:01Z")

</div>

Just an update on my temporary solution to this. I decided to just move it to another location.

```
Instances->UpdateInstanceTransform(TileNum, MoveInstanceTo);

```

I have actually pinpointed where the issue lies I think. Its the deletion and allocation of all that memory instead of shifting the indexes.

[https://github.com/EpicGames/UnrealEngine/blob/4.6/Engine/Source/Runtime/Engine/Private/InstancedStaticMesh.cpp#L1912](https://github.com/EpicGames/UnrealEngine/blob/4.6/Engine/Source/Runtime/Engine/Private/InstancedStaticMesh.cpp#L1912)

```
		// TODO: it may be possible to instead just update the BodyInstanceIndex for all bodies after the removed instance. 
		ClearAllInstanceBodies();
		CreateAllInstanceBodies();
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_550b73f9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_550b73f9](https://forums.unrealengine.com/u/anonymous_user_550b73f9)\
**Post date:** [February 11, 2015, 5:42am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/3 "2015-02-11T05:42:00Z")

</div>

Hi Mojobojo,

The slow part here is not memory allocation but rather destruction and recreation of the PhysX objects (Instance Bodies) and adding them to the scene.

For 4.7 we’ve added HierarchicalInstancedStaticMeshComponent which handles large numbers of instances much more efficiently. As part of that change, RemoveInstance now removes items in place, moving an item from the end of the array into the removed item’s spot. It then recreates the PhysX objects only for a single instance.

I’d recommend trying out a 4.7 preview version and converting your class to use a HierarchicalInstancedStaticMeshComponent to see if that will improve things for you.

Cheers

---

<div class="post-metadata">

**Author:** ![mojobojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mojobojo](https://forums.unrealengine.com/u/mojobojo)\
**Post date:** [February 12, 2015, 2:22am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/4 "2015-02-12T02:22:36Z")

</div>

Do I include it just like the other component? When I include it in my 4.7 project I get compile errors.

[http://i.imgur.com/6naysb0.png](http://i.imgur.com/6naysb0.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_550b73f9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_550b73f9](https://forums.unrealengine.com/u/anonymous_user_550b73f9)\
**Post date:** [February 12, 2015, 3:21am UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/5 "2015-02-12T03:21:42Z")

</div>

You need to add

```
#include "Components/HierarchicalInstancedStaticMeshComponent.h"

```

at the top of your file.

-

---

<div class="post-metadata">

**Author:** ![mojobojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mojobojo](https://forums.unrealengine.com/u/mojobojo)\
**Post date:** [February 12, 2015, 1:41pm UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/6 "2015-02-12T13:41:47Z")

</div>

For the future, are all the components are/going to be in this directory or is it just some?

EDIT: Also I am getting a lot of crashes using this. I cant add an instance in the editor without it crashing on

> > UE4Editor-Engine.dll!FHierarchicalStaticMeshSceneProxy::FHierarchicalStaticMeshSceneProxy(UHierarchicalInstancedStaticMeshComponent \* InComponent, ERHIFeatureLevel::Type InFeatureLevel) Line 599 C++

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_550b73f9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_550b73f9](https://forums.unrealengine.com/u/anonymous_user_550b73f9)\
**Post date:** [February 12, 2015, 1:52pm UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/7 "2015-02-12T13:52:14Z")

</div>

I believe all components are going to be in the Components folder.

The main change is that we have reduced what we always automatically include, to reduce compiling time, so lesser-used components and include files need to be manually included.

---

<div class="post-metadata">

**Author:** ![Grogger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Grogger](https://forums.unrealengine.com/u/Grogger)\
**Post date:** [March 3, 2015, 4:40pm UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/8 "2015-03-03T16:40:01Z")

</div>

Sorry for bumping this, but I’m trying to use “HierarchicalInstancedStaticMeshComponent”, but I’m having trouble keeping track of the indices after using “RemoveInstance”

I made a post about the problem I’m having [here](https://answers.unrealengine.com/questions/181570/bug-uhierarchicalinstancedstaticmeshcomponent-can.html), but maybe you guys can help me out if you’ve already dealt with it.

How do you keep track of the instance indices after removing an instance? I hope I’m missing something obvious!

EDIT: I’m a little embarrassed to bump this again, I just wanted to say my problem has been resolved

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_550b73f9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_550b73f9](https://forums.unrealengine.com/u/anonymous_user_550b73f9)\
**Post date:** [March 3, 2015, 4:57pm UTC](https://forums.unrealengine.com/t/4-6-1-uinstancedstaticmeshcomponent-removeinstance-performance-issues/304459/9 "2015-03-03T16:57:24Z")

</div>

I posted in that thread. It swaps out the removed item with the last item in the array. It’s bit inconvenient but you should be able to keep track of the instance indices by using the same mechanism. That’s what the foliage tool does.
