What happens to objects when you redeclare a new object?

... and is there a more proper way of "deleting" them from memory, or is it a case of waiting until the garbage collector in java sweeps them up?

i.e.

private CustomObjectType myObject;

public final void myMethod() {

myObject = new CustomObjectType("I am the first object");

// do some temporary work with myObject

/// now I need to do more work with another temporary object, so just replace the "link"

myObject = new CustomObjectType("I am a second object");

}

Is it better to declare new variables to hold this second object?

Should a person declare the object to be null when no longer required?

Do I ever need to worry about this, does the garbage collector sort out references for me?

(Please go easy, still learning and don't really need to know this right now but I'm curious! :)

[868 byte] By [kimberly22a] at [2007-11-27 0:00:23]
# 1

> ... and is there a more proper way of "deleting" them

> from memory, or is it a case of waiting until the

> garbage collector in java sweeps them up?

Correct

> Is it better to declare new variables to hold this

> second object?

No

> Should a person declare the object to be null when no

> longer required?

No, not if it's in a method.

> Do I ever need to worry about this, does the garbage

> collector sort out references for me?

Yes

Kaj

kajbja at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 2

If you no longer need an object instance, you can get rid of all the references that point to that instance by either setting the references to null or making it point to some other instance. Now this doesn't guarantee that the gc is going to gc it immediately. So you can specifically call the gc using this

System.gc();

I haven't run out of memory till date while trying to run an application, but i have seen a lot of people complain about running out of memory in this forum.

qUesT_foR_knOwLeDgea at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 3
OK, i'm glad i posted this since it's something I've been wondering for a while but been reluctant to ask in case it's an obvious question!!Thanks all.
kimberly22a at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 4
> So you can specifically call the gc using this> > > > System.gc();> > There is no guaranty that the GC will run even then! Generally you don't need this method call!-Puce
Pucea at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 5

> If you no longer need an object instance, you can get

> rid of all the references that point to that instance

> by either setting the references to null

That's not a good advice.

http://www-128.ibm.com/developerworks/library/j-jtp01274.html

Read the section on "Explicit nulling" and why it is bad.

> or making it

> point to some other instance. Now this doesn't

> guarantee that the gc is going to gc it immediately.

> So you can specifically call the gc using this

>

> >

> System.gc();

>

>

Also a very bad advice. It will not force the gc to execute and it might kill your performance.

Kaj

kajbja at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 6
> but i have seen a lot of people complain about running out of memory in this forum.When you get down to the bottom of their complaints, it's always because they'redoing something silly, like trying to hold multi-gigabyte files in memory.
DrLaszloJamfa at 2007-7-11 15:50:24 > top of Java-index,Java Essentials,Java Programming...
# 7

>What happens to objects when you redeclare a new object?

If you go:

Dog myDog = new Dog();

and then go:

Dog myDog = new Dog();

you are replacing the first reference to Dog() with another one. I don't think it would make sense to do this because it is redundant.

But if you go:

static Dog myDog = new Dog();

and then go:

static Dog myDog = new Dog();

then then second one will be ignored because the first one already assigned Dog() to myDog.. so the second one won't replace the first one - it will just be ignored or generate an error.

>is there a more proper way of "deleting" them from memory, or is it a case of waiting until the garbage collector in java sweeps them up?

In c and c++ you have to think about when the life of an object ends and destroy the object to prevent a memory leak. But in Java the garbage collector takes this task on if a certain amount of memory is used. If you don't use a lot of memory the gc won't bother and the objects will be destroyed when you exit the program.

You can use:

finalize(){

// insert code to be executed before the gc cleans up

}

and if you call System.gc() (which you probably won't need to do) then the code in the finalize() method will run first e.g. to erase a picture from the screen before collecting the object.

>private CustomObjectType myObject;

public final void myMethod() {

myObject = new CustomObjectType("I am the first object");

// do some temporary work with myObject

/// now I need to do more work with another temporary object, so just replace the "link"

myObject = new CustomObjectType("I am a second object");

you could do:

public class CustomObjectType{

//this constructs an instance of the class using a string of your choice

CustomObjectType(String str) {

System.out.println(str);

}

static void main(String[] args){

CustomObjectType myObject = new CustomObjectType("I am the first object");// This sends the constructor the string you want it to print

CustomObjectType myObject2 = new CustomObjectType("I am the second object");// This sends the constructor the string you want it to print

}

}

Bruce eckel wrote Thinking in Java 4th edition considered to be the best book on Java because of how much depth he goes into, although some recommend you should have atleast basic programming knowledge and a committment to learn to get through the book.

I just started it and it helps a lot. Maybe u could borrow it from the library.. good luck!

brettosm8a at 2007-7-11 15:50:25 > top of Java-index,Java Essentials,Java Programming...
# 8
> I haven't run out of memory till date while trying to> run an application, but i have seen a lot of people> complain about running out of memory in this forum.Invoking gc() will not help that, as the GC will run on its own before throwing OOME anyway.
jverda at 2007-7-11 15:50:25 > top of Java-index,Java Essentials,Java Programming...
# 9

> If you go:

>

> Dog myDog = new Dog();

>

> and then go:

>

> Dog myDog = new Dog();

...it won't compile.

> But if you go:

>

> static Dog myDog = new Dog();

>

> and then go:

>

> static Dog myDog = new Dog();

...it won't compile.

> then then second one will be ignored

No, that will never happen. If you remove the second declaration so that you just have

Dog dog = new Dog();

dog = new Dog();

OR

static Dog dog = new Dog();

dog = new Dog():

then in both cases, the second dog = line will point the reference to a different Dog object, leaving the first one eligible for GC.

> You can use:

>

> finalize(){

> // insert code to be executed before the gc cleans

> up

But don't.

> and if you call System.gc() (which you probably won't

> need to do) then the code in the finalize() method

> will run first e.g. to erase a picture from the

> screen before collecting the object.

Finalize will be called if and when the object is GCed, which may never happen. You can never guarantee that any specific object will be GCed.

jverda at 2007-7-11 15:50:25 > top of Java-index,Java Essentials,Java Programming...