|
|
动态内存__回收
|
|
在c语言中,main( )调用的函数中申请的动态内存是在此函数
返回时被系统收回,还是在整个程序结束后才被收回?
如:
main( )
{
... ...
function( );
... ...
}
function( )
{
... ...
int *a=(int *)malloc(sizeof(int));
... ...
/* 没有运用free( )释放内存. */
}
TRUST YOURSELF
TRUST YOUR FRIENDS
|
|
|
Re: 动态内存__回收
|
|
整个程序结束后才被收回
下岗了,救命啊(我可说的是真的)!
|
|
|
程序结束后才收回动态内存[空]
|
|
tinybit,喜欢shell编程。http://tinybit.163.net 或 http://tinybit.oso.com.cn
|
|
|
Re: 动态内存__回收
|
|
如果在function( )中运用了free( )呢?
是不是在function( )返回时被收回呢?
多谢. :-)
TRUST YOURSELF
TRUST YOUR FRIENDS
|
|
|
C是没有对动态内存自动回收功能的,只能显式的释放。不明白你们说些什么?!
|
|
malloc 必须 free掉,否则会 memory leak,
c不是java没有自动回收
也许你们在说别的?
我不明白你们说些什么
:)
|
|
|
我估计是指Linux系统的内存回收机制
|
|
Linux中的进程结束时,应该会回收进程所占用的存储空间的,即使进程中存在内存漏洞(这一点连Windows NT都可以做到)。否则,如果有一两个人在Linux上调试程序,系统岂不是要崩溃了?
|
|
|
Re: 我估计是指Linux系统的内存回收机制
|
|
there was some kind of similar discussions on the board. 1) The system doesn't actually RECYCLE or collect the gabbage, it just simply expires the virtual memory the program was consuming. 2) Since there are no pointers in java, so there MUST be a gabbage collection mechanism., to collect the "lost" objects. but abusive use can make your program very slow. That's why one should use StringBuffer rather than String (whenever possible) since String will need collect its gabbage twice.
不能怨天尤人 --David.
|
|
|
可是如果你的程序存在内存泄漏,长期运行的确会崩溃的,NT,LINUX都一样。
|
|
没听说除了java 以外c也有垃圾收集,
c这么底层的东西不会也没必要做这个
:)
|
|
|
当然不是由C来收集垃圾
|
|
你误解了我的意思。内存泄漏的程序如果长期运行,当然会造成系统崩溃了,谁要是喜欢长期运行一个调试着的程序那我也没办法。我说的是进程结束以后,占用的内存会收回来,这显然不是由C来完成的,我没有说过是C的垃圾收集功能。正如compress所说,跟操作系统本身的机制有关。
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
fundamental concept is wrong, as i said, the system does not recycle !
不能怨天尤人 --David.
|
|
|
内存泄漏未必会引起系统崩溃
|
|
内存泄漏未必会引起系统崩溃
程序会在耗尽内存后调用分配内存函数时失败
如果内存泄漏会引起系统崩溃,那么任何用户都会很容易将UNIX系统搞垮
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
you can try it.
allocate memory , not free it and exit.
your available memory change smaller?
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
read the f**king previous post of mine: the system does not RECYCLE!!! it just expires the virtual memory.
不能怨天尤人 --David.
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
RECYCLE和expires 到底什么意思呀.都什么具体的含义?
这不都是系统回收内存吗? 难道进程退出了, 但是虚拟内存的使用期限可以仍然很长,不能回收? 有这种情况发生??? 不明白,实在不明白
|
|
|
Re: 动态内存__回收
|
|
在linux中,对malloc类的处理是,如果在程序中用了free(),想当于给这段内存作个标志--他可以被malloc( )分配,
用完了应该不被down掉,最多malloc反回NULL,调用失败:)
*********实践是最好的学习方法********
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
You don't understand because of your little experience. Sometimes, a process exits, but its resources can't be released.
You don't believe? I don't care. Nobody cares about you believe or not.
人类每天都在干些什么?
--制造垃圾。
|
|
|
可以讲讲吗?很感兴趣。(NULL)
|
|
:)
|
|
|
Re: 可以讲讲吗?很感兴趣。(NULL)
|
|
stoneli已经说得很明白了吗?这些问题其实已经有无数人问过,无数人答过了,:-).
去groups.google.com吧,从那儿去comp.os.linux.*
很多问题都答了n次了
我睡我睡.......
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
内存监控
$ vmstat 1
0 0 0 692436 202232 0 0 0 0 0 0 0 0 0 0 0 107 21 42 0 0 100
0 0 0 692436 202232 0 0 0 0 0 0 0 0 0 0 0 110 26 49 0 0 100
0 0 0 589976 177712 5 7984 0 0 0 0 0 0 0 0 0 108 89 63 73 25 2
0 0 0 589976 145292 0 8086 0 0 0 0 0 0 0 0 0 107 21 47 74 26 0
0 0 0 589976 112852 0 8132 0 0 0 0 0 0 0 0 0 107 30 50 85 15 0
0 0 0 692436 202232 0 1479 0 0 0 0 0 0 0 0 0 109 76 44 15 21 64
0 0 0 692436 202232 0 0 0 0 0 0 0 0 0 0 0 109 22 48 0 0 100
0 0 0 692436 202232 0 0 0 0 0 0 0 0 0 0 0 109 27 44 0 0 100
测试程序:
#include <stdio.h>
#include <stdlib.h>
int main(){
char *p;
unsigned long size,i;
size = 100*1024*1024;
p = malloc(1024*1024*100);
if (!p) printf("mem error");
else {
printf("mem ok");
for(i =0 ; i < size; i++) *(p+i) = 1;
}
return 0;
}
为什么我的内存没有变小?我的程序能自动释放内存?
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
我倒!
您老的逻辑和我真的是不一样。我根本看不出你上篇内容和讨论的问题有什么关系。好吧,就按你说的,你的程序在我的机器上运行结果是这样的:
0 0 0 11556 2040 24540 15524 0 0 0 10 192 38 0 1 99
0 0 0 11556 2040 24540 15524 0 0 0 0 118 58 3 4 93
0 0 0 11556 2040 24540 15524 0 0 0 1 116 36 0 1 99
0 0 0 11556 2040 24540 15524 0 0 0 0 112 38 0 1 99
0 0 0 11556 2040 24540 15524 0 0 0 0 114 56 5 2 93
=====================================================
以上是运行测试程序之前的vmstat输出,以下是运行之后的
=====================================================
1 0 0 11556 1084 1308 8676 0 0 0 10 200 70 55 11 34
3 2 1 21436 1560 1308 8608 144 4978 47 1245 10449 195 21 54 5
4 0 0 26844 984 1308 9068 278 2766 88 692 6404 156 20 45 35
2 1 1 36444 1616 1308 8904 0 4802 55 1201 10210 194 17 58 5
4 1 1 45092 1576 1308 8824 58 4324 37 1084 9094 227 15 60 25
1 0 0 50396 1540 1308 4636 80 2654 75 664 6061 120 31 47 23
1 0 0 56112 1552 1308 1632 50 2866 18 722 6024 98 24 49 27
1 1 1 66760 1632 1308 6552 0 5324 1 1331 10727 135 13 55 2
1 3 1 76128 1540 1308 5144 32 4684 90 1176 10340 214 21 57 2
=========================================================
以下是程序结束后
=========================================================
0 0 0 17932 43788 1324 3712 84 0 37 0 404 40 0 1 99
0 0 0 17932 43788 1324 3712 0 0 0 0 114 42 0 0 100
0 0 0 17932 43788 1324 3712 0 0 0 10 193 48 3 3 94
真搞不懂,这个输出能得出“你的程序能自动释放内存”的结论吗?建议去考回GRE。
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
你的机器上除了测试程序还有其他程序运行吧.
你的内存怎么越用越多呀? ^_^
我这人比较苯, 考不过什么gre, 让你笑话了.
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
1 I didn't have any other program running when test your program.
2 Do you know what the output of vmstat means? I think you don't, or you won't say "memory gets more on your machine".
3 Yeah, you really have no logic.
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
没有其他程序?你到底懂不懂vmstat的各项含义呀? 回去看看系统帮助. 我看你对此一知半解吧.
|
|
|
Sorry,我不应该滥用“回收”一词
|
|
确切地说,应该不是回收,而是释放出物理内存,但又堆积在虚拟内存里了。我因为很少用指针动态分配大量内存,从来没有出现虚拟内存占满的问题,所以习惯于把释放物理内存当作“回收”,而对虚拟内存视而不见。这是个坏习惯。
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
yeah, some other programs must be running on an active system. that's true, but my meaning is different. everybody knows it, so i don't want to say more.
if you know what the output of vmstat means, you won't say "memory gets more".
i don't want to discuss this topic with you silly guy any more.
no logic person
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: unix一般都可以回收进程占用资源的
|
|
首先,我要说的是我的测试是在solaris上作的(256mb),你的可能是在linux上作的。
在linux上测试,你也应该让系统空负载吧,看看你的输出:测试前,系统仍有负载,id为100吗?你再看看其他项,如果不为100,给个理由系统没有其他的负载程序运行。测试程序结束后,同样的问题。(也许你的系统资源不够用,系统一直忙于处理?)
做实验,希望你严谨一些。
我们讨论问题是讨论问题,但是请你注意你的言辞。不好听的话,谁都可以说。
|
|
|
因为内存泄漏所以内存越用越多直到分配不出内存崩溃
|
|
我的理解如果正确吧?
:)
|
|
|
因为内存泄漏所以内存越用越多直到分配不出内存崩溃
|
|
我的理解正确吧
:)
|
|
|
I'll teach you the last lesson
|
|
"做实验"? "系统仍有负载,id为100吗" What stupid sentences!
Any difference between test on solaris and linux? What does "solaris上作的(256mb)" mean? Does memory size get in our question?
vmstat only reports the vitual memory statistics from a certain device. You cannot get how much memory available from its output. Although the system isn't at 100% idle, the system's load is stable (i.e no burst job), which you can see on my vmstat's output. I showed the vmstat's output, just want to tell you how silly your conclusion is.
Would you mind to explain the output of "mpstat 2" on a solaris system:
CPU minf mjf xcal intr ithr csw icsw migr smtx srw syscl usr sys wt idl
0 0 0 158 100 99 18 0 0 4 0 14 0 1 1 97
1 0 0 215 112 112 20 0 0 8 0 6 0 0 0 100
2 0 0 1316 412 212 21 0 0 8 0 5 0 1 0 99
3 0 0 145 101 101 10 0 0 4 0 8 0 4 2 94
4 0 0 116 101 100 10 0 0 4 0 13 0 1 1 97
5 0 0 96 105 103 11 0 0 4 0 13 0 1 2 97
6 0 0 161 100 100 10 0 0 4 0 12 0 2 1 97
7 0 0 161 101 101 11 0 0 4 0 13 0 1 1 98
8 0 0 100 100 100 14 0 0 4 0 12 0 1 2 98
9 0 0 148 100 100 14 0 0 4 0 14 0 1 1 98
10 0 0 165 100 100 12 0 0 4 0 15 0 0 1 98
11 0 0 192 100 100 13 0 0 4 0 15 0 0 2 98
CPU minf mjf xcal intr ithr csw icsw migr smtx srw syscl usr sys wt idl
0 2 0 0 100 100 15 0 1 4 0 18 0 0 0 100
1 0 0 0 100 100 5 0 0 4 0 0 0 0 0 100
2 0 0 1100 400 200 7 0 0 5 0 0 0 0 0 100
3 0 0 0 101 101 3 0 0 1 0 0 0 10 0 90
4 0 0 0 100 100 7 0 1 6 0 0 0 0 0 100
5 0 0 0 104 102 13 0 0 4 0 0 0 0 0 100
6 0 0 0 100 100 24 0 1 4 0 10 0 0 0 100
7 0 0 0 100 100 15 0 2 1 0 10 0 0 0 100
8 0 0 0 100 100 9 0 0 4 0 6 0 0 0 100
9 0 0 0 100 100 8 0 0 2 0 0 0 0 0 100
10 0 0 0 100 100 6 0 0 4 0 0 0 0 0 100
11 0 0 0 100 100 12 0 0 5 0 25 0 0 0 100
that why most of the idle was not 100 in the first page of output, and suddenly most of the idle was 100 in the second page?
And would you mind to tell me how much memory was on the solaris 7 system whose vmstat reports like:
procs memory page disk faults cpu
r b w swap free re mf pi po fr de sr s0 s1 s7 s8 in sy cs us sy id
0 0 0 119480 34160 1 1 1 5 226 0 28 1 0 0 0 1434 145 171 0 1 99
0 0 0 19135224 207704 0 2 0 0 0 0 0 0 0 0 0 1406 75 127 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 37 123 0 2 98
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 45 131 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1405 69 125 0 2 98
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1405 49 128 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 74 129 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 3 0 0 0 1409 167 171 0 3 97
And why is the sum of swap and free in the first line so small comparing to others?
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: I'll teach you the last lesson
|
|
关于内存变多的意思是可用内存,我漏字了。
如果你的内存过小,系统可能会经常页交换,这点我想我不用过多解释。
建议你仔细看看vmstat的各项说明,仔细看看你原来的贴子,系统中如果没有别的负载程序运行,用户的系统占用怎么不为0,能给个理由吗?可用内存变多了?谁释放的??空负载,不明白什么意思?
goole上关于内存释放及系统回收的贴子,有兴趣看看:
http://groups.google.com/groups?q=memory+allocate+free&start=60&hl=zh-CN&lr=&group=comp.*&safe=off&rnum=69&ic=1&selm=6sogkh%24605%244%40mississippi.rivm.nl
我也不想就这个问题讨论下去了。在unix上,进程结束后,申请但没有释放的内存,统统会被系统回收的。假设,系统不回收,如果系统有100M内存,一个程序申请60M内存,不释放,然后退出,紧接着再次运行,系统是不是不能提供足够的内存了呢?如果真如此,一个普通用户很容易将所有的内存吃掉,而系统又不知道谁吃的,这岂不是笑话?
一切就此打住吧!
|
|
|
Don't confuse us!
|
|
"关于内存变多的意思是可用内存" I know you meant availble memory. It's not that you lost a word, but it's the brain that you lost.
"系统中如果没有别的负载程序运行,用户的系统占用怎么不为0" I have metioned in my previous post that this is NOT involved in the question. My system has many programs running, such as X, but they are in a stable state, so memory swapping is almost caused by the test program. So although my system is not 100% idle, the monitorring of vmstat can still weak your point. I said once again: YOU CAN'T GET TO KNOW HOW MUCH MEMORY AVAIBLE FROM vmstat's OUPUT.
"如果系统有100M内存,一个程序申请60M内存,不释放,然后退出,紧接着再次运行,系统是不是不能提供足够的内存了呢" Everybody knows it. But this is not the topic we talking about.
人类每天都在干些什么?
--制造垃圾。
|
|
|
Oh my god! Can we just stop?!
|
|
"YOU CAN'T GET TO KNOW HOW MUCH MEMORY AVAIBLE FROM vmstat's OUPUT"
Of course you CAN! Vmstat tells you the available (free) physical memory.
Under solaris, looks at "free"; under linux, looks at "free"+"buff"+"cache".
Solaris' vmstat also tells the available virtual memory (swap) under "swap".
But on linux it tells the opposite, how much swap has been used under "swpd".
老大, 的确是您错的多点儿. 错了就认吧, 没什么丢人的. 要是再这么说下去, 到真是
丢份了. :)
最初的贴子其实问了一个很简单的问题. 搞不懂为什么 compress 和您非要在文字上
绕, 绕的无比复杂, 绕的大家都胡涂了.
我试着正式回答一下这个问题, 希望对被搞胡涂了的朋友有所帮助:
在程序运行过程中, 申请的动态内存是永远不会交还给系统的. 即使使用 free 也
不会! free 的动态内存只能被本程序后续的 *alloc 利用.
在程序结束后, 即使不使用 free, 所有申请的动态内存自动回到系统的控制之下,
可以在以后分配给其它程序使用.
有不同意见吗?
|
|
|
您是老大
|
|
其实我就等您发话呢。因为我估计您能对这个问题说出个子丑寅牟来。
您说的那个关于free的结论跟前面讨论的答案是一样的。
至于您说能从vmstat得到可用内存,我就搞不懂了。
这里是solaris上vmstat的输出,您看看,拜托能告诉我此时大概还有多少内存可用吗?
r b w swap free re mf pi po fr de sr s0 s1 s7 s8 in sy cs us sy id
0 0 0 119480 34160 1 1 1 5 226 0 28 1 0 0 0 1434 145 171 0 1 99
0 0 0 19135224 207704 0 2 0 0 0 0 0 0 0 0 0 1406 75 127 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 37 123 0 2 98
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 45 131 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1405 69 125 0 2 98
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1405 49 128 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 0 0 0 0 1406 74 129 0 0 100
0 0 0 19135224 207704 0 0 0 0 0 0 0 3 0 0 0 1409 167 171 0 3 97
PS,我可不是和compress非要在文字上绕,我是针对mirnshi在这里没逻辑的乱搅。
人类每天都在干些什么?
--制造垃圾。
|
|
|
万万不敢当
|
|
您别吓着我.
为什么您不原意相信该机器大约有 203M 物理内存可用? 有什么不对的吗?
|
|
|
Re: 万万不敢当
|
|
Oh? really?
It's so small!
My machine has 12Giga bytes physical memory. And so small left!
Thank you!
But I don't want to believe:( I hope it's not true.
555555555555555............................................
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: 您是老大
|
|
也许多年来,我误解了文档,可是我看了文档,理解的还是那个意思,我的英文不怎么好。只好将vmstat的英文帮助说明抄了过来,请你们这些中英文高手过过目,然后用中文告诉我们,以解心中疑惑。
memory Report on usage of virtual and real memory.
swap amount of swap space currently available (Kbytes)
free size of the free list (Kbytes)
最好呢,能根据您的数据给我们讲解一下,有劳了。
|
|
|
Re: Oh my god! Can we just stop?!
|
|
"申请的动态内存是永远不会交还给系统的" forever? really?
Would you mind to look at the following codes, and tell me whether it can return the allocated memory to the system during running? Thank you.
test on a solaris 2.6 system
#include "stdlib.h";
#include "unistd.h";
main()
{
char *p, *p1, *p2;
printf("pid=%d\n",getpid());
printf("sbrk=%x\n",p1=sbrk(0));
p = malloc(1024*1024*100);
printf("sbrk=%x\n",sbrk(0));
sleep(5);
free(p);
printf("sbrk=%x\n",sbrk(0));
sleep(5);
if (brk(p1) < 0) {
perror("brk");
exit(1);
}
printf("sbrk=%x\n",sbrk(0));
sleep(5);
}
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: Oh my god! Can we just stop?!
|
|
bash-2.03$ vmstat 1
procs memory page disk faults cpu
r b w swap free re mf pi po fr de sr cd s0 -- -- in sy cs us sy id
0 0 0 698976 207032 1 3 1 0 0 0 0 1 0 0 0 107 125 42 0 0 100
0 0 0 697136 202104 0 8 0 0 0 0 0 0 0 0 0 107 36 52 0 0 100
0 0 0 697136 202104 5 86 0 0 0 0 0 0 0 0 0 108 97 50 0 0 100
0 0 0 287472 202040 1 0 0 0 0 0 0 0 0 0 0 106 28 49 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 109 30 44 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 105 20 50 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 107 20 46 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 110 31 48 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 106 20 44 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 108 31 50 0 0 100
0 0 0 287472 202040 5 81 0 0 0 0 0 0 0 0 0 112 144 57 0 1 99
0 0 0 287472 202040 0 0 0 0 0 0 0 9 0 0 0 141 95 109 0 2 98
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 108 22 44 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 110 20 46 0 0 100
0 0 0 287472 202040 0 0 0 0 0 0 0 0 0 0 0 114 109 51 0 0 100
0 0 0 697136 202104 0 0 0 0 0 0 0 0 0 0 0 108 40 53 0 0 100
0 0 0 697136 202104 0 0 0 0 0 0 0 0 0 0 0 108 30 48 0 0 100
0 0 0 697136 202104 0 0 0 0 0 0 0 0 0 0 0 107 20 46 0 0 100
0 0 0 697136 202104 1 0 0 0 0 0 0 0 0 0 0 108 30 44 0 0 100
the test program:
#include
#define size 1024*1024*400
int main(){
char *p;
p = (char *)malloc(size);
if (p){
printf("P=%lp\n",p);
sleep(2); // enough time to view the memory
free(p);
printf("FREE\n");
sleep(10); // enough time to run another program.
}else{
printf("NO\n");
}
return 0;
}
我在第一个进程释放完内存后,但还进程还没有结束前,再次运行该程序时,不能申请到足够的内存。
也许这是一个普通的例子,所以我就想知道什么情况下,进程未退出前,释放的内存会回到系统,可以被其他进程使用?
|
|
|
faint
|
|
do you notice i used brk() to return memory to the system?
is it really you are brain-damaged?
have i ever said free() can return memory to system?
you don't confuse us.
i think you are not able to answer my question, so don't reply my post again.
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: faint
|
|
您别恼,讨论问题至于这样吗?我只是在问问题。
也许我没有把我的意思说清楚。当然可以通过修改数据段来做到这件事,不知道修改数据段会不会导致异常(您常用?),所以我想问还有没有其他常规的方法可以做到呢?
|
|
|
Re: faint
|
|
What did you want to say in last post which contains codes of free(), and the conclusion that "我在第一个进程释放完内存后,但还进程还没有结束前,再次运行该程序时,不能申请到足够的内存"?
You are so silly a guy! You always lead the original question to a wrong direction! I don't know what you are thinking about.
"也许我没有把我的意思说清楚" maybe you should go back to primary school to learn chinese language from abc. you always 没有把意思说清楚
Rigel, would you like to answer my question, "can i return memory to system using brk()"?
I really need your help. Thank you.
人类每天都在干些什么?
--制造垃圾。
|
|
|
Are you high or something?
|
|
haha, never mind, i'm just kidding. :)
How about I change it to this? Are you happy now?
在程序运行过程中, 使用 free 释放的动态内存一般是不会交还给系统的, 只能被
本程序后续的 *alloc 利用.
在程序结束后, 即使程序里没有使用 free 释放申请的动态内存, 所有属于该进程
内存自动回到系统的控制之下, 可以在以后分配给其它程序使用.
Excuse me, but i think your comments are just nifty picky. You knew we
were talking about malloc(3)/free(3)! Now you are not acting in the best
interest of answering the original question, rather trying to save face.
That's fine, i totally understand, after all we are human. I might do the
same thing. However i think you missed the point. The original question
is very common among programmers. To majority of them, malloc(3)/free(3)
are the bread and butter. They need to understand how it works from the
point of view of an application programmer, not a kernel hacker. What I
said above is not invented by me. It's the common knowledge in unix
programming circle for many years. It is the practical view of how the
thing works.
To most of us, saying "the system does not recycle memory, it just expires
the vm" is purely a nonsense. I feel sorry for the original poster who can
only get such a confusing and meaningless answer from this forum. To get
the record straight was my intention to post.
Of course the above cited common wisdom is not something carved on stone.
There are exceptions. At least one i'm aware of, linux. To be exactly it
is the glibc's implementation of malloc(3)/free(3) used in linux. In glibc,
if a chunk in the free list is bigger than a certain threshold, free(3) will
automatically return it to system.
And yes, your code using brk(2)/sbrk(2) indeed will be able to return memory
to the system. To show your point it's fine, but to use it this way in real
program is dangerous. The brk(2)/sbrk(2) are historical anyway. They probably
will eventually disappear or become deprecated.
If one really wants to be able to return memory to system, almost all modern
unix suggest to use mmap(2) to create a anonymous private mapping. When you
want to return the memory, do munmap(2).
However for an application programmer, he/she really does not need to know
all these details. It will be much better off for them just sticking with
malloc(3)/free(3) and leave the complexity of using mmap/munmap to library
programmers.
As for the vmstat problem. It says that you only have about 200MB left, and
you believed that is not possible. Either your vmstat was lying, or there's
something wired happening in your system. I'm just glad that it's not my
machine :) So good luck.
|
|
|
大虾就是大虾
|
|
发表个人3个说明
1 估计您看我和compress似乎穿一条裤子不顺眼,实际情况是我根本没有涉及最初那个问题的讨论,因为最初问题的结论已经很明确了。但是我发现有人(mirnshi)在这里搅浑水,乱七八糟的,所以开贬。
2 我所有的结论只有两个,一是mirnshi在胡说八道,二是vmstat不能得到可用内存。前一个结论不用多说,第二个结论也不是我自己想当然想出来的,我也是从sun那学来的,而且从经验看,确实不敢完全相信vmstat的结果。我没有研究过内存的管理问题,一切都是从材料上看来的,所以我不能说出本质的道理来。因此我希望有人能回答这个问题。
3 brk的问题应该和原始问题有点关系吧?既然您的文章中做了定论,就应该是完善的答案,不该留个小尾巴,所以提出来请教。我看到在我机器上的linux下是用mmap实现的malloc,因此在一台solaris 2.6的系统上测试了一下brk,并且用pmap看到了我想象的结果。
得到3个结论
1 放心大胆使用malloc和free吧
2 不要使用brk/sbrk
3 使用mmap/munmap可能会更好利用内存
留下1个疑问
到底怎么理解vmstat的输出?
人类每天都在干些什么?
--制造垃圾。
|
|
|
Re: 大虾就是大虾
|
|
我记得在哪看过solaris8可以看到可用的物理内存的.
出处忘记了. 不过我用vmstat和top在我的solaris8上看到的可用物理内存是一致的. 有什么不妥之处, 您就接着批, 这也许是你讨论问题的风格, 呵呵.
|
|
|
Re: vmstat
|
|
绝没有的事. 就算您和 compress 合穿半条裤子 (短裤?), 我看了只会乐. compress
是个好同志, 热心助人, 诲人不倦. 我只是就事论事, 觉得在这件事上 compress 过
于在细节上钻牛脚尖了.
残留的疑问 vmstat, 如果您能得到以下几个数, 我们可以继续讨论:
1. 机器启动时显示的物理内存和 swap 区 size, 比如,
grep mem /var/adm/messages*
(但愿我记得对, 已有几年没用过 sun 的机器了).
2. 用 pmap 得到每一个进程的 mapping, 只要总和就行了. 要三个数, private,
residence (pmap -x) 和 reserved (pmap -r). 把所有进程的这三个数分别
相加, 得到三个系统总和. (一定要所有进程, 千万别落了谁!)
3. 当时的 vmstat 数据.
您有 root 权力吧? 2 需要.
|
|
|
Re: vmstat
|
|
我换了台看起来比较正常的机器,因为这台机器vmstat显示的free和机器的物理内存相比值得相信,而且那台看起来不正常的机器我也不想动。
1 /usr/platform/sun4u/sbin/prtdiag显示:
Memory size: 4096 Megabytes
swap -s显示:
total: 42400k bytes allocated + 5104k reserved = 47504k used, 11933560k available
2 除了三个system process不能pmap外,得到755376这个和
3 vmstat 2的前三行输出为:
0 0 0 7472 6008 74 6 25 606 624 0 20 4 0 0 0 707 970 291 5 4 91
0 0 0 11933152 3562696 0 1 0 0 0 0 0 0 0 0 0 607 13 95 0 1 99
0 0 0 11933152 3562696 0 0 0 0 0 0 0 0 0 0 0 604 32 91 0 0 100
以上数据显然会有些误差,因为我无法绝对精确的同时进行三个工作。这台机器很闲,几乎没有任何工作,也没人用。这下mirnshi该不会有意见了吧。
拜托分析一下
人类每天都在干些什么?
--制造垃圾。
|