java core technology Chapter IV doubt point

1, java the finalize () method

Java assumed finalize works as follows: Once the garbage collector ready to reclaim the memory occupied by the object is released when the memory, will first call finalize method of the object, and then again the next garbage collection when it needs to truly recover objects. Role finalize () of: finalize occurred prior to GC before calling a special memory to reclaim JNI call in the application, the next time GC occurs after GC to ensure all memory of the object are released.

Then there will be a question. java in itself, there is garbage collection. Why does it need to finalize () method yet. the reason is. Java garbage collection for recycling only for garbage objects in the heap. That is to say, jvm only responsible for our new out of the heap memory block. For those who are not new when out, but through jni method to call the method (usually c / c ++) and non-heap memory called non-realization of java. jvm garbage collection process can not do it. This memory consumption is therefore not recovered. Thus java introduced finalize () method.

So now we can obviously know. finalize () method to deal with less memory consumption management jvm born. General release for the special memory (memory jvm can not handle). Therefore, if a class defines a native method. It produced some non-heap memory consumption. At this time, before considering finalize () method to release the non-heap memory.

 

() With different finalize destructor

  -Java because GC mechanism, there is no concept of a destructor!

  -finalize not in C ++ destructor: destructor will definitely destroy the object, but because gc finalize the mechanism does not happen often, so it is executed is uncertain. If gc does not happen, then finalize () method will not be called. Even gc occurred, if the class is not garbage objects, finalize () method will not be called.

gc collection mechanism

  -Java the garbage collector will release new out by our heap blocks that are not made out of the new "special memory", the garbage collector is not management.
  - so-called special memory refers to the memory allocation to the system via JNI with C / C ++, these memory if you do not have to manually purge would have been occupied in memory.
  - Moreover, the cost of garbage collection itself, so the virtual machine is not always GC, only when the memory is running out of JVM will trigger GC.

2, the classpath

https://www.cnblogs.com/hejing-swust/p/8514251.html

Guess you like

Origin www.cnblogs.com/exceptionblog/p/10984443.html