x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

memory leak when use UInstancedStaticMeshComponent

In Tick() function, I add FInstancedStaticMeshInstanceData to PerInstanceSMData,and ClearInstances in next Tick(),the memory cant be release. but if call ClearInstances () after adding,there is no problem.I need update PerInstanceSMData in tick,and clean them before set.any one knows how to solve it.

Product Version: UE 4.20
Tags:
more ▼

asked Nov 21 '18 at 09:31 AM in C++ Programming

avatar image

孤城太守
1 1 1

avatar image 孤城太守 Nov 21 '18 at 01:48 PM

seems like peroblem with materials. I test below. clear then add ISM to tarray PerInstanceSMData in Tick(),works fine without memory leak. .h

         UPROPERTY(EditAnywhere, Category = "test")
         AActor * myObj;
 
 public:
     UInstancedStaticMeshComponent * mISMC;


.cpp

 void AMyActorForISMTest::BeginPlay()
 {
     Super::BeginPlay();
     
     UWorld* World = myObj->GetWorld();
     UClass * muClass = myObj->GetClass();
 
     AActor* actorPtr = World->SpawnActor<AActor>(muClass, FVector(0, 0, 0), FVector(0, 0, 0).Rotation());
     actorPtr->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepWorld, false));
     UStaticMeshComponent* mCube = actorPtr->FindComponentByClass<UStaticMeshComponent>();
     mCube->SetMobility(EComponentMobility::Static);
     UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(mCube->GetMaterial(0), NULL);
     mCube->SetMaterial(0, material);
     mISMC = actorPtr->FindComponentByClass<UInstancedStaticMeshComponent>();
 
 }
 
 // Called every frame
 void AMyActorForISMTest::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
     mISMC->ClearInstances();
 
     for (size_t i = 0; i < 100000; i++)
     {
         FInstancedStaticMeshInstanceData insData;
         insData.Transform = FTransform(FVector(i, 0, 0)).ToMatrixWithScale();
         mISMC->PerInstanceSMData.Add(insData); 
     }
 
 }
 

set AActor * myObj

alt text

alt text

setmyobj.png (3.4 kB)
pawnactor.png (4.4 kB)
(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Here is the problem code: .h // Fill out your copyright notice in the Description page of Project Settings.

 #pragma once
 
 #include "CoreMinimal.h"
 #include "GameFramework/Actor.h"
 #include "Components/InstancedStaticMeshComponent.h"
 #include "MyActorForISMTest.generated.h"
 
 UCLASS()
 class ISMTEST_API AMyActorForISMTest : public AActor
 {
     GENERATED_BODY()
 
         UPROPERTY(EditAnywhere, Category = "test")
         AActor * myObj;
 
 public: 
     TMap<size_t, UInstancedStaticMeshComponent *> mapForISMC; 
     TArray <size_t> arrayHaveColor; 
     int totalNum;
 public:    
     // Sets default values for this actor's properties
     AMyActorForISMTest();
 
 protected:
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
 
 public:    
     // Called every frame
     virtual void Tick(float DeltaTime) override;
 
     
     
 };

.cpp

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "MyActorForISMTest.h"
 
 
 // Sets default values
 AMyActorForISMTest::AMyActorForISMTest()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     totalNum = 3000;
 
 }
 
 // Called when the game starts or when spawned
 void AMyActorForISMTest::BeginPlay()
 {
     Super::BeginPlay();
     
     UWorld* World = myObj->GetWorld();
     UClass * muClass = myObj->GetClass();
 
     for (size_t i = 0; i < totalNum; i++)
     { 
         AActor* actorPtr = World->SpawnActor<AActor>(muClass, FVector(0, 0, 0), FVector(0, 0, 0).Rotation());
         actorPtr->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepWorld, false)); 
         actorPtr->SetOwner(this);
         UStaticMeshComponent* mCube = actorPtr->FindComponentByClass<UStaticMeshComponent>();
         mCube->SetMobility(EComponentMobility::Static); 
         UMaterialInstanceDynamic* mMaterial = UMaterialInstanceDynamic::Create(mCube->GetMaterial(0), NULL); 
         mCube->SetMaterial(0, mMaterial); 
         mapForISMC.Add(i, actorPtr->FindComponentByClass<UInstancedStaticMeshComponent>()); 
     } 
 }
 
 // Called every frame
 void AMyActorForISMTest::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
     arrayHaveColor.Empty();
     for (size_t i = 0; i < totalNum; i++)
     {
         mapForISMC[i]->ClearInstances();
     }
 
     for (size_t i = 0; i < totalNum; i++)
     { 
         arrayHaveColor.Add(i);
     }
      
     for (size_t i = 0; i < totalNum; i++)
     {
         size_t tempIndex = arrayHaveColor[i];
         FInstancedStaticMeshInstanceData insData;  
         mapForISMC[i]->PerInstanceSMData.Add(insData);     
     }  
 }
 

That will cause memory leak.Because too many InstancedStaticMeshComponent need update in Tick function.After I put all insData in one InstancedStaticMeshComponent,problem solved.But the reason detail still unclear for me.

more ▼

answered Nov 27 '18 at 02:11 AM

avatar image

孤城太守
1 1 1

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question