Jump to content

STL memory leaks?


Josh
 Share

Recommended Posts

If I declare a list, map, or stringstream in a function, and call that function, my memory usage goes up when the function ends and the stl object goes out of scope. Does STL use a funny memory pool or something?

 

This is all it takes:

void MyFunc()
{
   std::list<int> list;
}

mem1=System::GetMemoryUsage();
MyFunc();
mem2=System::GetMemoryUsage();

And my memory usage increases by 12 bytes.

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

STL containers work kinda like BASIC: they make sure the zero element exists, is initialized. That's why they take up initial memory, but it's totally meaningless because you actually want to populate the container, and if not, then don't declare it.

 

Where STL really comes to play is in the speed. Even an ineffective iteration through a vector is faster than an native Fortran linked list. I don't know how they do it, but it's insane fast.

 

Windows cleans up all leaked memory, but it doesn't do it immediately. That's why it's kinda "OK" to not call delete, but just let windows clean the memory up. I mean in the sense that your game reserves the memory what it needs, so there is no harm done to the game itself.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Josh as you declared your std::list on the stack it should.. no must.. be cleaned up after leaving the scope.

Are you sure your System::GetMemoryUsage() works correctly ?

 

To verify it, please check your Ressourcemonitor on the RAM Tab.

 

**edit**

 

608cf04afd.png

Link to comment
Share on other sites

Gah, I was measuring total allocations. wacko.png

 

This is the correct code:

	long System::GetMemoryUsage()
{
#ifdef _WIN32
#ifdef _DEBUG
	//Exact memory usage, but only works in debug mode:
	_CrtMemState memstate;
	_CrtMemCheckpoint(&memstate);
	return memstate.lSizes[0]+memstate.lSizes[1]+memstate.lSizes[2]+memstate.lSizes[3]+memstate.lSizes[4];
#else
	//Only gives approximate results, but works in release mode:
	long sz=0;
	HANDLE proc = GetCurrentProcess();
	PPROCESS_MEMORY_COUNTERS ppsmemCounters=new PROCESS_MEMORY_COUNTERS;
	if (GetProcessMemoryInfo(proc,ppsmemCounters,sizeof(PROCESS_MEMORY_COUNTERS)))
	{
		sz=ppsmemCounters->WorkingSetSize
	}
	delete ppsmemCounters;
	return sz;
   #endif
#endif
}

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

I love how the developer asks the community questions, and gets help, It really is a good way for us to give back to the hard working developer, and strengthens the community. This is the first time I have ever seen this, this community is the tightest one I have ever seen.

Windows 7 Professional 64 bit, 16 gigs ram, 3.30GHz Quad Core, GeForce GTX 460 one gig, Leadwerks 2.5, Blender 2.62, Photoshop CS3, UU3D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...