0

I am new to GC and wondering what does garbage collector collect besides reference that are no longer being referencing by any variables ? The following is the list that I want to check

  • Do primitive types (int, double, float, char....) get GC-ed ?
  • Do static variables get GC-ed ?
  • Do final variables get GC-ed (I think since it's marked as immutable, so there's nothing to collect) ?
  • Do all the methods (both static and non-static) get GC-ed ?
  • Do threads get GC-ed ?
peter
  • 8,333
  • 17
  • 71
  • 94
  • 4
    You could've broken your question down into the 5 bullet points you've written and found several Q&As on each one already on S.O but what you probably want to read is this http://chaoticjava.com/posts/how-does-garbage-collection-work/ – Ozzy Aug 07 '12 at 21:04

7 Answers7

3

GC doesn't collect any references, it just frees the objects (memory on heap) which are no more reachable.

Static is special memory location and associated with class/classloader. If class/classloader un-deployed then static content will be removed from memory.

Primitive types if associated with object (class variables), then they will be GCed when object is not reachable.

If local variables/param variables, they will be on stack, so as soon as method execution completed, they are reclaimed.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • local variables are reclaimed when the method returns. i.e. primitives and references. Only Objects are GC-ed. – Peter Lawrey Aug 07 '12 at 21:28
  • The *variables* are static, not the referenced objects. Otherwise an object would have to change it's "type" if it was re-assigned from a non-static to a static variable. (it can be assigned to static, non-static and volatile variables at the same time, btw) – Andreas Dolk Aug 07 '12 at 21:31
0

Only objects (instances) can get garbage collected, nothing else.

  • Variables are not objects - so they don't get gc'd
  • Methods are not objects - no gc.
  • primitives - not objects, no gc
  • Threads - the Thread class instances: yes.

The keyword static has nothing to do with garbage collection.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • What about object with specific keyword 'static' ? and what about thread ? because we can declare thread as an object and execute it. – peter Aug 07 '12 at 21:04
  • GC collects objects that are not referenced any more.A running thread is not referenced by anyone yet it is not GCed. – Cratylus Aug 07 '12 at 21:18
  • What about after it completes the task. Will it get GCed ? – peter Aug 07 '12 at 21:21
  • The *Thread instance* will be gc'd exactly like all other objects (unless it represents a daemon thread) – Andreas Dolk Aug 07 '12 at 21:25
0

Garbage collection is only for Objects.

•Do primitive types (int, double, float, char....) get GC-ed ? - are primitive types objects? no.

•Do static variables get GC-ed ? - are the variables objects, yes.

•Do final variables get GC-ed (I think since it's marked as immutable, so there's nothing to collect) ? - no they will get GC-ed

•Do all the methods (both static and non-static) get GC-ed ? - methods and class defs take memory but not the memory managed by GC, its on the type of JVM that they are eventually created and destroyed at will.

•Do threads get GC-ed ? - yes threads are objects, so they get GC-ed.

SriN
  • 54
  • 8
0

The garbage collector works only on the heap. Given this you can exclude the static vars.

  • Do primitive types (int, double, float, char....) get GC-ed ? They get GC-ed if the object they belong to gets GC-ed

  • Do static variables get GC-ed ? No

  • Do final variables get GC-ed (I think since it's marked as immutable, so there's nothing to collect) ? They get GC-ed if the object they belong to gets GC-ed

  • Do all the methods (both static and non-static) get GC-ed ? This doesn't make so much sense

  • Do threads get GC-ed ? If they are object that are not referenced anymore, yes

Razvan
  • 9,925
  • 6
  • 38
  • 51
0

Garbage collection does work on objects allocated on heap, which are all the objects that are created through new.

  • not sure about primitive objects, they could be GCed if internally managed with objects (eg new Integer(..) but I'm not sure about boxing and unboxing here so I'd say no since JVM has specific instructions to manage them
  • static variables can be GC-ed since the only thing static is the reference but not the referred object
  • final variables can be GC-ed, the fact that they are final doesn't mean that your program will require a reference to them forever
  • methods and threads are not GCed by themselves but in this sense a thread is always contained in an object which has a run() method so they can be GCed
Jack
  • 131,802
  • 30
  • 241
  • 343
0

GC only collect memory spaces that have not any references in program so by definition collecting primitives by GC can not be occur. Any variable that can reference to an object and can be change its demand can be collected so static variables can be collected in some situation but finals no! I can't imagine what do you mean by method GC-ed.in a general answer i should say method are not part of Object state to be GC-ed.

CoderInNetwork
  • 2,923
  • 4
  • 22
  • 39
0

Garbage collection is a huge topic, probably too large for a stack overflow answer. There is a good book on the subject called the Garbage Collection Handbook.

Michael Barker
  • 14,153
  • 4
  • 48
  • 55