garbage collector

  • Thread starter timothy ma and constance lee
  • Start date
T

timothy ma and constance lee

Sir/madam

I have this question in my heart. In Java, they always say object use up the
memory. Since all java programming will have object instiantiate like
Date(), timestamp, String etc. Should we need to garbage collect them after
use?
If so, how to do that by batch rather than object bu object?

Thanks
 
A

Andrew Thompson

timothy ma and constance lee wrote:
...
...In Java, they always say object use up the
memory.

That would be true of any language that is OO.
(And for non-OO languages, other things beside
objects use up memory.)
..Since all java programming will have object instiantiate like
Date(), timestamp, String etc. Should we need to garbage collect them after
use?

It depends.

For example, let's look at one program, designed
two different ways. Say you have two programs that
each deal with 10,000,000 records from a DataBase.

The code of both apps. might use a common Java Object
to represent each record, but while one program retains
a single instance of the RecordObject, and simply recycles
it for use with each record, the second instantiates 10,000,000
RecordObject's.

The first app. would probably do just fine, whereas the
second would be likely to crash in the standard memory
assigned to Java apps.

The usual advice for Java applications is not to worry
about it until/unless it becomes a problem, then use
a profiler to determine the source of the problem.
If so, how to do that by batch rather than object bu object?

To my knowledge, there is no way to ensure no more
references to an object 'by batch'. Of course, you can
call GC, and any objects with no further references should
be disposed, but I suspect you are referring to something
else.
 
J

Joshua Cranmer

timothy said:
Sir/madam

I have this question in my heart. In Java, they always say object use up the
memory. Since all java programming will have object instiantiate like
Date(), timestamp, String etc. Should we need to garbage collect them after
use?
If so, how to do that by batch rather than object bu object?

Java garbage collection is handled automatically for you. If you want
objects to be garbage collected, then drop all of the references to the
object, and it will be collected on the next run.
 
L

Lew

Joshua said:
Java garbage collection is handled automatically for you. If you want
objects to be garbage collected, then drop all of the references to the
object, and it will be collected on the next run.

Well, maybe not the very next run, depending on what you mean by "next run".
There are two flavors of GC in the standard Sun default implementation, and it
could be that the object is not collected in a minor GC and will have to wait
for a major GC.

But that doesn't diminish Joshua's point, which is that you don't collect
objects, the JVM does. You just have to make sure nothing refers to them any
more, as Joshua indicated.

The Sun page
<http://java.sun.com/javase/technologies/hotspot/gc/index.jsp>
has a bunch of information that you should study.

It's often a good idea in Javaland to search, in order, Sun, Google and IBM
for information. It didn't take me too long to find the link I posted, given
that I didn't have it bookmarked and needed to hunt it down on Sun's Java
site. It shouldn't have taken you much longer.

Basically, you navigate to
<http://java.sun.com/>
Find the link on the right that says, "Technologies: Java SE"
<http://java.sun.com/javase/>
Click on the "Technologies" tab.
Since GC is a JVM issue, look for the header "Java Virtual Machine". It
sports one link: "HotSpot VM"
<http://java.sun.com/javase/technologies/hotspot/index.jsp>
Look down the page, and hey, presto! There's a link for "HotSpot Garbage
Collection" (/ibid./).
 
C

Curt Welch

timothy ma and constance lee said:
Sir/madam

I have this question in my heart.

Other people answered your question, but I wonder if they were assuming you
knew more than you do. So let me make a few points clear in case you don't
understant the basics of garbage collection.
In Java, they always say object use up
the memory. Since all java programming will have object instiantiate like
Date(), timestamp, String etc. Should we need to garbage collect them
after use?

No. It's all automatic. You don't have to worry about that.

if you do something like:

for (int i=0; i < 1000000; i++) {
Date d = new Date();
// do stuff with date
}

It will create a million Date objects for you, but each Date object is lost
at the end of each iteration of the for() loop, and as such, they are
available for the garbage collector to free them. The garbage collector
runs automaitcally when it's needed so you just don't worry about it.

However, the better way to write that is something like this (as someone
else already suggested to you):

Date d = new Date();
for (int i=0; i < 1000000; i++) {
// keep reusing the one Date object for each loop
}

Then you only use up the memory for one Date object and only one Date
object has to be cleaned up after your method terminates. So as a general
rule, it's better if you don't create lots of objects inside large loops if
you don't need to. But at the same time, Java object creation and cleanup
is so fast, you can normally get away with doing it the bad way.
If so, how to do that by batch rather than object bu object?

The garbage collector will clean up your dead objects for you at some point
in the future. You normally never have to worry about it. That's the
beauty of using a gc based language like Java.

You can force the garbage collector to run by calling System.gc() at any
point, but you never need to do that unless you have very special
requirements. It runs automatically when it's needed.

If you have data you don't need anymore, you can drop your access to it by
setting the object to null. So for example:

int a[] = new int[10000000];

Uses up a large chunk of memory which won't be freed as long as you can
access the variable a. But if you are done with the data, you can drop your
access to the array by doing this:

a = null;

At that point, all the memory used to hold the large array is available for
the gc() to reclaim when it needs more memory.

Normally however, you don't even do that, instead, you just make sure the
life of the variable matches the programs need for the data. If you only
need that large data for the life of a single method, then you make it a
local variable in the method. If you only needed it for the life of a
given object, you make it an instance variable in the object.

When you might find a need for that is for class variables (static). They
stay around for the life of your program, and if there's some point in the
program you don't need them anymore, you can make the memory available for
other use just by setting it to null like above.

As someone else said, the normal practice is to not worry about it until it
becomes a problem. If you are used to programing in other languages where
you have to carefully alloc and free all your memory and spend all your
life hoping you haven't created a memory leak, it's hard at first to get
used to ignoring it. But once you get used to the beauty of not having to
worry about memory, you will never want to go back.
 
L

Lew

(Curt Welch) said:
However, the better way to write that is something like this (as someone
else already suggested to you):

Date d = new Date();
for (int i=0; i < 1000000; i++) {
// keep reusing the one Date object for each loop
}

Why is that better than declaring the Date inside the loop?

Doesn't it force the object into the tenured generation, actually hurting
memory usage?
 
J

Joshua Cranmer

Lew said:
Why is that better than declaring the Date inside the loop?

Doesn't it force the object into the tenured generation, actually
hurting memory usage?
But the object is only being created once rather than one million times.
I suppose it is ultimately slightly worse on the memory usage, but the
garbage collection procedure will be triggered fewer times. Inside the
loop, it is likely to have a slight increase on memory--there are fewer
uncollected fragments on the heap--but it will properly degrade memory
somewhat outside of the loop (expanded scope, etc.). [*]

[*] Note: Well, the reference for initialization inside the loop would
probably not be lost until a new variable had been declared following
the loop, due to the Java VM constraints. If that is true, then the
scope of better performance is slightly expanded.
 
R

Roedy Green

Why is that better than declaring the Date inside the loop?

Doesn't it force the object into the tenured generation, actually hurting
memory usage?

With new Date inside the loop you get 1000000 Date objects created and
gced. With it outside you have only one.

If you are worried about the Date object persisting after the loop,
do a d=null aftter the loop.
 
L

Lew

Joshua said:
But the object is only being created once rather than one million times.
I suppose it is ultimately slightly worse on the memory usage, but the
garbage collection procedure will be triggered fewer times. Inside the
loop, it is likely to have a slight increase on memory--there are fewer
uncollected fragments on the heap--but it will properly degrade memory
somewhat outside of the loop (expanded scope, etc.). [*]

It's that "expanded scope" that worries me. You have to make sure that the
Date instance is properly cleared between instances, for example, perhaps as
part of reinitialization. You now have a variable visible outside its
intended range, which in some applications can become an issue. Memory
allocation in Java is blindingly fast, and young-generation GCs don't take all
that long.

Another consideration is that long-lived mutable objects present threading
challenges. Alas, Date is not immutable, but if one had a better example than
Date one might find the benefits of an immutable object outweigh the downside
of reinstantiation inside the loop, in the face of multi-threaded considerations.

I'm not making the case that having the variable inside the loop will be
faster, only that it is not a sure thing that all types will benefit from
re-use of an instance in lieu of re-allocation. There are many scenarios
where the overhead of managing a long-lived, wide-scoped mutable object will
be worse than the overhead of newly instantiating a short-lived, limited-scope
immutable object.
 
R

Roedy Green

Memory
allocation in Java is blindingly fast, and young-generation GCs don't take all
that long.

The allocation is fairly quick, but you still have to initialise the
object. This means zeroing the whole thing, then filling in any
fields that initialise to something other than 0. (I wonder how long
before CPUs have single instruction to do the zeroing efficiently.)
Further some classes have some fairly extensive computation in the
initialization methods.

Note how JTable uses but one object to render all the cells. Sun
would not have done such a peculiar thing if allocating a component
were almost free.
 
L

Lew

Roedy said:
The allocation is fairly quick, but you still have to initialise the
object. This means zeroing the whole thing, then filling in any
fields that initialise to something other than 0. (I wonder how long
before CPUs have single instruction to do the zeroing efficiently.)
Further some classes have some fairly extensive computation in the
initialization methods.

Which initializing is just as necessary and sometimes harder to keep safe with
an object that is reused after having been dirtied.
Note how JTable uses but one object to render all the cells. Sun
would not have done such a peculiar thing if allocating a component
were almost free.

One example of where a paradigm doesn't apply doesn't imply that it never applies.

The point is that it isn't always true that re-use of an object is better than
re-instantiation.

Furthermore, your point isn't proven because Sun has done many peculiar things
with Java that aren't the best. Making java.util.Date mutable is one.

Further furthermore, while allocation may have been difficult in Java 1.1 when
they were inventing JTable, it might not be such the problem any more in Java 6.

Further further furthermore, allocation cost might not have been involved in
the decision at all - perhaps scope or algorithm concerns were what prevailed
there. Do you have evidence for the factors that went into that decision?
 
N

nebulous99

Further further furthermore, allocation cost might not have been involved in
the decision at all - perhaps scope or algorithm concerns were what prevailed
there. Do you have evidence for the factors that went into that decision?

One more thing that seems relevant here: "Premature optimization is
the root of all evil"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top