Weird behaviour of Hierarchial Instance static mesh,while adding instance from c++ code

Hello,
I am facing a really weird behaviour when i add instance from c++ code.
Due to some reason it consumes a lot of RAM,when i add it from code.
But,when i create an actor from blueprint and do the same thing in blueprint it hardly takes any memory.

Here is my c++ code.

void AMyActor::PostEditChangeProperty(FPropertyChangedEvent & PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);

	const auto PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;

	if (PropertyName == GET_MEMBER_NAME_CHECKED(AMyActor, Spawn))
	{
		SpawnInstance();
	}
}

void AMyActor::SpawnInstance()
{
	FTransform Transform;
	Transform.SetTranslation(FVector::ZeroVector);
	const FVector Increment{ 150.0f,0.0f,0.0f };

	for (int32 Item = 0; Item < 10000; Item++)
	{
		Transform.AddToTranslation(Increment);

		HierarchialInstance->AddInstance(Transform);
	}

	TotalInstance = HierarchialInstance->GetInstanceCount();
}

Here is an image,which shows my RAM consumption,when i run cpp version.

My blueprint version of code looks like this.

RAM consumption,when i run blueprint version of the code.

As,you can see RAM consumption is high,when i run the cpp version.

Am i doing something wrong in cpp version?

Please help.

Thank you.

PostEditChangeProperty(FPropertyChangedEvent & PropertyChangedEvent)
gets triggered whenever you make changes in details panel in editor.

Here,i am using a boolean variable named Spawn to trigger SpawnInstance() function.

[link text][1]

OnPropertyChanged or similiar, does it exist? - C++ - Epic Developer Community Forums

It seems like this problem was there even in older versions of the engine.
Here is a link to the forum post from 2015,where they are facing same kind of problem.

[InstancedStaticMeshComponent->AddInstance Performance][1]

I hope someone from Epic notices this answerhub post and guide us in right direction,if we are doing something wrong here.

InstancedStaticMeshComponent->AddInstance Performance - C++ - Epic Developer Community Forums

the BP version is a construction script, but the C++ is not:
how do you trigger AMyActor::SpawnInstance() then? it could be the reason.

I tried similar setup few weeks ago, and this was reproduced for me either. blueprint version works faster and handle more instances easily, while cpp version takes a lot of time to update instances.
I tried to nativize bp version to check generated cpp code, but I didn’t find any specific variables/functions there.
the difference was that cpp used ‘for loops’, and bp used ‘do while’. but I don’t think this is the root cause.

I just tested it with BeginPlay() method in c++ and this problem does not occurs there.
I added 100K instances in Beginplay() method and my RAM consumption was not even close to want happens in editor when i spawn just 10K instances.

Here’s an image showing my RAM consumption in game.

OK,i don’t what’s happening,but,if i just add HISM component and do no coding at all in cpp Actor class and then add instance in blueprint construction script the problem still occurs.

At first,i thought,it might be related to my code but,i think,something might be happening in editor when i create HISM component in cpp and add instance inside the editor with blueprint or inside cpp method.

I think this is a bug which occurs only in editor when i create HISM component in cpp.

I forgot to mention before,the memory consumption is high only when i select the actor with HISM component created in cpp.
If i deselect the actor,everything goes back to normal.

Here is my test project that i used with both blueprint version and cpp version of the code.

Thank you.