Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Clearing memory from instantiated objects
Hi,

Is it possible to remove a specific object (instantiated) whilst the
running a program.  I'm writing a highly repeatative task and dont want
to suck the systems memory having old information stored in the memory.
Thanks for any help, Jon


Report this thread to moderator Post Follow-up to this message
Old Post
mmm_moo_cows@hotmail.com
12-24-04 09:16 AM


Re: Clearing memory from instantiated objects
mmm_moo_cows@hotmail.com wrote:
> Hi,
>
> Is it possible to remove a specific object (instantiated) whilst the
> running a program.  I'm writing a highly repeatative task and dont want
> to suck the systems memory having old information stored in the memory.
> Thanks for any help, Jon
>

If you use the same name for the result variable or something like that
you don't need to worry about memory leaks; The variable is overwritten.

If you use other names the Java Virtual Machine will decide when Objects
aren't used anymore and dispose of them when it likes.

If you are about to execute a long and memory intensive task, you might
do "System.gc();" before (and after) it. That manually starts the
garbage collection (the process described in the above paragraph), but
it is not recommended for 'normal' programs, which almost every program is.

--
-------------
- ByteCoder -           ...I see stupid people
-------------
Curiosity *Skilled* the cat

Report this thread to moderator Post Follow-up to this message
Old Post
ByteCoder
12-24-04 09:16 AM


Re: Clearing memory from instantiated objects
Hi,

Thanks very much for your help, it was very helpful!
Thanks again, Jon


Report this thread to moderator Post Follow-up to this message
Old Post
mmm_moo_cows@hotmail.com
12-24-04 09:17 AM


Re: Clearing memory from instantiated objects
It wasn't in fact.
It was very untrue.

--
Tony Morris
http://xdweb.net/~dibblego/


<mmm_moo_cows@hotmail.com> wrote in message
news:1103857237.213431.148810@c13g2000cwb.googlegroups.com...
> Hi,
>
> Thanks very much for your help, it was very helpful!
> Thanks again, Jon
>



Report this thread to moderator Post Follow-up to this message
Old Post
Tony Morris
12-24-04 09:17 AM


Re: Clearing memory from instantiated objects
"ByteCoder" <ByteCoder@127.0.0.1> wrote in message
news:331eb2F3sa8pgU1@individual.net...
> mmm_moo_cows@hotmail.com wrote: 
>
> If you use the same name for the result variable or something like that
> you don't need to worry about memory leaks; The variable is overwritten.

This is not even close to true.

> If you use other names the Java Virtual Machine will decide when Objects
> aren't used anymore and dispose of them when it likes.
>
> If you are about to execute a long and memory intensive task, you might
> do "System.gc();" before (and after) it. That manually starts the
> garbage collection (the process described in the above paragraph), but
> it is not recommended for 'normal' programs, which almost every program
is.
>

It doesn't.
It makes a suggestion to run the garbage collector.
This seldom occurs in practice - that is, your suggestion is seldom honoured
by the gc.
To call gc is general poor form - an attempt to deviate from the already
complicated algorithms used by the garbage collector undermines the value
provided at best.


> --
> -------------
> - ByteCoder -           ...I see stupid people
> -------------
>                     Curiosity *Skilled* the cat

Google is the best choice for you and the OP.

The answer to the original question is, "you can't, you don't, and to want
to suggests you have deeper problems." In this case, it helps to state the
real requirement, not a broken solution.

Merry Christmas.

--
Tony Morris
http://xdweb.net/~dibblego/




Report this thread to moderator Post Follow-up to this message
Old Post
Tony Morris
12-24-04 09:17 AM


Re: Clearing memory from instantiated objects
Tony Morris <not@telling.you> wrote:
> "ByteCoder" <ByteCoder@127.0.0.1> wrote ...
 
>
> This is not even close to true.

There's some confusion in the use of the words "variable" and "name" in
ByteCoder's explanation above.  However, if you substitute "variable"
for "name", and substitute "value" for "variable", then it starts to
become fairly close to the truth.
 

> It doesn't.
> It makes a suggestion to run the garbage collector.
> This seldom occurs in practice - that is, your suggestion is seldom honour
ed
> by the gc.

Whether it's permitted for a VM to ignore System.gc() in the general
case is occasionally debated here.  I'm of the opinion that it isn't,
unless there is no deferred GC work in the algorithm (e.g., ref
counting) or there are serious reasons the garbage collector can't do a
collection at that time.  The API specification specifically calls for a
"best effort" to free all unreachable objects, and that doesn't sound
much like doing nothing.

Under either answer to that question, though, it's patently false to
suggest that System.gc() really does, in practice, rarely do anything.
Quite the contrary, it almost universally does quite a bit.  In pretty
much every VM implementation I'm aware of, System.gc() does cause a full
stop-the-world garbage collection to occur.

