when did libstdc++ free memory of string ?

M

MickeyJerry

Linux 2.4.21-37.EL / gcc version 3.2.3 20030502 (Red Hat Linux
3.2.3-47) / libstdc++.so.5

struct O
{
//vector<int> v;
vector<string> v;
void load()
{
char buf[128];
for (int i = 0; i < 1024 * 1024; i++)
{
sprintf(buf, "%d", i);
//v.push_back(i);
v.push_back(buf);
}
}
};
int main()
{
//1
{
O o;
o.load();
//2
}
//3
}

when using vector<int>, memory usage at //1,//2, //3 :
PID %CPU %MEM VSZ RSS
1 17633 0.1 0.1 2272 688
2 17633 2.2 0.9 6508 5008
3 17633 1.8 0.1 2408 908

when using vector<string> :
PID %CPU %MEM VSZ RSS
1 17670 0.1 0.1 2284 688
2 17670 5.6 5.8 31728 29560
3 17670 4.6 5.0 27628 25460

when o leaves its scope(at //3), the strings in vector<string> are not
freed
(vss & rss are not decreesed as vector<int> does).
it must be the libstdc++ library that keeps this portion memory for
later usage
is there any way to disable this behaviour ?
because it seems like a kind of 'leak' if we have a server program
which should run for a long time and use string frequently.

ps. exporting GLIBCPP_FORCE_NEW or GLIBCXX_FORCE_NEW seems taking no
effect
 
K

Kai-Uwe Bux

Linux 2.4.21-37.EL / gcc version 3.2.3 20030502 (Red Hat Linux
3.2.3-47) / libstdc++.so.5

struct O
{
//vector<int> v;
vector<string> v;
void load()
{
char buf[128];
for (int i = 0; i < 1024 * 1024; i++)
{
sprintf(buf, "%d", i);
//v.push_back(i);
v.push_back(buf);
}
}
};
int main()
{
//1
{
O o;
o.load();
//2
}
//3
}

when using vector<int>, memory usage at //1,//2, //3 :
PID %CPU %MEM VSZ RSS
1 17633 0.1 0.1 2272 688
2 17633 2.2 0.9 6508 5008
3 17633 1.8 0.1 2408 908

when using vector<string> :
PID %CPU %MEM VSZ RSS
1 17670 0.1 0.1 2284 688
2 17670 5.6 5.8 31728 29560
3 17670 4.6 5.0 27628 25460

when o leaves its scope(at //3), the strings in vector<string> are not
freed
(vss & rss are not decreesed as vector<int> does).
it must be the libstdc++ library that keeps this portion memory for
later usage
is there any way to disable this behaviour ?

std::vector<T> uses std::allocator<T> for memory management by default. You
can write your own allocator and use that. Very likely, the behavior that
you see is caused by a pooling allocator.

because it seems like a kind of 'leak' if we have a server program
which should run for a long time and use string frequently.

Why is there a leak? Have you checked whether the amount of pooled memory
will grow beyond reasonable bounds if the program runs for a long time.


Best

Kai-Uwe Bux
 
G

Gianni Mariani

when using vector<int>, memory usage at //1,//2, //3 :
PID %CPU %MEM VSZ RSS
1 17633 0.1 0.1 2272 688
2 17633 2.2 0.9 6508 5008
3 17633 1.8 0.1 2408 908

when using vector<string> :
PID %CPU %MEM VSZ RSS
1 17670 0.1 0.1 2284 688
2 17670 5.6 5.8 31728 29560
3 17670 4.6 5.0 27628 25460

These do not reflect the memory usage of the application.

You need a malloc that recycles memory if you want to make this visible
and even there, it's not really going to do much for you.

Write a small test that does a malloc( 3000 ) 10,000 times and then
free()'s every second block allocated and then sleeps for 60 seconds.
You'll notice thet the memory does not appear to be available to other
processes and is still allocated to that process.
when o leaves its scope(at //3), the strings in vector<string> are not
freed
(vss & rss are not decreesed as vector<int> does).
it must be the libstdc++ library that keeps this portion memory for
later usage
is there any way to disable this behaviour ?

The question you need to ask yourself is "do you really care" ? If
you're an X server, you care and there are special versions of malloc
that use mmap for large blocks and munmap to free them which will do
what you're asking, but it may be overkill for most apps.
because it seems like a kind of 'leak' if we have a server program
which should run for a long time and use string frequently.

memory allocators like malloc will fragment memory and will cause what
appears to be like a leak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top