lichMax
Level 40
Санкт-Петербург

Level 34

Published in the Random EN group
I searched the site with my native search and with the help of Google I covered everything - there are no answers to questions from this level. Maybe I missed something, and they are still somewhere here on the site!? Level 34Just in case, I am attaching the answers that I wrote for myself: Interview questions:
  1. What is garbage collection?
  2. When is the method called finalize?
  3. What happens if finalizean exception occurs in a method?
  4. What is SoftReference?
  5. What is WeakReference?
  6. What is PhantomReference?
  7. How does it work WeakHashMap? Where is it used?
  8. Why is it necessary to pass a queue to the constructor PhantomReference?
  9. Why do you need a logger?
  10. What logger settings do you know?
My answers:
  1. This is a mechanism for destroying unnecessary objects. Waste objects are unused objects. There are two ways to find such objects: reference counting and tracing. In the first case, some variable is associated with each object, which stores the number of references to this object. If this number drops to zero, then the object is considered dead. In the second case, the garbage collector follows the object references from the root points to the end (to the value of null), bypassing the entire tree. Objects that it cannot reach from root points are considered dead. Root points are all active threads, the main method, method arguments main(), and all static variables of the class in which the method resides main().

    Determining which objects to destroy is only the first part of the garbage collector's job. The second part is actually their removal and work with memory. A hybrid approach is used here. All memory available for objects is divided into three areas: the area of ​​young objects, the area of ​​old objects and the area of ​​permanent objects (these are classes, metadata, interned strings, etc.). The first area is divided into three more sub-areas: into Eden and suvivor space 1 and 2. All just created objects are stored in Eden. The remaining two zones store objects that survived the last garbage collection. The garbage collector works with this entire region (the region of young objects) in the following way. During the next garbage collection, it finds living objects in the Eden area and copies them to the second survivor area. After that, he also searches for living objects in the first area and copies them either to the second area of ​​the survivors, or, if they are already "old" enough, to the area of ​​the old generation. After that, he clears the Eden area and the first survivor area. He then counts the second survivor area as the first. And that's it, garbage collection ends for this area.

    For the second region, garbage collection is somewhat different. There is one large area there, it is not divided into anything, but the garbage collector moves all living objects in it to the beginning of the area during its work. Accordingly, the second part of the area will consist only of empty space and dead objects. After that, the garbage collector ends its work.

  2. Before the object is destroyed by the garbage collector. You can also manually launch calls to this method on all unreachable objects by calling the System.runFinalization()or method Runtime.getRuntime().runFinalization().

  3. This exception will be ignored and the method will exit.

  4. SoftReferencetranslates as "soft link". This is an object reference, but weaker than a regular reference (StrongReference). Objects that only have soft references are called soft-reachable. Such objects are not normally destroyed. But if the JVM runs out of memory, then the garbage collector removes all such objects.

  5. WeakReferenceis a so-called weak reference to an object. It is even weaker than soft links. All objects that only have weak references will be removed at the next garbage collection.

  6. PhantomReferenceis the weakest link. The mechanism for working with such references is only triggered if there are no other references to the object. Ghost links are used for the complicated procedure of deleting an object. This may be necessary if the object does something outside of the Java machine, such as calling low-level OS functions, or writing its state to a file, or doing something else important and complex.

    The mechanism for working with such links is as follows. If there are no other references left to the object, and it has a method overridden finalize(), then this method will be called during the next garbage collection. If this method is not overridden, then this object skips the current garbage collection, and only gets into the next one. During this (next) garbage collection, this object is put into the ghost object queue, from which it will be removed when the method is called on its ghost reference clear(). It's also worth noting that the method get()on a ghost reference always returns null (unlike the other two non-strong references, for which it nullonly returns if the object has already been destroyed).

  7. WeakHashMapis the one HashMapwhose keys are weak references. Therefore, if during the next garbage collection it is found that only a reference to the object exists in WeakHashMap, then WeakHashMapthe entire key-value pair associated with this object will be removed from .

    In this regard, this collection can be used to store some additional, not very important information about the object. It is also convenient to use it to store some temporary information (which is needed only within the framework of this operation).

  8. This queue is used to keep track of when an object is no longer needed. Can be used to close resources opened by this object (for example, deleting created files).

  9. The logger is needed to save information about the behavior of the program, as well as some of its states. It can be used for debugging and detecting program errors and crashes. The logger also allows the developer to receive feedback from his program while it is running. In addition, in case of critical failures, the logger can promptly notify the right people (for example, developers, clients, project managers, technical support, etc.) about these failures.

  10. When setting up logging, you can specify the following things:

    • the place where the information will be written (file, console, database, network, etc.)
    • what level of messages will be recorded
    • log entry type
    • for files, you can specify: the path to the file and directory, the size of the files, the number of files
    • specify for each individual package its own level of messages that will be written to the log
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION