10

I want to destroy static object programmatically. Is it possible? If yes then how can i achieve this. Kindly suggest.

Madhav Bhattarai
  • 847
  • 2
  • 10
  • 29

5 Answers5

17

The thing that you need to understand is - the references are static, the objects aren't. By that, I mean to say, in

static SomeClass someClassInstance = new SomeClassInstance();

the static property is on the reference someClassInstance and GC acts on instances / objects.

someClassInstance =null will make the first SomeClassInstance eligible for GC.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Actually iam performing a memory intensive task and i want to clear memory as soon as my task is done. – Madhav Bhattarai Jan 13 '15 at 05:59
  • 6
    @user2749218 - Actually, *explicitly* trying to do a GC *affects* your application's performance. It slows down the application. You should see the priorities of *VM level threads*.. they have priority like 8,9,10 etc..Your app will go to sleep when GC will be running Don't do it. Let the GC decide when to run. – TheLostMind Jan 13 '15 at 06:01
2

If by "destroy" you mean get rid of the object itself, it is pretty easy.

Say your Object is defined in Class ExampleClass as below

public class ExampleClass{
    public static Object toBeDestroyed;
}

You simply need to do something like this

ExampleClass.toBeDestroyed = null;

The first line removes references to the object (assuming nobody else is using it). The garbage collector will call finalize() on the object and free up the memory.

k_g
  • 4,333
  • 2
  • 25
  • 40
  • System.gc() does *not* call the garbage collector. It suggests that GC be run. – Kylar Jan 13 '15 at 05:46
  • If you ever need to call System.gc() - don't. the VM is way smarter than you on determining when GC should be run. – Kylar Jan 13 '15 at 05:48
  • No. Just no. It in NO WAY guarantees that the GC cycles will run, nor does it guarantee that any object will be destroyed. Any use of System.gc() most likely represents a place where your code is *fundamentally broken*. – Kylar Jan 13 '15 at 05:50
  • @Kylar - Either that or you are trying to do some kind of *Micro - benchmarking* – TheLostMind Jan 13 '15 at 05:52
  • @Kylar I will edit my suggestion accordingly. I actually have not had opportunity to use System.gc() in the past and knowing what I know now I will not in the future; thank you for the advice. – k_g Jan 13 '15 at 05:53
0

By definition, a static variable is defined once per class and ( if declared final) has an immutable value ... and cannot be "destroyed".

What are you really trying to do?

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
0

in java I don't think you can destroy a variable, if you really meant to free the memory space then the JVM is the one that collects or frees the memory of the unused variables. This process is called as garbage collection. How to do this? Refer:https://stackoverflow.com/a/1567996/1904479

Community
  • 1
  • 1
Kailas
  • 7,350
  • 3
  • 47
  • 63
0

You cannot destroyed static variable. Everyone can access static variable without declaring class instance. Its a shared memory concept. You can read, if you have enough permission can change.

So, you need to assign null or empty value or something like into that variable. You have to do some tricks on it to maintain or to check if that variable is set or not. You may set a flag to check value existence.

You may get more information from Where are static class variables stored in memory? and static allocation in java - heap, stack and permanent generation

Community
  • 1
  • 1