将内存返回给操作系统 [英] return memory to the OS

查看:55
本文介绍了将内存返回给操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们!

请看下面的程序:


1 #include< iostream>

2 #include< list>

3 #include< unistd.h>

4使用命名空间std;

5 int main()

6 {

7 {

8 list< int> tempList;

9

10 cout<< 即将开始添加...... << (b)(长期i = 0; i <= 1000000; i ++)

12 {

13 tempList.push_back(i) ;

14}

15 cout<< 完成添加。 <<结束;

16睡觉(10);

17}

18

19 cout<< 超出范围 <<结束;

20睡觉(10);

21返回0;

22}


当我用顶级跑步运行这个程序时,我看到即使在行打印了
19后,内存(顶部返回为16 M)也没有返回到

运行系统(Red Hat Linux 9.)。据我所知,这是由STL进行优化以缓存内存以供将来使用,但它是否可以使STL将此内存返回给操作系统(例如使用系统

电话)?


任何投入赞赏。

谢谢!

Hi Folks!
Please see the program below:

1 #include<iostream>
2 #include<list>
3 #include <unistd.h>
4 using namespace std;
5 int main()
6 {
7 {
8 list<int> tempList;
9
10 cout << "going to start adding now ..." << endl;
11 for(long i=0; i<= 1000000; i++)
12 {
13 tempList.push_back(i);
14 }
15 cout << "done adding." << endl;
16 sleep(10);
17 }
18
19 cout << "out of scope" << endl;
20 sleep(10);
21 return 0;
22 }

When I run this program with top running, I see that even after line
19 is printed the memory(which top returns as 16 M) is not returned to
the operating system(Red Hat Linux 9.). I understand that this is an
optimization by STL to cache the memory for future use, but is it
possible to make STL return this memory to the OS (say using a system
call)?

Any inputs appreciated.
Thanks!

推荐答案

2004年7月12日11:47:14 -0700,~Gee< hu ******** @ hotmail.com>写道:
On 12 Jul 2004 11:47:14 -0700, ~Gee <hu********@hotmail.com> wrote:
嗨伙计们!
请看下面的程序:

1#include< iostream>
2 #include< list> ;
3 #include< unistd.h>
4使用命名空间std;
5 int main()
6 {
7 {
8 list< ; INT> tempList;
9
10 cout<< 即将开始添加...... << endl;
11 for(long i = 0; i< = 1000000; i ++)
12 {
13 tempList.push_back(i);
14}
15 cout<< 完成添加。 << endl;
16 sleep(10);
17}
18
19 cout<< 超出范围 << endl;
20睡觉(10);
21返回0;
22}

当我运行顶级跑步程序时,我看到即使在行< 19打印内存(顶部返回为16 M)未返回操作系统(Red Hat Linux 9.)。据我所知,这是STL优化缓存内存以供将来使用,但是可以让STL将此内存返回给操作系统(比如使用系统调用)吗?

任何意见都赞赏。
谢谢!
Hi Folks!
Please see the program below:

1 #include<iostream>
2 #include<list>
3 #include <unistd.h>
4 using namespace std;
5 int main()
6 {
7 {
8 list<int> tempList;
9
10 cout << "going to start adding now ..." << endl;
11 for(long i=0; i<= 1000000; i++)
12 {
13 tempList.push_back(i);
14 }
15 cout << "done adding." << endl;
16 sleep(10);
17 }
18
19 cout << "out of scope" << endl;
20 sleep(10);
21 return 0;
22 }

When I run this program with top running, I see that even after line
19 is printed the memory(which top returns as 16 M) is not returned to
the operating system(Red Hat Linux 9.). I understand that this is an
optimization by STL to cache the memory for future use, but is it
possible to make STL return this memory to the OS (say using a system
call)?

Any inputs appreciated.
Thanks!




STL不应该按照你描述的方式缓存内存,但如果是然后

你应该为std :: list编写自己的自定义分配器然后你可以用你想要的任何方式分配和释放内存。


这个主题在Josuttis的C ++标准库中为

实例介绍,但如果你需要帮助,可以再次发布。


john



