473,763 Members | 7,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wrapping std::vector<> to track memory usage?


Hi!

I need to be able to track memory usage in a medium-sized
application I'm developing. The only significant (memory-wise) non-
local objects are of two types -- std::vector<and of a custom class
simple_vector<t hat is a hand-rolled substitute for array<>. With the
latter I have code that tracks all allocations and destructions, so I
can account for all the memory.

The question is about std::vector<-- how can I track memory usage
by individual std::vector's? I'm thinking along the lines of a wrapper
(templated) class, like std::tracked_ve ctor<which would have the
original std::vector<as a private member, delegate relevant
operations to the underlying std::vector, while doing the accounting
job behind the scenes.

Are there any particular pitfalls to such a design? Would it suffice
to delegate only the methods I actually use,
like .size(), .reserve(), .at(), [] operators, etc. or would I need to
delegate all possible methods? I fear that since I use the
std::vector's in stl algorithms and other stl containers (vectors of
vectors), I might need to delegate all the iterator stuff and other
methods I don't use directly, is this the case? How do I account for
vectors of vectors, so that I don't bill the same memory twice?

Surely, someone must have gone that route, are there any particular
do's and don'ts there? Is it feasible? I don't want to reinvent the
wheel.

TIA,
- J.
Sep 30 '08 #1
8 3619
On Sep 30, 3:00*pm, jacek.dzied...@ gmail.com wrote:
* I need to be able to track memory usage in a medium-sized
application I'm developing. The only significant (memory-wise) non-
local objects are of two types -- std::vector<and of a custom class
simple_vector<t hat is a hand-rolled substitute for array<>. With the
latter I have code that tracks all allocations and destructions, so I
can account for all the memory.

* The question is about std::vector<-- how can I track memory usage
by individual std::vector's? I'm thinking along the lines of a wrapper
(templated) class, like std::tracked_ve ctor<which would have the
original std::vector<as a private member, delegate relevant
operations to the underlying std::vector, while doing the accounting
job behind the scenes.
[]

You could use a custom allocator that would maintain a counter of how
much memory has been allocated. Something like that:

#include <vector>
#include <iostream>

size_t allocated;

void print_allocated (int n)
{
std::cout << n << ": " << allocated << '\n';
}

template<class T>
struct counted_allocat or : std::allocator< T>
{
template<class U>
struct rebind { typedef counted_allocat or<Uother; };

typedef std::allocator< Tbase;

typedef typename base::pointer pointer;
typedef typename base::size_type size_type;

pointer allocate(size_t ype n)
{
allocated += n * sizeof(T);
return this->base::allocate (n);
}

pointer allocate(size_t ype n, void const* hint)
{
allocated += n * sizeof(T);
return this->base::allocate (n, hint);
}

void deallocate(poin ter p, size_type n)
{
allocated -= n * sizeof(T);
this->base::dealloca te(p, n);
}
};

int main()
{
typedef std::vector<int , counted_allocat or<int IntVec;

print_allocated (0);
{
IntVec v;
v.resize(1000);
print_allocated (1);
v.resize(2000);
print_allocated (2);
IntVec u = v;
print_allocated (3);
}
print_allocated (4);
}
Output:
0: 0
1: 4000
2: 8000
3: 16000
4: 0

--
Max
Sep 30 '08 #2
ja************@ gmail.com wrote:
Would it suffice to delegate only the methods I actually use,
like .size(), .reserve(), .at(), [] operators, etc. or would I need to
delegate all possible methods?
std::vector itself obviously doesn't require anything about your
delegating functions. You can implement those functions which you need
and leave the rest. (Of course you will find that you will have to keep
adding delegating functions as you start using vector functions in your
code which you weren't using before. But as long as you have access to
the wrapper class, it shouldn't be a huge problem.)

Note, however, that by using this technique you will only be able to
track the amount of space requested from std::vector *explicitly*.
There's no way of knowing how much memory the std::vector is *really*
allocating behind the scenes. Also, even if you were able to do that, it
wouldn't help you knowing the real amount of RAM used. All allocations
have a certain overhead to them, and especially std::vector easily
causes memory fragmentation when it grows, and you might end up having a
significant amount of unused memory which is nevertheless allocated from
the system because of memory fragmentation. In the worst case scenario
the real memory usage of your program (ie. what your program requests
the OS to allocate for it) might be even over double the amount of
memory that you are *explicitly* allocating (and tracking).

Explicit memory allocation tracking is a lot less useful than one
might think, at least if what you are trying to do is to estimate how
much RAM your program is consuming. You will only get a very rough lower
limit, while the actual memory usage may be much larger (even
significantly larger in the worst cases).
Sep 30 '08 #3
On Sep 30, 5:29*pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
You could use a custom allocator that would maintain a counter of how
much memory has been allocated. Something like that:
[helpful code snipped]
Thanks a lot, you've been very helpful, I'll look into that!

- J.
Oct 1 '08 #4
On Sep 30, 6:51*pm, Juha Nieminen <nos...@thanks. invalidwrote:
* Note, however, that by using this technique you will only be able to
track the amount of space requested from std::vector *explicitly*.
There's no way of knowing how much memory the std::vector is *really*
allocating behind the scenes.
What would std::vector<be allocating "behind the scenes"?
Are you talking about the capacity-margin that usually causes
reallocations
to consume geometrically larger amounts of memory each time, to
satisfy
asymptotic performance requirements? Because apart from that I hope
that a typical implementation will not consume more than several
tens of bytes for any bookkeeping.
Also, even if you were able to do that, it
wouldn't help you knowing the real amount of RAM used. All allocations
have a certain overhead to them, and especially std::vector easily
causes memory fragmentation when it grows, and you might end up having a
significant amount of unused memory which is nevertheless allocated from
the system because of memory fragmentation. In the worst case scenario
the real memory usage of your program (ie. what your program requests
the OS to allocate for it) might be even over double the amount of
memory that you are *explicitly* allocating (and tracking).
Yes, I realize that. Using OS-based approaches (the 'top' command)
I can see how much memory is really used up. I'm mostly attempting
to track potential memory leaks, I'll be satisfied with the values
the program "would use if there were no fragmentation, overhead
and VM issues". This also to calculate how memory requirements change
wrt some parameters like system size.
* Explicit memory allocation tracking is a lot less useful than one
might think, at least if what you are trying to do is to estimate how
much RAM your program is consuming. You will only get a very rough lower
limit, while the actual memory usage may be much larger (even
significantly larger in the worst cases).
Right.

Thanks for the helpful answer. Are there any drawbacks to the
custom-allocator approach that Maxim Yegorushkin suggested?
Looks like a lot less work to do than creating a wrapper. It
seems to me that it would take care of the vector of vectors'
issue, as in not booking the same memory twice.

thanks,
- J.
Oct 1 '08 #5
ja************@ gmail.com wrote:
On Sep 30, 6:51 pm, Juha Nieminen <nos...@thanks. invalidwrote:
> Note, however, that by using this technique you will only be able to
track the amount of space requested from std::vector *explicitly*.
There's no way of knowing how much memory the std::vector is *really*
allocating behind the scenes.

What would std::vector<be allocating "behind the scenes"?
When you perform a "vec.push_back( value);" you might track that the
size of the vector increased by sizeof(value), when in fact it may well
be that the size of the vector increased by a lot more.
I'm mostly attempting to track potential memory leaks
Aren't there tools to do exactly that, such as valgrind?
Thanks for the helpful answer. Are there any drawbacks to the
custom-allocator approach that Maxim Yegorushkin suggested?
It's probably the easiest way to track the amount of memory explicitly
allocated in the program. Of course you would have to make sure that
everything is indeed using that allocator.
Oct 1 '08 #6
On Oct 1, 4:39*pm, Juha Nieminen <nos...@thanks. invalidwrote:
I'm mostly attempting to track potential memory leaks

* Aren't there tools to do exactly that, such as valgrind?
Yes, there are three difficulties I'm experiencing with valgrind:
- not all platforms are supported (Itanium!),
- valid code sometimes crashes valgrind's "virtual machine",
especially hand-tuned, vendor-specific code that does weird
trickery to squeeze the last cycles out of every pipeline,
- can only be used for debugging, production code that begins
to leak after a month of uptime cannot be feasibly "simulated"
on valgrind.
* It's probably the easiest way to track the amount of memory explicitly
allocated in the program. Of course you would have to make sure that
everything is indeed using that allocator.
OK. Thank you!

cheers,
- J.
Oct 1 '08 #7
Juha Nieminen wrote:
ja************@ gmail.com wrote:
>On Sep 30, 6:51 pm, Juha Nieminen <nos...@thanks. invalidwrote:
>> Note, however, that by using this technique you will only be able to
track the amount of space requested from std::vector *explicitly*.
There's no way of knowing how much memory the std::vector is *really*
allocating behind the scenes.
What would std::vector<be allocating "behind the scenes"?

When you perform a "vec.push_back( value);" you might track that the
size of the vector increased by sizeof(value), when in fact it may well
be that the size of the vector increased by a lot more.
The capacity, not the size.

Schobi
Oct 1 '08 #8
In article <b6************ *************** *******@34g2000 hsh.googlegroup s.com>,
<ja************ @gmail.comwrote :
>On Oct 1, 4:39*pm, Juha Nieminen <nos...@thanks. invalidwrote:
I'm mostly attempting to track potential memory leaks

* Aren't there tools to do exactly that, such as valgrind?

Yes, there are three difficulties I'm experiencing with valgrind:
- not all platforms are supported (Itanium!),
Could you not compile your code on x86 and test it using valgrind?
Seems to work for me although it does make things more difficult.
>- valid code sometimes crashes valgrind's "virtual machine",
especially hand-tuned, vendor-specific code that does weird
trickery to squeeze the last cycles out of every pipeline,
Hopefully, this code is only a very small percentage of your code
base. The location where hand tuning was required had been identified
using a profiler (e.g. valgrind callgrind tool). So valgrind is still
very useful to test the other 99% of your codebase.

If you need to run through the whole system and some of the hand
optimised code make valgrind crash, you hopefully could revert back to
the original not-hand-optimised code which you hopefully have
originally written. Hopefully, at some point, you had a version of
the codebase with simple easy switching between the "obvious and
maintainable" code and the "hand optimised unreadable hack" code, so
that you could measure if decreasing the maintainability of your code
base was worth it by giving you any actual perfomrance gain.
>- can only be used for debugging, production code that begins
to leak after a month of uptime cannot be feasibly "simulated"
on valgrind.
That is probably because the tests that were run through valgrind
did not cover the code path that is being exercised by the production
code.

Possible solutions:
Expand your unit tests.
Verify your unit tests with a code coverage tool.
Try to figure out what code path is exercised in the deployed system
(through logs, input analysis, head scratching, head banging against a
brick wall, strace...) and then create a targetted test and run the test
through valgrind.
Oct 6 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7079
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
2
4774
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
11
1928
by: Lynn | last post by:
Hi, Is it better to use std::vector <type> with a pointer to the types being stored or the actual storage for the types ? In other words, is std::vector <mytype> or std::vector <mytype *> better ? I know that there are specific cases where it is dictated but in the general case, what do you think ? Thanks, Lynn McGuire
20
17833
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
8
5372
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it possible to "spoof" std::vector into implementing a "true" vector of bool rather than the specialisation? Say I do: typedef bool boolreally; std::vector<booleallybvec;
4
20112
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
1
2399
by: sergio311 | last post by:
Hi all, I'm new of c++ and i'm writing a class that create a matrix of data (of T type, it's a template class). The member variable m_data must stores the matrix's data and i prefer use heap so, i need to allocate the std::vector<std::vector<T> > member variable with new operator. How can i?
1
1827
by: mathieu | last post by:
Hi there, I am dealing with the following problem: I need to convert a std::vector<T(where T can be any interget type: char, short, ushort) by applying a linear transform (a,b), following; output = a*input + b (a & b are floating point type)
3
5649
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t k = i+1;
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.