简体   繁体   English

Java:垃圾收集器收集什么?

[英]Java : What is Garbage Collector collecting?

I am new to GC and wondering what does garbage collector collect besides reference that are no longer being referencing by any variables ? 我是GC的新手,我想知道垃圾回收器除了不再被任何变量引用的引用之外还收集什么? The following is the list that I want to check 以下是我要检查的列表

  • Do primitive types (int, double, float, char....) get GC-ed ? 基本类型(int,double,float,char ....)是否得到GC-ed?
  • Do static variables get GC-ed ? 静态变量会被GC编辑吗?
  • Do final variables get GC-ed (I think since it's marked as immutable, so there's nothing to collect) ? 最终变量是否经过GC检验(我认为,因为它被标记为不可变的,所以没有什么可收集的)?
  • Do all the methods (both static and non-static) get GC-ed ? 是否所有方法(静态方法和非静态方法)都经过GC处理?
  • Do threads get GC-ed ? 线程是否经过GC编辑?

GC doesn't collect any references, it just frees the objects (memory on heap) which are no more reachable. GC不会收集任何引用,它只是释放不再可访问的对象(堆上的内存)。

Static is special memory location and associated with class/classloader. 静态是特殊的内存位置,并且与类/类加载器关联。 If class/classloader un-deployed then static content will be removed from memory. 如果未部署class / classloader,则静态内容将从内存中删除。

Primitive types if associated with object (class variables), then they will be GCed when object is not reachable. 如果原始类型与对象(类变量)相关联,那么当对象不可访问时,它们将被GC化。

If local variables/param variables, they will be on stack, so as soon as method execution completed, they are reclaimed. 如果是局部变量/参数变量,则它们将在堆栈上,因此一旦方法执行完成,便将其回收。

Only objects (instances) can get garbage collected, nothing else. 只有对象(实例)才能收集垃圾,而没有其他东西。

  • Variables are not objects - so they don't get gc'd 变量不是对象-因此它们不会被gc'd
  • Methods are not objects - no gc. 方法不是对象-没有gc。
  • primitives - not objects, no gc 原语-不是对象,没有gc
  • Threads - the Thread class instances: yes. 线程-线程类实例:是的。

The keyword static has nothing to do with garbage collection. 关键字static与垃圾回收无关。

Garbage collection is only for Objects. 垃圾收集仅适用于对象。

•Do primitive types (int, double, float, char....) get GC-ed ? •原始类型(int,double,float,char ....)是否获得GC-ed? - are primitive types objects? -是原始类型对象吗? no. 没有。

•Do static variables get GC-ed ? •静态变量是否得到GC检验? - 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) ? •最终变量是否经过GC检验(我认为由于将其标记为不可变的,因此没有什么可收集的)? - no they will get GC-ed -不,他们将接受GC培训

•Do all the methods (both static and non-static) get GC-ed ? •是否所有方法(静态和非静态)都经过GC检验? - 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. -方法和类defs会占用内存,但不会占用由GC管理的内存,这取决于最终将随意创建和销毁的JVM类型。

•Do threads get GC-ed ? •线程是否经过GC编辑? - yes threads are objects, so they get GC-ed. -是的,线程是对象,因此它们经过GC处理。

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 ? 基本类型(int,double,float,char ....)是否得到GC-ed? They get GC-ed if the object they belong to gets GC-ed 如果它们所属的对象被GC检验,则它们被GC检验。

  • Do static variables get GC-ed ? 静态变量会被GC编辑吗? No 没有

  • Do final variables get GC-ed (I think since it's marked as immutable, so there's nothing to collect) ? 最终变量是否经过GC检验(我认为,因为它被标记为不可变的,所以没有什么可收集的)? They get GC-ed if the object they belong to gets GC-ed 如果它们所属的对象被GC检验,则它们被GC检验。

  • Do all the methods (both static and non-static) get GC-ed ? 是否所有方法(静态方法和非静态方法)都经过GC处理? This doesn't make so much sense 这没有多大意义

  • Do threads get GC-ed ? 线程是否经过GC编辑? If they are object that are not referenced anymore, yes 如果它们是不再引用的对象,是

Garbage collection does work on objects allocated on heap, which are all the objects that are created through new . 垃圾回收确实对在堆上分配的对象起作用,这些对象是通过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 不确定原始对象,如果在内部使用对象(例如, new Integer(..)管理,则可以对它们进行GC处理,但是我不确定此处是否装箱和装箱,所以我说不,因为JVM有特定的指令来管理它们
  • static variables can be GC-ed since the only thing static is the reference but not the referred object 静态变量可以进行GC编辑,因为唯一静态的是引用,但不是引用的对象
  • 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 final变量可以是GC-ed,它们是final的事实并不意味着您的程序将永远需要对其进行引用
  • 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 方法和线程本身并不进行GC,但从这个意义上讲,线程始终包含在具有run()方法的对象中,因此可以对其进行GC

GC only collect memory spaces that have not any references in program so by definition collecting primitives by GC can not be occur. GC仅收集程序中没有任何引用的内存空间,因此按定义收集GC不会发生。 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. 我无法想象您用GC-ed方法表示什么。在一般的回答中,我应该说方法不是要进行GC-ed的对象状态的一部分。

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 . 有一关于垃圾收集手册的 好书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM