Probably everyone ran into this problem at least once during development:
while(/*some condition here that somehow never will be false*/)
{
...
yourvector.push_back(new SomeType());
...
}
As you see the program starts to drain all system memory, your program hangs and your system starts to swap like crazy. If you don't recognize the problem fast enough and kill the process you probably get an unresponsive system in seconds where your mouse pointer don't even moving. You can either wait your program crash with "out of memory" error (which may take several long minutes) or hit the reset on your computer.
If you can't track down the bug immediately then you will need several tests and resets to find out which is very annoying...
I'm looking for a possibly cross-platform way to prevent this somehow. The best would be a debug mode code that exits the program if it allocated too much memory, but how can I keep track how much memory is allocated? Overriding the global new and delete operators won't help, because the free function I would invoke in the delete won't give any idea how many bytes are freed.
Any ideas appreciated.
If you're on a Linux or Unix-ish system, you could check into setrlimit(2) which allows you to configure resource limits for your program. You can do similar things from the shell with ulimit.
Overriding the global new and delete operators won't help, because the free function I would invoke in the delete won't give any idea how many bytes are freed.
But you can make it so. Here's a full framework for overloading the global memory operators (throw it in some global_memory.cpp
file):
namespace
{
// utility
std::new_handler get_new_handler(void)
{
std::new_handler handler = std::set_new_handler(0);
std::set_new_handler(handler);
return handler;
}
// custom allocation scheme goes here!
void* allocate(std::size_t pAmount)
{
}
void deallocate(void* pMemory)
{
}
// allocate with throw, properly
void* allocate_throw(std::size_t pAmount)
{
void* result = allocate(pAmount);
while (!result)
{
// call failure handler
std::new_handler handler = get_new_handler();
if (!handler)
{
throw std::bad_alloc();
}
handler();
// try again
result = allocate(pAmount);
}
return result;
}
}
void* operator new(std::size_t pAmount) throw(std::bad_alloc)
{
return allocate_throw(pAmount);
}
void *operator new[](std::size_t pAmount) throw(std::bad_alloc)
{
return allocate_throw(pAmount);
}
void *operator new(std::size_t pAmount, const std::nothrow_t&) throw()
{
return allocate(pAmount);
}
void *operator new[](std::size_t pAmount, const std::nothrow_t&) throw()
{
return allocate(pAmount);
}
void operator delete(void* pMemory) throw()
{
deallocate(pMemory);
}
void operator delete[](void* pMemory) throw()
{
deallocate(pMemory);
}
void operator delete(void* pMemory, const std::nothrow_t&) throw()
{
deallocate(pMemory);
}
void operator delete[](void* pMemory, const std::nothrow_t&) throw()
{
deallocate(pMemory);
}
Then you can do something like:
// custom allocation scheme goes here!
const std::size_t allocation_limit = 1073741824; // 1G
std::size_t totalAllocation = 0;
void* allocate(std::size_t pAmount)
{
// make sure we're within bounds
assert(totalAllocation + pAmount < allocation_limit);
// over allocate to store size
void* mem = std::malloc(pAmount + sizeof(std::size_t));
if (!mem)
return 0;
// track amount, return remainder
totalAllocation += pAmount;
*static_cast<std::size_t*>(mem) = pAmount;
return static_cast<char*>(mem) + sizeof(std::size_t);
}
void deallocate(void* pMemory)
{
// get original block
void* mem = static_cast<char*>(pMemory) - sizeof(std::size_t);
// track amount
std::size_t amount = *static_cast<std::size_t*>(mem);
totalAllocation -= pAmount;
// free
std::free(mem);
}
because the free function I would invoke in the delete won't give any idea how many bytes are freed
It can, you'll just have to keep a map of the size of allocated memory by address, and subtract the right amount based on that information during the free.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With