You are not logged in.

#1 2008-04-25 14:54:18

fiod
Member
Registered: 2007-04-02
Posts: 205

Grabage Collector?

Hey,

I got this silly question:
I know that one of the main features of Java is its Garbage Collector, preventing memory leaks during (and after) the run of the program.

Does the linux kernel have a similar feature?
If not, is it possible to add such a thing? Is there any development attempts at doing so?
If not, why not?

Thanks
Fiod

Offline

#2 2008-04-25 15:17:43

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Grabage Collector?

No, because the kernel is written in C, for speed of execution.

It's undesirable, because it would screw up the attempts at getting Linux feeling "responsive" on the desktop, which is already very difficult.

Offline

#3 2008-04-25 15:20:01

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Grabage Collector?

No, the linux kernel does not do garbage collection, and it never will.

I can't explain everything here, as it's technically like 3 or 4 college courses to cover all the details, but I will summarize. Garbage collection "costs" a lot. It takes CPU time, extra memory, things like that. The linux kernel cannot afford to pay these costs.
Additionally, the linux kernel is written in C. C has been around for 20-something years without garbage collection. There are standards and practices used to ensure that memory is cleaned up just fine. All good C programmers understand good memory management, and all kernel devs are good C programmers

Offline

#4 2008-04-25 15:20:57

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Grabage Collector?

I'm not sure what you mean by adding a garbage collector to the Linux kernel - do you mean something that goes along, checking every single running application for unreferenced memory, and getting rid of it?  That would be hugely computationally expensive, and probably not even possible to a great degree of accuracy.  Do you mean something that goes along and checks the running kernel, and its modules, for memory leaks?  I don't see why that's necessary - just fix the leaks and you don't need a GC anymore.

Offline

#5 2008-04-25 15:38:24

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: Grabage Collector?

thanks for the replies.

My question started out mainly because I feel that every time I use firefox, I "loose" some memory until
the next time I reboot.

That feeling might be misleading, but nevertheless - not every programmer can be perfect,
thus every once in a while, we use a program that causes memory leaks.

I think this applies also to kernel modules, not to mention the kernel itself (don't think I don't appreciate the kernel developers,
its just that we are all just human - and we all make mistakes..)

I got the point of the great cost a GC, and I guess the price of responsiveness is pretty big.
But as the technology advances (and CPUs are getting faster, computers get more memory, etc) - isn't that a good idea to make sure as little human mistakes as possible affect our everyday use?

thanks
fiod

Offline

#6 2008-04-25 17:37:19

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: Grabage Collector?

The problem you're likely having is called memory fragmentation, and it is being worked on. One of the major changes in 2.6.24 was a fairly large set of anti-fragmentation patches. Read this article and this changelog entry for the details.

Offline

#7 2008-04-25 17:59:50

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Grabage Collector?

fiod wrote:

My question started out mainly because I feel that every time I use firefox, I "loose" some memory until
the next time I reboot.

That feeling might be misleading, but nevertheless - not every programmer can be perfect,
thus every once in a while, we use a program that causes memory leaks.

When you quit an application, the kernel automatically reclaims ALL memory that application used, whether it was "leaked" or not by the app itself.  This is not garbage collection - just standard memory management.

What you're experiencing is most likely, as skymt said, fragmentation.

Offline

#8 2008-04-25 18:33:38

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Grabage Collector?

It's either fragmentation, or buffered memory. When people come from a Windows background, they look at free RAM as a measure of performance. That is simply not true.

Think about it this way: if you run at very low RAM usage, then you wasted money on that extra RAM. The linux kernel uses as much memory as it possibly can, at all times. It keeps files, shared libraries, things like that sitting in RAM, and your firefox usage may have caused some of this.

Offline

#9 2008-04-25 18:58:56

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: Grabage Collector?

thanks a lot for the replies.

I don't quite get that "ext3 (or any  other linux filesystem actually) does not need a defrag tool" say.

If you say that the main problem is fragmentation, shouldn't I use a defrag tool to solve it?
So how come it is not recommended to use defrag tools with ext3 / any other filesystem?

How can I defrag?

thanks
fiod

Offline

#10 2008-04-25 19:07:04

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Grabage Collector?

This is _memory_ fragmentation, not _disk_ fragmentation.  They are different things.

Offline

#11 2008-04-25 20:53:06

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: Grabage Collector?

Just an off-topic remark on Garbage collection: Done right, it can be faster than manually freed memory or at least on par with manual code. The same applies for execution speed - you can get close (or thanks to special optimization in some cases even significantly faster) to C/C++ speed in other languages, too.
It's just important that you use native code and not a VM if you care for speed and memory consumption.

The biggest advantage of garbage collection is of course for the programmer. I'm not one of the good C-programmers, I merely write decent code Valgrind is not complaining about. (i.e. without memleaks or related problems)
But I believe that if I can think more about the actual algorithm, the actual code I want to write and I have to worry less about "maintenance code", I can create a better algorithm and  less bugs. And it takes me less time, too.

But then I'm still young and yet fresh in love with functional programming languages. wink

Offline

Board footer

Powered by FluxBB