Board index » jbuilder » If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?


2004-04-29 07:35:29 PM
jbuilder10
If a C++ stops executing, does it give all memory back to the Operating
System, even if it has leaks? I thought this was the case, but I am reading
something about Java memory leaks in:
www-106.ibm.com/developerworks/java/library/j-leaks/
"One last consideration is that the memory leak isn't a concern at all. Java
memory leaks should not be considered as dangerous as leaks that occur in
other languages such as C++ where memory is lost and never returned to the
operating system. In the case of Java applications, we have unneeded objects
clinging to memory resources that have been given to the JVM by the
operating system. So in theory, once the Java application and its JVM have
been closed, all allocated memory will be returned to the operating system."
Here it says that the difference between Java leaks and C++ leaks, is that
Java leaks are given back to the OS and C++ leaks not, if I understand it
correctly.
 
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

Mar wrote:
Quote
If a C++ stops executing, does it give all memory back to the Operating
System, even if it has leaks? I thought this was the case, but I am
reading something about Java memory leaks in:

I don't think it does, unless you have explicit mechanisms in your program
to do so, which can be a problem under Windows. If you are using Linux, it
is still best to avoid memory leaks, but I think there is an o/s garbage
collector which runs in the background freeing up lost memory allocations.
Maybe Java deals with the memory leaks by doing, on your behalf, what you
would normally do in your C++ program i.e. having exception catching
constructs to release allocated memory. However, no matter how carefully
you design your program, I suspect that there are some pathological cases
where memory leaks will still occur, even with Java.
Good luck
John
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

At 13:35:29, 29.04.2004, Mar wrote:
Quote
If a C++ stops executing, does it give all memory back to the Operating
System, even if it has leaks?
The OS will take all its memory back.
--
Rudy Velthuis (TeamB)
"There is no reason anyone would want a computer in their home."
-- Ken Olson, president, chairman and founder of Digital Equipment Corp.,
1977
 

{smallsort}

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

Rudy Velthuis (TeamB) wrote:
Quote
At 13:35:29, 29.04.2004, Mar wrote:

>If a C++ stops executing, does it give all memory back to the Operating
>System, even if it has leaks?

The OS will take all its memory back.
Sorry, I stand corrected
John
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

"Mar" < XXXX@XXXXX.COM >writes:
Quote
If a C++ stops executing, does it give all memory back to the Operating
System, even if it has leaks?
Depends on the environment. C++ is a language, and you're asking how
the operating system behaves. The real question is, "does the OS
reclaim the memory?", since it is running the show. C++ doesn't give
it back at shutdown, the OS *takes* it.
However, that is a function of the OS, and now you have to consider
the platform. If you're running in a hosted environment, then the OS
will reclaim memory. In many embedded devices, the OS does very
little, and the application must release all memory.
For windows, unix, linux, mac, etc, you don't have to worry.
Quote
I thought this was the case, but I am reading something about Java
"One last consideration is that the memory leak isn't a concern at all. Java
memory leaks should not be considered as dangerous as leaks that occur in
other languages such as C++ where memory is lost and never returned to the
operating system. In the case of Java applications, we have unneeded objects
clinging to memory resources that have been given to the JVM by the
operating system. So in theory, once the Java application and its JVM have
been closed, all allocated memory will be returned to the operating system."

Here it says that the difference between Java leaks and C++ leaks, is that
Java leaks are given back to the OS and C++ leaks not, if I understand it
correctly.
That's not what it's saying. They're saying that in a garbage
collected environment, no memory is ever truly leaked like it can be
in C++, since the GA always knows about the memory, and in C++ the
memory can be completely forgotton. In a GA language, you can have
situations where the memory is EFFECTIVELY leaked, because the GA is
convinced it's still in use but it isn't, so the memory never gets
reclaimed.
In both cases, however, the JVM or a C++ program, upon termination,
will have their memory reclaimed by the os when running in a hosted
environment. (The java JVM is itself "just a program.")
Note, some different types of resources are NOT reclaimed, even by
windows, because they are expected to exist for multiple programs to
use. IN those cases, if the only program that needs them crashes,
those resources are not released.
--
Chris (TeamB);
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in
Quote
Depends on the environment. C++ is a language, and you're asking how
the operating system behaves. The real question is, "does the OS
reclaim the memory?", since it is running the show. C++ doesn't give
it back at shutdown, the OS *takes* it.
Ok. Now I understand. Thanks
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

At 16:22:58, 29.04.2004, Chris Uzdavinis (TeamB) wrote:
Quote
"Mar" < XXXX@XXXXX.COM >writes:

>If a C++ stops executing, does it give all memory back to the
>Operating System, even if it has leaks?

Depends on the environment. C++ is a language, and you're asking how
the operating system behaves. The real question is, "does the OS
reclaim the memory?", since it is running the show. C++ doesn't give
it back at shutdown, the OS takes it.
Indeed. And in the case of BCB written programs (which are obviously
Win32), the OS does.
--
Rudy Velthuis (TeamB)
"Most people would sooner die than think; in fact, they do so."
- Bertrand Russell (1872-1970)
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

On Thu, 29 Apr 2004 13:35:29 +0200, "Mar"
< XXXX@XXXXX.COM >wrote:
Quote
If a C++ stops executing, does it give all memory back to the Operating
System, even if it has leaks? I thought this was the case, but I am reading
something about Java memory leaks in:

www-106.ibm.com/developerworks/java/library/j-leaks/
I saw your post in the java language group, also read the link above,
and got a laugh out of the section you quote. Java people tend to
hate sweeping generalized disparities such as "Java is slow", then
they toss out something like that...
I considered replying in the java group but wanted to check my Bjarne
book first. I looked this morning and didn't see anything specific.
I'm not {*word*155} enough to check the language spec, but I don't think
the C++ language itself makes any guarantees one way or the other.
After all, some C++ implementations use garbage collection, some run
on virtual machines - the language is pretty flexible in that regard
and there seems to be room for all sorts of behaviors.
As Rudy says, for Win32 programs there are no worries. If you use a
different OS, you'd probably want to confirm what it does. (I'd be
particularly careful with embedded ones.)
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

On Thu, 29 Apr 2004 17:12:30 GMT, XXXX@XXXXX.COM (Bill D.
Pirkle) wrote:
Quote
I saw your post in the java language group, also read the link above,
and got a laugh out of the section you quote. Java people tend to
hate sweeping generalized disparities such as "Java is slow", then
they toss out something like that...
I see now that this message was cross-posted to both the cpp and java
language groups (normally a bad idea BTW). I replied in the cpp group
and was about to post in the Java one, but now no need. Cool.
(For the record, I realize that my "they" above could also be
considered a sweeping generalized disparity and I'm fully cognizant of
the hypocrisy that I'm exhibiting.) :)
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

"Rudy Velthuis (TeamB)" < XXXX@XXXXX.COM >wrote
Quote
Indeed. And in the case of BCB written programs (which are obviously
Win32), the OS does.
I am hardly using BCB by the way, it was a more general question.
 

Re:If a C++ stops executing, does it give all memory back to the Operating System, even if it has leaks?

At 14:06:52, 03.05.2004, Mar wrote:
Quote

"Rudy Velthuis (TeamB)" < XXXX@XXXXX.COM >wrote

>Indeed. And in the case of BCB written programs (which are obviously
>Win32), the OS does.

I am hardly using BCB by the way, it was a more general question.
In the case of Win32 programs, the OS will do it. This is probably true
for most other OSes as well. It is of course good programming to do it
yourself, and not to rely on the OS.
--
Rudy Velthuis (TeamB)
Mother-in-law = A woman who destroys her son-in-law's peace of mind by
giving him a piece of hers. - Anonymous.