简体   繁体   English

垃圾收集Java中的静态变量

[英]Garbage collecting static variables in Java

I am running one Java process in Unix machine. 我在Unix机器上运行一个Java进程。 If I want to stop the application, I am simply killing the process using shell script. 如果要停止该应用程序,我只是使用shell脚本终止该进程。

Here is my question: 这是我的问题:

How static variables are garbage collected after killing the process ? 杀死进程后如何收集静态变量?

Do i require to set each static variable to null to claim back the memory or JVM itself do immediately ? 我是否需要将每个静态变量设置为null才能立即收回内存或JVM本身?

If you are on a unix box, and force a thread dump (kill -3), it should force garbage collection. 如果您在Unix机器上,并强制执行线程转储(kill -3),则应强制进行垃圾回收。

Static variables are referenced by Class objects which are referenced by ClassLoaders -so, when the classloders become elligible for collection, your static variables will follow suit. 静态变量由Class对象引用,而Class对象由ClassLoaders引用-因此,当分类器变得易于收集时,您的静态变量将随之效仿。

Assigning null to an object or System.gc() doesn't start garbage collection, they just signal eligibility for garbage collection. 将null分配给对象或System.gc()不会启动垃圾回收,它们只是发出垃圾回收资格的信号。 Killing the process returns the entire java heap to the system. 终止进程会将整个java堆返回给系统。

When a process is killed, the entire java heap is returned to the system; 当一个进程被杀死时,整个java堆将返回到系统。 there is no need for the JVM to garbage collect at this point. 此时,JVM无需进行垃圾收集。

Java will itself do the garbage collection. Java本身将进行垃圾收集。 Even if you set it to null you cannot force the JVM to do the garbage collection. 即使将其设置为null,也不能强制JVM进行垃圾回收。

Java application is run over JVM which is an other application which runs over OS. Java应用程序在JVM上运行,而JVM是在OS上运行的另一个应用程序。 Killing an application means killing JVM itself,after than OS will deallocate the memory occupied by JVM, in other words java application. 杀死应用程序意味着杀死JVM本身,然后OS将释放JVM占用的内存,即Java应用程序。

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

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