Archive 17/01/2023.

How to release resource of Node? Memory monitor graph went up steadily

ab4daa

Sorry for the basic question… but I cannot figure out.

In 1.7 release source code sample 20_HugeObjectCount, I add following code in HugeObjectCount::HandleUpdate() to remove and add nodes every 3 seconds at runtime.
My environment is win7+vs 2017 community +x64

{//test
	static float accTime = 0.0f;
	accTime += timeStep;
	if (accTime >= 3.0f)
	{
		accTime = 0.0f;
		ResourceCache* cache = GetSubsystem<ResourceCache>();
		if (!useGroups_)
		{
			/*remove boxes*/
			while( boxNodes_.Size())
			{
				SharedPtr<Node> n = boxNodes_.Back();
				boxNodes_.Pop();
				n->Remove();
			}

			/*add back boxes*/
			// Create individual box StaticModels in the scene
			for (int y = -125; y < 125; ++y)
			{
				for (int x = -125; x < 125; ++x)
				{
					Node* boxNode = scene_->CreateChild("Box");
					boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f));
					boxNode->SetScale(0.25f);
					StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
					boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
					boxNodes_.Push(SharedPtr<Node>(boxNode));
				}
			}
		}
		else
		{
			/*remove boxes*/
			const Vector<SharedPtr<Node> >& boxgrp = scene_->GetChildren();
			for (unsigned ii = 0; ii < boxgrp.Size(); ii++)
			{
				StaticModelGroup * lastGroup = boxgrp[ii]->GetComponent<StaticModelGroup>();
				if (lastGroup != NULL)
					lastGroup->RemoveAllInstanceNodes();
			}
			while( boxNodes_.Size())
			{
				SharedPtr<Node> n = boxNodes_.Back();
				n->Remove();
				boxNodes_.Pop();
			}

			/*add back boxes*/
			unsigned ii = 0;
			StaticModelGroup * lastGroup = boxgrp[ii]->GetComponent<StaticModelGroup>();
			for (int y = -125; y < 125; ++y)
			{
				for (int x = -125; x < 125; ++x)
				{
					if (lastGroup == 0 || lastGroup->GetNumInstanceNodes() >= 25 * 25)
					{
						for (ii = ii + 1;; ii++)
						{
							StaticModelGroup * next = boxgrp[ii]->GetComponent<StaticModelGroup>();
							if (next != NULL)
							{
								lastGroup = next;
								break;
							}
						}
					}
					Node* boxNode = scene_->CreateChild("Box");
					boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f));
					boxNode->SetScale(0.25f);
					boxNodes_.Push(SharedPtr<Node>(boxNode));
					lastGroup->AddInstanceNode(boxNode);
				}
			}
		}
	}
}

Basically it uses node->remove() to remove node.
But I saw memory usage monitor in visual studio 2017 just went larger and larger no matter use of staticModelGroup or not.
mem_usage
Do I write something wrong?
Thanks!

Eugene

Memory monitor doesn’t show how much memory your application use. It only shows how much memory the OS allocates for your application. It may allocate whole RAM, actually, I never care.

If you want to confirm memory leak, crash your app or use leak detection tools.

ab4daa

Thanks, I feel a burden released.:grinning: