Java面试题解
1. Java中垃圾回收的目的是什么, 它什么时候被使用 ?
垃圾回收用于识别和丢弃程序不再需要的对象, 以便回收和复用资源。
1. What is the purpose of garbage collection in Java, and when is it used ?
The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.
2. System.gc() 和Runtime.gc()方法用途?
这些方法用于提醒JVM开始垃圾回收. 然而开始垃圾回收的时机是由JVM决定的。
2. What does System.gc() and Runtime.gc() methods do ?
These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.
3. finalize()什么时候被调用 ? 它的目的是什么 ?
finallize方法是在释放该对象内存前由gc(垃圾回收器)调用. 通常建议在这个方法中释放该对象持有的资源。
3. When is the finalize() called ? What is the purpose of finalization ?
The finalize method is called by the garbage collector, just before releasing the object’s memory. It is normally advised to release resources held by the object inside the finalize method.
4.如果一个对象的引用被设置为null, gc会立即释放该对象的内存么?
不会, 这个对象将会在下一次gc循环中被回收。
4. If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object ?
No, the object will be available for garbage collection in the next cycle of the garbage collector.
5. Java堆的结构是什么 ? 堆中的Perm Gen(全称是Permanent Generation)空间是什么 ?
JVM有一个运行时数据区,即堆([heap](http://www.javacodegeeks.com/2012/07/5-tips-for-proper-java-heap-size.html)).所有的类实例和数组的内存都是从堆中分配的. 它在JVM启动时被创建。对象所占用的堆内存会被一个称为垃圾回收器的自动内存管理系统收回。
堆内存中包含live和dead对象。live对象可以被程序访问并且不会被垃圾回收,dead对象是那些不会被程序访问的, 但还没有被垃圾回收器收回的对象. 这种对象会占用堆内存空间直到最终被垃圾回收器收回。
5. What is structure of Java Heap ?
The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector.
Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.
6. Serial 垃圾回收器与 Throughput 垃圾回收器区别 ?
Throughput垃圾回收器使用并行版本的新生代回收器, 它用于中到大型数据集的应用. 另一方面, Serial回收器通常足以应对大多数的小应用(在现代处理器上不会超过约100MB的堆内存)。
6. What is the difference between Serial and Throughput Garbage collector ?
The throughput garbage collector uses a parallel version of the young generation collector and is meant to be used with applications that have medium to large data sets. On the other hand, the serial collector is usually adequate for most small applications (those requiring heaps of up to approximately 100MB on modern processors).
7. 什么时候对象会被回收 ?
当当前程序无法访问到某个对象时,该对象将被回收。
7. When does an Object becomes eligible for Garbage collection in Java ?
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used.
8. 垃圾回收发生在指定的JVM区域 ?
垃圾回收在 PermGen 里发生。如果 PermGen满了或是到了瓶颈,就会触发完全回收。如果仔细 观察垃圾回收过程,会发现PermGen最后也被回收了. 这就是为什么要设置合理的PermGen大小以避免完全垃圾回收。
8. Does Garbage collection occur in permanent generation space in JVM ?
Garbage Collection does occur in PermGen space and if PermGen space is full or cross a threshold, it can trigger a full garbage collection. If you look carefully at the output of the garbage collector, you will find that PermGen space is also garbage collected. This is the reason why correct sizing of PermGen space is important to avoid frequent full garbage collections.
#内推##面经##校招#