The STL should not cache memory in the way you describe, but if it is then
you should write your own custom allocator for std::list then you can
allocate and free memory in any way you like.

This topic is covered in The C++ Standard Library by Josuttis for
instance, but post again if you need help doing this.

john


~Gee发布:
~Gee posted:
当我运行顶级跑步时,我看到第19行后甚至
打印内存(顶部返回16 M)是
没有返回到操作系统(Red Hat Linux 9.)。我知道
这是STL优化缓存内存以备将来使用,
但是有可能让STL将这个内存返回给操作系统(比如使用系统调用的
)吗?

任何意见赞赏。
谢谢!
When I run this program with top running, I see that even after line 19 is printed the memory(which top returns as 16 M) is not returned to the operating system(Red Hat Linux 9.). I understand that this is an optimization by STL to cache the memory for future use, but is it possible to make STL return this memory to the OS (say using a system call)?

Any inputs appreciated.
Thanks!




由yourDictionary.com提供:

top < br $>
n。

1.最上面的部分,点,表面或结束。


2.离给定距离最远的部分参考点:从钥匙顶部拍摄

a跳投。


3.头顶:从上到下。


4.植物的一部分,如大头菜,即地面上的



5.东西,如盖子或帽子,盖上或形成最上面的部分。


6.穿在身体上半部的衣服,< br $> b $ b特别是毛衣或针织衬衫。


7.航海用一个平台包围每个桅杆的头部

的帆船,哪个顶级索具是

附加。


8.最高等级,音高或点数;高峰,极致,

或天顶:它来的时候他感觉不到他的形式顶部的b $ b。 (安东尼鲍威尔)。


9. a。最高职位或等级:在他的b $ b $职业的顶部。湾这个职位的人。


10.游戏最高卡或西装或手牌。


11.最好的部分。


12.最早的部分或开头:她从顶部再次演奏了




13.棒球上半局。


14.运动a。中风落在高尔夫球场或网球场中心球的中心上方,让它向前旋转。湾A />
由于这样的击球导致球向前旋转。


adj。

1.位于顶部:顶级货架。


2.最高学历,质量,等级或金额:

顶级表格;十大畅销书。


3.处于优势地位:她所在部门的最高历史学家。


v。顶部,顶部·ping,顶部

v.tr.

1.形成,提供或作为顶部。
< br $>
2.到达顶部。


3.超越。


4。超过或超过。


5.成为她的头:她在班上名列前茅。


6.删除顶部或最上面的部分;庄稼:

高居果树。


鉴于此,请告知我们*您的*定义

of ; top"。

-JKop



Courtesy of yourDictionary.com:
top
n.
1. The uppermost part, point, surface, or end.

2. The part farthest from a given reference point: took
a jump shot from the top of the key.

3. The crown of the head: from top to toe.

4. The part of a plant, such as a rutabaga, that is
above the ground.

5. Something, such as a lid or cap, that covers or forms
an uppermost part.

6. A garment worn on the upper half of the body,
especially a sweater or knit shirt.

7. Nautical A platform enclosing the head of each mast
of a sailing ship, to which the topmast rigging is
attached.

8. The highest degree, pitch, or point; the peak, acme,
or zenith: "It had come at a time when he was not feeling
at the top of his form" (Anthony Powell).

9. a. The highest position or rank: at the top of his
profession. b. A person in this position.

10. Games The highest card or cards in a suit or hand.

11. The best part.

12. The earliest part or beginning: She played the piece
again, from the top.

13. Baseball The first half of an inning.

14. Sports a. A stroke that lands above the center of a
ball, as in golf or tennis, giving it a forward spin. b. A
forward spin on a ball resulting from such a stroke.

adj.
1. Situated at the top: the top shelf.

2. Of the highest degree, quality, rank, or amount: in
top form; the top ten bestsellers.

3. In a position of preeminence: the top historian in
her department.

v. topped, top·ping, tops
v. tr.
1. To form, furnish with, or serve as a top.

2. To reach the top of.

3. To go over the top of.

4. To exceed or surpass.

5. To be at the head of: She topped her class.

6. To remove the top or uppermost part from; crop:
topped the fruit trees.

Given that, could you kindly inform us of *your* definition
of "top".
-JKop


hu ******** @ hotmail.com (~Gee)在留言中写道:< 83 ******************** ******@posting.google。 com> ...
hu********@hotmail.com (~Gee) wrote in message news:<83**************************@posting.google. com>...
嗨伙计们!
请看下面的程序:

1#include< iostream>
2 #include< list>
3 #include< unistd.h>
4使用命名空间std;
5 int main()
6 {
7 {
8 list< INT> tempList;
9
10 cout<< 即将开始添加...... << endl;
11 for(long i = 0; i< = 1000000; i ++)
12 {
13 tempList.push_back(i);
14}
15 cout<< 完成添加。 << endl;
16 sleep(10);
17}
18
19 cout<< 超出范围 << endl;
20睡觉(10);
21返回0;
22}

当我运行顶级跑步程序时,我看到即使在行< 19打印内存(顶部返回为16 M)未返回操作系统(Red Hat Linux 9.)。据我所知,这是STL优化缓存内存以供将来使用,但是可以让STL将此内存返回给操作系统(比如使用系统调用)吗?

任何投入赞赏。
谢谢!
Hi Folks!
Please see the program below:

1 #include<iostream>
2 #include<list>
3 #include <unistd.h>
4 using namespace std;
5 int main()
6 {
7 {
8 list<int> tempList;
9
10 cout << "going to start adding now ..." << endl;
11 for(long i=0; i<= 1000000; i++)
12 {
13 tempList.push_back(i);
14 }
15 cout << "done adding." << endl;
16 sleep(10);
17 }
18
19 cout << "out of scope" << endl;
20 sleep(10);
21 return 0;
22 }

When I run this program with top running, I see that even after line
19 is printed the memory(which top returns as 16 M) is not returned to
the operating system(Red Hat Linux 9.). I understand that this is an
optimization by STL to cache the memory for future use, but is it
possible to make STL return this memory to the OS (say using a system
call)?

Any inputs appreciated.
Thanks!




这可能不是STL的一个功能。 (有些实现确实需要保留一个小的对象池,例如SGI',但可能不是那么大。)


我会期待它'是g ++中malloc的实现细节。请记住

当你释放内存时,你将它释放给内存管理器,而不是操作系统的
。内存管理器本身只能在最后分配的块之后释放尾随内存

。由于这通常不是总额的百分比很大,而且由于这样做的电话很昂贵,所以它不是优先考虑的事情。通常,你的应用程序的内存空间只会增长。 (我实际上已经预料到它会发布16M的

尾随记忆,请注意。这有点极端。)


有几个解决这个问题。一个是写一个游泳池

分配器,它预先要求全部内存,然后

服务它。在支持体系结构时,g ++ malloc应该通过对其进行mmapping来响应非常大的请求,在这种情况下,当它被释放时,它将释放内存回到操作系统中的
通过申请。你会为一个链接的整体列表获得更好的内存使用量,因为你可以一次服务
少于16个字节。


另一种是编写自己的malloc实现。祝你好运

。 ;-)


Jam



This is probably not a feature of the STL. (Some implementations do
keep a small object pool, e.g. SGI''s, but probably not that big.)

I''d expect it''s an implementation detail of malloc in g++. Remember
that when you free memory, you free it to the memory manager, and not
to the OS. The memory manager itself can only free trailing memory
after the last allocated block. Since this is not usually a large
percentage of the total, and since the call to do it is expensive,
it''s not a priority. So usually, your application''s memory space will
only grow. (I would actually have expected it to release 16M of
trailing memory, mind you. That''s a little extreme.)

There are a couple of ways around this. One is to write a pool
allocator which requests the full amount of memory up front, then
serves it out. On supporting architectures, g++ malloc should respond
to very large requests by mmapping them, in which case it will free
the memory back to the OS when it''s freed by the application. You''ll
also get better memory usage for a linked list of ints, since you can
serve less than 16 bytes at a time.

The other is to write your own implementation of malloc. Good luck
with that one. ;-)

Jam


这篇关于将内存返回给操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