[was : Re: Cross platform: How they do it?] Java memory management

Andrew L. Gould algould
Mon May 17 12:01:26 PDT 2004


On Saturday 17 April 2004 06:44 am, Chong Yu Meng wrote:
> Andrew L. Gould wrote:
> >I have a question for the programmers on the list:  It's my understanding
> > that Perl, Python and Java (and Ruby?) manage memory allocation for the
> > programmer.  Does this impact the programmers' ability to control the
> > efficiency of the application?
>
> I'm always afraid when people mention memory management in Java. Because
> poorly coded applications in Java are worse than equivalent packages in
> Visual Basic.
>
> Theoretically, you can let Java's Virtual Machine take care of the
> memory. Just create any number of objects you want and eventually, the
> garbage collector will kick in and clear all the unused or expired
> objects away. There are problems with this approach.
>
> A useful analogy that comes to mind is the way my wife and I handle
> housework. Because we have no kids and no maid, housework will have to
> be done by one of us. My wife takes out a clean cup everytime she needs
> a drink, which adds up to quite a number of dirty cups by the end of the
> day. Theoretically, I should clean the cups at the end of every day
> before all the clean cups get used (because I'm not working), and
> therefore ensure that there are plenty of clean cups the next day. Well,
> when 6 pm comes around, and I'm working on a problem (or playing
> Warcraft), guess what ? I have to drop everything and do the cups.
>
> The Java garbage collector, especially in Sun's Java VM, does the same
> thing. If you leave memory management to the Java VM, eventually, when
> it does kick in, because memory is low, you will find that your
> application "hangs up" -- literally stops responding, until the garbage
> collector finishes its task of re-claiming unused memory. This time can
> be quite significant (up to minute, once when I was doing a presentation
> to venture capitalists !) if you have a lot of memory that needs to be
> cleaned up.
>
> There is no explicit concept of pointers in Java, such as you find in C.
> But there are ways that you can make sure that objects you create are
> disposed of, by setting them to null, or using the close() method if it
> is available. This affects the programmer in a couple of ways: for
> novice programmers working on small projects for a small audience
> (example: a school lab project), this means that they can concentrate on
> the logic, and understand the language, without worrying about memory.
> This means higher productivity and a less steep learning curve. For
> larger projects, however, you'll quickly find that the garbage collector
> is no substitute for some defensive programing techniques.

Great answer -- it was easily understood.

Thank you.

Andrew Gould



More information about the Linux-users mailing list