> To call gc is general poor form - an attempt to deviate from the already
> complicated algorithms used by the garbage collector undermines the value
> provided at best.

In general, this may be true.  However, there are times when a well-
placed System.gc() will greatly improve perceived performance.  It
almost certainly decreases actual performance, but if delays can be
concentrated into times when they are expected the result is an
application more acceptable to an interactive end-user.  I don't know
whether that applies to the OP's situation, and neither do you.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Smith
12-24-04 09:17 AM


Re: Clearing memory from instantiated objects
"Tony Morris" <not@telling.you> wrote in message
news:RVLyd.86570$K7.35508@news-server.bigpond.net.au...
>
> It doesn't.
> It makes a suggestion to run the garbage collector.
> This seldom occurs in practice - that is, your suggestion is seldom
honoured
> by the gc.

I used to hear this term "suggestion" in C++ by people describing how
"inline" functions work.  It's wrong.  It's not a suggestion.  It's a
request.

> To call gc is general poor form - an attempt to deviate from the already
> complicated algorithms used by the garbage collector undermines the value
> provided at best.

If it "undermined the value provided at best", then the function would never
have been provided in the first place.  It's there for a reason.



Report this thread to moderator Post Follow-up to this message
Old Post
jeffc
12-24-04 09:17 AM


Re: Clearing memory from instantiated objects
Tony Morris wrote:
> "ByteCoder" <ByteCoder@127.0.0.1> wrote in message
> news:331eb2F3sa8pgU1@individual.net...
> 
>
>
> This is not even close to true.

Ok, I should have used proper jargon. If an Object can't used anymore by
any other Object the Garbage Collector will remove it from memory when
it runs.
 
>
> is.
>
>
> It doesn't.
> It makes a suggestion to run the garbage collector.
> This seldom occurs in practice - that is, your suggestion is seldom honour
ed
> by the gc.
> To call gc is general poor form - an attempt to deviate from the already
> complicated algorithms used by the garbage collector undermines the value
> provided at best.

Like I said, it is very rare that the programmer has a need to call the
GC, but if it is called, it *will* execute. How do I know? netBeans has
a memory toolbar. If I click on it, it will always run the GC.

--
-------------
- ByteCoder -           ...I see stupid people
-------------
Curiosity *Skilled* the cat

Report this thread to moderator Post Follow-up to this message
Old Post
ByteCoder
12-24-04 02:06 PM


Re: Clearing memory from instantiated objects
mmm_moo_cows@hotmail.com wrote:
> Hi,
>
> Is it possible to remove a specific object (instantiated) whilst the
> running a program.  I'm writing a highly repeatative task and dont want
> to suck the systems memory having old information stored in the memory.
> Thanks for any help, Jon
>

All of the other posts have provided a good description of the GC in
java, so I won't add anything to it.

However,  a suggestion that often helps for this sinerio is to treat
Java like any other language, when performance is an issue. So....

1) Is there actually a performance problem? Write the code, then test it
with a profiler to see if it is not the best performance.


If and only if you know the code is badly performing (because of some
testing) then attempt some/ all of the following:

2) Don't create objects if necessary, its 'generally' quicker to reuse
an existing object than to create new ones.

3) Ensure objects are de-referenced as soon as possible, to allow the GC
to remove as many objects as possible, as soon as possible.

4) Employ the relevant patterns that aid memory consumption - flyweight,
etc..

5) Look for silly yet problematic issues like - new-ing Boolean objects,
instead of using Boolean.valueof(boolean b);

http://java.sun.com/j2se/1.4.2/docs...an.html#valueOf(boole
an)


6) ....for others to fit in.

Failing all of that, post your code and its unit test code if available,
and we can all have a go at optimizing it for you.

HTH

Andrew

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew McDonagh
12-25-04 01:56 AM


Re: Clearing memory from instantiated objects


"jeffc" <nobody@nowhere.com> wrote in message
news:OyNyd.81$aM4.37529@twister.southeast.rr.com...
>
> "Tony Morris" <not@telling.you> wrote in message
> news:RVLyd.86570$K7.35508@news-server.bigpond.net.au... 
> honoured 
>
> I used to hear this term "suggestion" in C++ by people describing how
> "inline" functions work.  It's wrong.  It's not a suggestion.  It's a
> request.

You can take that argument up with the specification:
"Calling the gc method suggests that the Java Virtual Machine expend effort
toward recycling unused objects in order to make the memory they currently
occupy available for quick reuse."
 
value 
>
> If it "undermined the value provided at best", then the function would
never
> have been provided in the first place.  It's there for a reason.
>

This is not my opinion - it's a well documented fact.
There are a lot of core APIs that are there for the wrong reason - this is
one of thousands.

--
Tony Morris
http://xdweb.net/~dibblego/




Report this thread to moderator Post Follow-up to this message
Old Post
Tony Morris
12-26-04 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Pages (3): [1] 2 3 »
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:29 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.