• Post Reply Bookmark Topic Watch Topic
  • New Topic

static objects  RSS feed

 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi this time I,m with some differnet type of confusion.
as we know that garbage collector in java wroks only with those memory which are allocated by which are allocated by new() in heap;
but we also know that static data get s their allcation in static storage independently regrdless to objects.
so the prblem is that how this type of memory get freed???
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi satyam, welcome to javaranch

I think static data will only be garbage collected when the related class is unloaded. This is something new to me so I did some research. As you might know, JVM classloaders are responsible for loading of classes. But they don't support unloading of classes as given in this bug report. So for that you'll have to implement your own class loaders. I found this discussion also very interesting if you want to give it a read...
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or simply set the reference to null.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the object to null is a solution I accept it, but that is like memory management manually like in C++ etc . But at least this is a simpler solution than writing a class loader ...
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS FOR SUPPORT FRIENDS.....
BUT THE ACTUAL PROBLEM IS STILL THERE
ALL YOU GUS HAVE TOLD ME TO MANUAL METHOD TO DEALLOCATE, BUT USUALLY WE DON'T TRY THESE THINGS.
IN A SIMPLE EXAMPLE HOW JAVA COMPILER MAKE FREE THESE MEMORIES(ITSELF)???
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote:... static data get s their allcation in static storage independently regrdless to objects....


What do you mean by 'static data' ?

I suspect you mean static references. If so then any object pointed to be a static reference is no different to an object pointed to by a non-static reference. In fact an object can have both static and non-static references to it at the same time.
An object is only eligible for garbage collection when there are no valid references to it. As Ankit mentioned earlier, if you have a static reference to an object that reference will only be lost when the class is unloaded, and as this is fairly unlikely to ever happen, the only other way to get that object GC'ed is to set the static reference to null or to point it at a different object.

And KeepItDown
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joanne Neal.

class hi{
static int i=5;
}

i,m telling about this type of static data. as we know this can be accessed without any object, means memory is allocated for this data 'i'.then how this memory will be free.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote:
i,m telling about this type of static data. as we know this can be accessed without any object, means memory is allocated for this data 'i'.then how this memory will be free.


Well, since static variables are available to the application for the lifetime of the application, objects that they refer to can't be freed (assuming that you don't null out the static variable), as there is no way for the GC to determine that it is no longer neccesary. (and regardless, primitive variables are not GC'ed)

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote:
i,m telling about this type of static data....


And BTW, the term "static data" has a specific meaning in computer science. It means data that doesn't change during a specified scope... be careful when using this term.

Henry
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry
And BTW, the term "static data" has a specific meaning in computer science. It means data that doesn't change during a specified scope... be careful when using this term.

thanks friends for making me understand the definition of static data. but we are again at square one.
we haven't get appropriate answer
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote: thanks friends for making me understand the definition of static data. but we are again at square one.
we haven't get appropriate answer


Well, the first response was "the memory will be freed when you null out the reference".... Your response was "You don't want to manually null out the reference, so what happens to the memory?".

The second response was "if you don't null out the reference, memory won't be freed".... Your response was "We are back at square one".


What do you want us to say? ... Lie to you that there is a magical feature in Java, that can determine when references that still points to objects in memory, will no longer be used, and free them?

Henry
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want us to say? ... Lie to you that there is a magical feature in Java, that can determine when references that still points to objects in memory, will no longer be used, and free them?


I think I have failed to explain the problem?
i,m telling again..
like in this small pgm

so by now the memory allocated to 'hi.i' will be freed??? and normally i have never seen and any explicit method used in any book for deallocation of such static data. so i think JAVA should have any feature to check it out. As in JAVA we do much for heap which is actually very large in comparison to static storage. So if we have to make explicit method for such deallocation then static storage will face crisis.
so there must be any method for such treatment. and as it is very clear i have not made any objects so there is no confusion regarding so called "REFERENCE".
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Memory for static (primative) variables are *never* freed by the application, and are accessible during the lifetime of the application.

The memory will be eventually be freed, only after the Java program exits, and the operating system free all resources related to the application.


BTW, as a side note, I believe (older) JVMs used to unload Java classes, when they are no longer in use (which did free the static variables). But this created confusion, with just when a class is no longer necessary, so the normal class loader no longer unload classes, hence, the memory is never freed.

Henry
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The memory will be eventually be freed, only after the Java program exits, and the operating system free all resources related to the application.


if it is true then objects can be also freed by similar process then why is so trouble about garbage collector.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote:
if it is true then objects can be also freed by similar process then why is so trouble about garbage collector.


There is a *big* difference between classes and instances of classes. For any particular class, there is only one Class object, but you can have hundreds or thousands of instances of that class being created and thrown away every minute.

If you don't GC the instances, your program can run out of memory really fast. And while exiting your program may get the memory back, it is generally not a viable option. On the other hand, if you don't free the classes themselves... unless you have a program that generate classes on the fly, it is unlikely that you will run out of memory.... so, yea, using the default class loader, classes are not freed.

Henry
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam mishra wrote:but we also know that static data get s their allcation in static storage independently regrdless to objects.


All objects (objects of type java.lang.Object or any of its sub-type) live only in heap memory because you always have to use the 'new' operator to create them, regardless of the fact if they are static or not. I guess that resolves your dilemma.
 
satyam mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any particular class, there is only one Class object, but you can have hundreds or thousands of instances of that class being created and thrown away every minute.


Then if we consider this simple program


then as for as i think here we have got 2 objects.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!