How gc works in this scenario ?
Any one please tell How the garbage collection happen in the following scenario
class MyClass
{
public MyClass()
{
.......
// Doing some database operation.
}
publicvoid finalize()throws Throwable
{
MyClass a =new MyClass();
}
}
If we are creating an instance of MyClass, and at one moment of time
that object is out of scope. Then how the garbage collection will happen in this case?
Thanks in advance
Nikhil.K.R
nikhilkr@huawei.com
[931 byte] By [
nikhilkra] at [2007-11-27 11:55:02]

> Any one please tell How the garbage collection happen
> in the following scenario
Undefined. Maybe not at all.
> If we are creating an instance of MyClass, and at one
> moment of time
> that object is out of scope. Then how the garbage
> collection will happen in this case?
It will happen sometime, create a new instance, and then happen sometime again, creating a new instance.
What do you mean? finalize is just a method, and it won't reference a MyClass instance unless it is executing. The MyClass instance will be collected by the GC (when it needs to)
Kaj
kajbja at 2007-7-29 18:59:25 >

Consider the following situation
public static void main(String args[])
{
MyClass aa = new MyClass();
}
In this case when 'aa' got garbage collected ?
> Consider the following situation
> [code]
> public static void main(String args[])
> {
>MyClass aa = new MyClass();
> ]
> In this case when 'aa' got garbage collected ?
What do you mean? aa isn't even created unless you call the method.
Kaj
kajbja at 2007-7-29 18:59:25 >

u can not predict behavior of garbage collector ,, u can just request it to run ,,by using system.gc (),,, weather to call it or not that will be decided by JVM ,,
u can just make last attempt in finalize () to save u r object.
But in the constructor of MyClass it doing to DB operations, then the object should be create, right?
> But in the constructor of MyClass it doing to DB
> operations, then the object should be create, right?
Eh? What are you actually asking here? Whether the garbage collector can tell what your code wants to do, and not GC an object because you don't want it to?
Why are you creating a new instance in the finalize method? That's just asking for things to behave very erratically
@Op. You have a really bad design. Is there are reason to why you are asking these odd question? I hope that you haven't done anything similar to this in real life (and you shouldn't do database operations in the constructor)
kajbja at 2007-7-29 18:59:25 >

My question is if we create a class like this and run, will it cause a memory leak or not?
MyClass a = new MyClass()
while(true)
{
Thread.sleep(10000);
}
it is infinite loop u r using it will never come out of it
This is not a discussion about designing or checking the brillance of JVM or whether this Class is really reqd or not. Just asking how the gc happens if some one creating an object like this and run it for a long time.
I am not arguing with you people, just asking a doubt.
> This is not a discussion about designing or checking
> the brillance of JVM or whether this Class is really
> reqd or not. Just asking how the gc happens if some
> one creating an object like this and run it for a
> long time.
>
> I am not arguing with you people, just asking a doubt.
The GC will not try to collect object that are reachable.
kaj
kajbja at 2007-7-29 18:59:26 >

i a m telling u again u can not force garbage collector ,
How would an empty loop cause a memory leak?
> I am not arguing with you people, just asking a doubt.
Did it ever occur to you to simply write an example and execute it?
or make u r object ref pointing to null it will be garbage collected
Hi CeciNEstPasUnProgrammeur , pls see the definition of MyClass() in the beginning of this topic.
> My question is if we create a class like this and
> run, will it cause a memory leak or not?
> [code]
>MyClass a = new MyClass()
> while(true)
>{
>Thread.sleep(10000);
>}
the program would not execute because an error will occur
interruptedException no try catch in thread.sleep.
that should answer your question.
> or make u r object ref pointing to null it will be
> garbage collected
Doesn't matter. Either it is reachable when GC runs, or it isn't. No point in nulling non-attribute variables as they run out of scope anyway.
> Hi CeciNEstPasUnProgrammeur , pls see the definition
> of MyClass() in the beginning of this topic.
Okay. Did.
So now I ask: how would an empty loop create a memory leak? You still have a very pointless GC cycle, although now you block it with an endless loop. I don't really see any change to the original code with regards to memory usage/GCing.
This is not a silly example, but a simulation of a class wrote by my collegue.
> This is not a silly example, but a simulation of a
> class wrote by my collegue.
So it's not an example, but still silly.
> or make u r object ref pointing to null it will be
> garbage collected
Not true. If there are no references pointing to an object, it is eligible for garbage collection. There's nothing to say that will actually happen, though. If it's a reference inside a method, it'll fall out of scope when the method returns anyway
> This is not a silly example, but a simulation of a
> class wrote by my collegue.
Tell your collegue to change his desing. It's really bad.
kajbja at 2007-7-29 18:59:30 >

> This is not a silly example, but a simulation of a
> class wrote by my collegue.
Still silly
> Hi CeciNEstPasUnProgrammeur , pls see the definition
> of MyClass() in the beginning of this topic.
>Okay. Did.
>So now I ask: how would an empty loop create a memory leak? You still have >a very pointless GC cycle, although now you block it with an endless loop. I >don't really see any change to the original code with regards to memory >usage/GCing.
Consider a realtime situation, a server application run continously 30 days.
In the application somebody creates an object of this class. This instance is not doing much more and after some time GC found this object can be garbage collected since no reference to it. GC calls the overrided finalize() method. Then what will happen ?
I think you got my question !!!
> > Hi CeciNEstPasUnProgrammeur , pls see the
> definition
> > of MyClass() in the beginning of this topic.
>
> >Okay. Did.
>
> >So now I ask: how would an empty loop create a
> memory leak? You still have >a very pointless GC
> cycle, although now you block it with an endless
> loop. I >don't really see any change to the original
> code with regards to memory >usage/GCing.
>
> Consider a realtime situation, a server application
> run continously 30 days.
> In the application somebody creates an object of this
> class. This instance is not doing much more and after
> some time GC found this object can be garbage
> collected since no reference to it. GC calls the
> overrided finalize() method. Then what will happen ?
> I think you got my question !!!
If finalize in your example is called, a new instance will be created, and then the sole reference to it will fall out of scope and that instance will be eligible for GC again. But this isn't guaranteed to happen, and if you're writing code that depends on it happening, you've got a bad design
I don't think you quite understand what "real time" means, by the way
http://en.wikipedia.org/wiki/Real_time
> Consider a realtime situation, a server application
> run continously 30 days.
> In the application somebody creates an object of this
> class. This instance is not doing much more and after
> some time GC found this object can be garbage
> collected since no reference to it. GC calls the
> overrided finalize() method. Then what will happen ?
> I think you got my question !!!
Have I mentioned that it's a really stupid design? You should really correct that instead of asking this question.
Your object can still be collected since your implementation doesn't create a new reference to the instance.
kajbja at 2007-7-29 18:59:30 >

did u read waht kaj said ? now forget about that problem ,,, improve u r design !!!! and don't run u r server on this design
> Consider a realtime situation,
I hope you mean "real life" because "real time" is something completely different.
> a server application
> run continously 30 days.
> In the application somebody creates an object of this
> class. This instance is not doing much more and after
> some time GC found this object can be garbage
> collected since no reference to it. GC calls the
> overrided finalize() method. Then what will happen ?
> I think you got my question !!!
I also think I already answered it, twice.