SpiderPig Posted June 28, 2018 Share Posted June 28, 2018 I create a collision patch for my terrain on the fly with the following code which increases the memory usage over time. Commenting out creating the shape seams to stop it. #include "App.h" using namespace Leadwerks; App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {} App::~App() { delete world; delete window; } Model* box; Camera* camera; bool wireframe = false; bool App::Start() { window = Window::Create("PolyMesh",50,50, 1024, 768); window->Show(); context = Context::Create(window); world = World::Create(); DirectionalLight* light = DirectionalLight::Create(); light->SetRotation(35, 35, 35); camera = Camera::Create(); box = Model::Box(); box->Move(0, 0, 2); return true; } bool App::Loop() { if (window->KeyHit(Key::Escape) == true) { return false; } if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); } if (window->KeyHit(Key::F2) == true) { if (wireframe == true) { camera->SetDrawMode(0); wireframe = false; } else { camera->SetDrawMode(2); wireframe = true; } } Vec3 rot = box->GetRotation(); rot.x += 0.2f; rot.y += 0.2f; rot.z += 0.2f; box->SetRotation(rot); //Make a collsion surface on the fly for terrain/////////////////// int xGrid = 3; int zGrid = 3; int vertIndex = 0; Surface* surface = Surface::Create(); for (int z = 0; z < zGrid; z++) { for (int x = 0; x < xGrid; x++) { surface->AddVertex(x, 0.0f, z); if (z != 0 && x != 0) { surface->AddTriangle(vertIndex, vertIndex - xGrid, vertIndex - 1); surface->AddTriangle(vertIndex - xGrid, vertIndex - xGrid - 1, vertIndex - 1); } vertIndex++; } } Shape* shape = Shape::PolyMesh(surface);//memory leak here? //box->SetShape(shape); shape->Release(); surface->Release(); ////////////////////////////////////////////////////////////////////// Time::Update(); world->Update(); world->Render(); context->SetBlendMode(Blend::Alpha); context->DrawStats(10.0f, 10.0f, true); context->Sync(true); return true; } Link to comment Share on other sites More sharing options...
macklebee Posted June 29, 2018 Share Posted June 29, 2018 Maybe try updating the surface after adding all the vertices/triangles and prior to using it in the PolyMesh()? Surface:Update() Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
SpiderPig Posted June 29, 2018 Author Share Posted June 29, 2018 Just tried it with no success. Also tried opting out of Betas but still nothing. Haven't tried 4.4 yet because there was a few errors to sort out first. Evan just create a simple box shape increases the memory used, albeit a bit slower. bool App::Loop() { if (window->KeyHit(Key::Escape) == true) { return false; } if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); } if (window->KeyHit(Key::F2) == true) { if (wireframe == true) { camera->SetDrawMode(0); wireframe = false; } else { camera->SetDrawMode(2); wireframe = true; } } Shape* shape = Shape::Box();//memeory leak here? shape->Release(); Time::Update(); world->Update(); world->Render(); context->SetBlendMode(Blend::Alpha); context->DrawStats(10.0f, 10.0f, true); context->Sync(true); return true; } Link to comment Share on other sites More sharing options...
macklebee Posted June 29, 2018 Share Posted June 29, 2018 So in your code, you are just constantly creating a shape every cycle of the loop? Seems like that would be something you would want to do in the start code not the loop? thats a lot of shapes being created every second... not to mention the number of surfaces you were making in the first example... Is this your actual code that you are having problems with or just something you are doing to show your problem? Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
SpiderPig Posted June 29, 2018 Author Share Posted June 29, 2018 This is the code to just show the problem, but I'm doing something pretty much the same in game. The only difference is, the code will only generate a shape if it's changed, like if the player has moved around on the terrain. I have my own large terrain class and generating all this data at the start will take enormous amounts of memory and time. (this was my first approach) Unless there is a way to edit the vertices of the shape itself then I'm stuck with deleting and remaking it every time it's needed. It's actually not as slow as you might think either, most of the patches I need form 4x4 grid. So as long as I don't try to create a thousand of them at once, there's no frame drop. 1 Link to comment Share on other sites More sharing options...
gamecreator Posted June 29, 2018 Share Posted June 29, 2018 This probably won't work but I'd try the garbage collection trick: https://www.leadwerks.com/community/topic/15212-improve-load-times-when-changing-maps/ Link to comment Share on other sites More sharing options...
SpiderPig Posted June 29, 2018 Author Share Posted June 29, 2018 Unfortunately it didn't work. But I did notice something interesting, it only happens in release mode. I've also noticed that in release mode the memory usually fluctuates a bit for about 20 seconds then stabilizes, but not in debug mode or at least for not as long... Maybe internally the release mode function is missing some garbage collection stage...? Link to comment Share on other sites More sharing options...
Josh Posted June 29, 2018 Share Posted June 29, 2018 Thank you for the code example. My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted June 29, 2018 Author Share Posted June 29, 2018 Most welcome. ? Link to comment Share on other sites More sharing options...
SpiderPig Posted June 30, 2018 Author Share Posted June 30, 2018 Just discovered this code does it too, but again only in release mode. Perhaps the releasing of any object is not working correctly? bool App::Loop() { if (window->KeyHit(Key::Escape) == true) { return false; } if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); } if (window->KeyHit(Key::F2) == true) { if (wireframe == true) { camera->SetDrawMode(0); wireframe = false; } else { camera->SetDrawMode(2); wireframe = true; } } Model* box = Model::Box(); box->Release(); //<- Increases memory used in Release mode Time::Update(); world->Update(); world->Render(); context->SetBlendMode(Blend::Alpha); context->DrawStats(10.0f, 10.0f, true); context->Sync(true); return true; } EDIT : Been watching the debug version of this for nearly 2 minutes and while Leadwerks says a constant memory usage, the diagnostics tools window of Visual Studio shows a steady increase. About 1MB a minute. 1 Link to comment Share on other sites More sharing options...
Josh Posted February 26, 2019 Share Posted February 26, 2019 I am seeing the same exact behavior. System::GetMemoryUsage() is reporting constant memory. But Task Manager and the VS debugger are showing a slow increase. Internally, the Leadwerks GetMemoryUsage() command uses _CrtMemCheckpoint on Windows. I wonder if this includes memory allocated by DLLs the program is using? My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted February 26, 2019 Share Posted February 26, 2019 Okay, I found the cause and fixed it. 1 My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts