Windows Memory Usage & Games
How does windows handle memory usage, exactly? I'm not looking for an in-depth discussion here... just a general idea... because apparently, I have it all wrong!
I am working on resetting my game (taking it from game-mode, to menu-mode). For this transition, I am freeing up all of my resources (sprites, models/meshes, textures, pointsprites, vertex buffers, enemies, etc. etc. etc.) used by the single-player portion of the game. THEN, I load up the menu resources again.
When I then choose to start a new game (second time this run), I noticed in Windows Task Manager my mem usage DOUBLED. It went from about 200,000k to about 400,000k :x
So what gives? When I perform that game->menu transition I am calling delete on all of my dynamically allocated resources... and believe me, *everything* is dynamically allocated!
Does windows not get the memory I am freeing back immediately? Does it wait until game-exit to reacquire what I have used? If so, how should I go about freeing and "regetting" my resources during this transition?
-dbh
That is weird, If I am understanding correctly the following is happening:
1. Go to main menu (X Memory used)
2. Enter game (Y Memory used)
3. Go back to main menu (X Memory used again)
4. Enter game again as in step 2 (2*Y memory used)
If this is correct then it is likely that you are doing something wrong in your code, or that some library that you are using is managing its own memory in a special way.
AFAIK this has nothing to do with Windows, and has more to do with either your code or the libraries you are using. Some libraries have their own memory manager, and when you release resources for those libraries, the custom memory manager does not give back the resources to the OS. Note also that if you are working on managed code then you have very little control over the memory used by the runtime.
I am sure you can get more help here, but it would be beneficial if you give more details about the scenario (which language are you using, libraries, etc, etc)
1. Go to main menu (X Memory used)
2. Enter game (Y Memory used)
3. Go back to main menu (X Memory used again)
4. Enter game again as in step 2 (2*Y memory used)
If this is correct then it is likely that you are doing something wrong in your code, or that some library that you are using is managing its own memory in a special way.
AFAIK this has nothing to do with Windows, and has more to do with either your code or the libraries you are using. Some libraries have their own memory manager, and when you release resources for those libraries, the custom memory manager does not give back the resources to the OS. Note also that if you are working on managed code then you have very little control over the memory used by the runtime.
I am sure you can get more help here, but it would be beneficial if you give more details about the scenario (which language are you using, libraries, etc, etc)
Ideej,
The scenario you are described is accurate.
I am using C++ (unmanaged) with Microsoft Visual Studio .NET 2003 and DirectX9.0c April 2005 SDK.
In regard to memory allocation, I am talking specifically about .x files (which I am using for meshes-- each of these has one material (.png)) and sprites (also .pngs) which are created with a wrapper class around both a LPD3DXSPRITE and a LPDIRECT3DTEXTURE9.
The wrapper classes I made for my .x files and sprite/texture combinations are being newed and deleted upon creation/deletion, so I know that memory is fine.
I am looking into those wrappers now to see if maybe they aren't properly releasing the DX resources...
Anything else I should check?
-dbh
The scenario you are described is accurate.
I am using C++ (unmanaged) with Microsoft Visual Studio .NET 2003 and DirectX9.0c April 2005 SDK.
In regard to memory allocation, I am talking specifically about .x files (which I am using for meshes-- each of these has one material (.png)) and sprites (also .pngs) which are created with a wrapper class around both a LPD3DXSPRITE and a LPDIRECT3DTEXTURE9.
The wrapper classes I made for my .x files and sprite/texture combinations are being newed and deleted upon creation/deletion, so I know that memory is fine.
I am looking into those wrappers now to see if maybe they aren't properly releasing the DX resources...
Anything else I should check?
-dbh
Also, if Windows Task Manager is a poor memory usage monitor, what should I be using? I first started checking my memory footprint when I noticed that step4 that Ideej described above (second run of the game) was extremely slow!
-dbh
-dbh
I'm using the following code to load and release all of my models (wrapper class for .x meshes w/texture materials):
This code releases the above upon model deletion:
I double-checked, and all of my sprites (wrapper for LPD3DXTEXTURE9 and D3DXSPRITE) are released the same way as well.
Those are the only DirectX resources I can think of my game uses that need to be removed during this transition.
Am I doing something wrong?
-dbh
// Load the mesh and check for errors: hResult = D3DXLoadMeshFromX(filename.c_str(), D3DXMESH_SYSTEMMEM, device, NULL, &m_matBuffer, NULL, &m_numMaterials, &m_mesh); if (FAILED(hResult)) return false; D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)m_matBuffer->GetBufferPointer(); // Create two arrays, one for materials and one for textures: m_pMeshMaterials = new D3DMATERIAL9[m_numMaterials]; m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_numMaterials]; // Copy each material and texture into the buffer: for (DWORD i = 0; i < m_numMaterials; ++i) { // Copy the material: m_pMeshMaterials = matMaterials.MatD3D; // Set the ambient color for the material: m_pMeshMaterials.Ambient = m_pMeshMaterials.Diffuse; // Make sure the texture name is valid: if (matMaterials.pTextureFilename != NULL) { // Load the texture: // -- append models\ directory in front: std::string texture = matMaterials.pTextureFilename; std::string textureFinal = "models\\" + texture; hResult = D3DXCreateTextureFromFile(device, textureFinal.c_str(), &m_pMeshTextures); if (FAILED(hResult)) return false; device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); device->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 16); } else { // There is no texture name, NULL this spot in the list: m_pMeshTextures = NULL; } } // Release the material buffer: if (m_matBuffer) m_matBuffer->Release();
This code releases the above upon model deletion:
// Release the mesh: if (m_mesh) m_mesh->Release(); // Release the materials: if (m_pMeshMaterials) delete [] m_pMeshMaterials; // Release the textures: if (m_pMeshTextures) delete [] m_pMeshTextures;
I double-checked, and all of my sprites (wrapper for LPD3DXTEXTURE9 and D3DXSPRITE) are released the same way as well.
Those are the only DirectX resources I can think of my game uses that need to be removed during this transition.
Am I doing something wrong?
-dbh
Even MORE info from the wall of weird...
I setup NVPerfHud on my second rig (it has a GF5900 Ultra, my primary dev platform is running an ATi 9800 Pro). Its graph displaying video memory starts around 10MB at the menu, then jumps to around 100 or so when the game is in full swing. After reloading the menu and restarting the game-- the video memory NEVER goes down, mind you-- the video memory usage jumps up to around 200 MB.
So again, I'm oblivious. Shouldn't I be getting those resources back? Shouldn't the video card be cleaning out its memory once I delete and release all of the resources I am using?
-dbh
I setup NVPerfHud on my second rig (it has a GF5900 Ultra, my primary dev platform is running an ATi 9800 Pro). Its graph displaying video memory starts around 10MB at the menu, then jumps to around 100 or so when the game is in full swing. After reloading the menu and restarting the game-- the video memory NEVER goes down, mind you-- the video memory usage jumps up to around 200 MB.
So again, I'm oblivious. Shouldn't I be getting those resources back? Shouldn't the video card be cleaning out its memory once I delete and release all of the resources I am using?
-dbh
You are not releasing your texture memory, you are only releasing the array.
Your code:
// Release the textures:
if (m_pMeshTextures)
delete [] m_pMeshTextures;
What it should be (Pseudocode):
foreach(texture in textures)
texture->Release();
delete [] textures;
Your code:
// Release the textures:
if (m_pMeshTextures)
delete [] m_pMeshTextures;
What it should be (Pseudocode):
foreach(texture in textures)
texture->Release();
delete [] textures;
ack!
Of course! Bah, just a commonly misprogrammed issue? :\
Thanks for pointing that out; I'll go fix that and double-check all other shutdown functions and let you know what that does to my memory footprint!
-dbh
Of course! Bah, just a commonly misprogrammed issue? :\
Thanks for pointing that out; I'll go fix that and double-check all other shutdown functions and let you know what that does to my memory footprint!
-dbh
Wow. I can't believe it! I fixed that ONE problem area, and now the memory footprint is *exactly* how it should look. Runs at 49,000k at menu, 200,000k in-game and then back to 49,000k at menu again.
Wow.
Thanks again, Ideej, you just saved me days (literally) of frustration! I went back to the book I used months and months ago to get my DX framework up and running, and it does the exact same thing I was doing to release meshes with textures/materials.
I was shocked. I really should write their editor and let them know they goofed... that's a big, bad mistake!
Anyway, thanks again!!!
-dbh
Wow.
Thanks again, Ideej, you just saved me days (literally) of frustration! I went back to the book I used months and months ago to get my DX framework up and running, and it does the exact same thing I was doing to release meshes with textures/materials.
I was shocked. I really should write their editor and let them know they goofed... that's a big, bad mistake!
Anyway, thanks again!!!
-dbh
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement