It goes up when you call testmap(), but does it go up again when you call testvec() ?
Using a process monitor to determine how much memory is in use can be misleading. When you delete memory in your program, it typically goes back into a pool of memory owned by the program to be re-used later, it is not returned to the OS.
If this is true, you will only see one jump in memory usage.
Allocating and freeing memory within the program is a matter of adjusting a few pointers (ie quick). Getting more memory from the OS usually involves a system call (slow), so the program run-time library works hard to avoid this as much as possible. Namely asking for large blocks, and not giving it back.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
you could write a custom allocator that tracks calls to allocate and deallocate.
#include <map>
template< typename T > struct myallocator : public std::allocator<T>
{
typedef std::allocator<T> base ;
typedef typename base::size_type size_type;
typedef typename base::difference_type difference_type;
typedef typename base::pointer pointer;
typedef typename base::const_pointer const_pointer;
typedef typename base::reference reference;
typedef typename base::const_reference const_reference;
typedef typename base::value_type value_type;
myallocator() throw() {}
myallocator( const myallocator& a ) throw() : base(a) {}
template <typename X> myallocator(
const myallocator<X>& ) throw() {}
~myallocator() throw() {}
template <typename X> struct rebind
{ typedef myallocator<X> other; };
pointer allocate( size_type sz, const void* hint = 0 )
{
// record alloc request eg. ++num_allocs ;
return base::allocate(sz,hint) ;
}
void deallocate( pointer p, size_type n )
{
// record dealloc request eg. --num_allocs ;
return base::deallocate(p,n) ;
}
};
template<typename T> inline bool operator==(
const myallocator<T>&, const myallocator<T>& )
{ return true; }
template<typename T> inline bool operator!=(
const myallocator<T>&, const myallocator<T>& )
{ return false; }
int main()
{
std::map< int, float, std::less<int>,
myallocator<int> > testmap;
for ( int i = 0; i < 1000000; i++ )
{
testmap[i] = (float)i;
}
testmap.clear();
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
> When I clear the map, the memory stays allocated to my program, but when I clear the vector it is returned to the OS immediately.
the vector allocation is for one large contiguous chunk of memory, the map makes many allocations of small chunks of memory. the allocator behaviour could be different in each case. (the behaviour varies depending on the implementation: microsoft's heap api on windows, dlmalloc used by gnu and either phkmalloc or jemalloc on bsd)
> Is there any way to tell it to give the memory back to the OS?
with the standard c++ library, implementing a custom allocator is the canonical way of specifying memory management policy. as the map has a large number of allocation and deallocation of small objects, a pool allocator could be used. one implementation is available in http://www.boost.org/libs/pool/doc/index.html
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
I found this:
Use std::vector().swap( c ) to free vector memory
it seems to work!
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Hey ggsddu,
Don't you think your reply to this thread came quite late?
(The thread is already dead for over a year)
Another suggestion: Daniweb offers you the ability to wrap your code between
code tags , make use of it, your code will be much easier to read that way :)
Edit:: You must have edited your post (added code tags) while I was writing mine.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243