JavaRush /Java Blog /Random EN /Coffee break #60. How garbage collection works in Java (J...

Coffee break #60. How garbage collection works in Java (JVM). 15 important questions about Spring in a technical interview

Published in the Random EN group

How garbage collection works in Java (JVM)

Source: DZone

Garbage collection process in Java

Java garbage collection automatically allocates and frees memory, so developers don't have to write a separate program to manage memory, which is one of the main benefits of programming in Java. Every time a Java program runs on the JVM, objects are created on the heap and represent a portion of memory allocated to the program. Over time, some items will no longer be needed. The garbage collector finds these unused objects and removes them to free up memory. Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 1The garbage collector, after evaluating the memory heap, will determine which objects are in use and which are not, and remove unused objects. A used or referenced object means that some part of your program still maintains a pointer to that object. If an object is no longer in use or is no longer referenced, then it will no longer be referenced by any part of the program. Thus, memory that is used by an unreferenced object can be freed by performing garbage collection. Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 2Freeing memory can be described by three main processes:
  1. Marking.
  2. Normal removal.
  3. Removal with compaction.
Marking is the process of identifying parts of memory that are and are not used by the garbage collector. Labeling is usually the first step. Normal deletion is the process of deleting objects that are not referenced, leaving referenced objects and pointers in free space. Compaction delete - In addition to deleting unreferenced objects, it compacts the remaining referenced objects, moving the objects together to make new memory allocation much easier and faster.

JVM Heap Memory

Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 3

Young Generation

Newly created objects start in Young Generation. It is also called a nursery because new objects begin to live here. Young Generation is divided into Eden Space, where all new objects start, and two Survivor Spaces, where objects are moved out of Eden after surviving in a single garbage collection cycle. They cause re-garbage collection when objects are collected by the Young Generation garbage collector. Eden Space All new objects are first created in Eden Space. Minor garbage collection will fire when it reaches a threshold determined by the JVM. The mentioned objects are moved from Eden Space to the first save space ('Eden' and 'from' -> 'to'). Unreferenced objects are removed when Eden Space is cleared. Survivor 0 (S0) and Survivor 1 (S1) Both survivor (From and to) fields start blank. When garbage collection occurs again, all referenced objects are moved into the remaining space. When garbage collection finishes, the places (names) of the survivor "from" and "to" are swapped. If during the previous garbage collection S1 was in the “to” role, now S1 is full and becomes “from”. Accordingly, if S0 is empty, then it will become “to”.

Old Generation

After minor garbage collection, when obsolete objects reach a certain age threshold (the default threshold for modern JVMs is set to 15 garbage collection cycles), they, along with long-lived objects, move from the young generation to the old one. As minor garbage collectors continue to arise, objects continue to move into the Old Generation space, it will begin to fill up and major garbage collection will occur. Basic garbage collection occurs when objects are collected by the Old Generation garbage collector. Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 4

Permanent Generation

Metadata such as classes and methods are stored in Permanent Generation. Classes that are no longer used can be removed from it by the garbage collector. During a full garbage collection, unused objects from all generations are collected.Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 5

Types of Garbage Collection

Garbage collections that clean up various parts within the heap are often called Minor, Major, and Full garbage collections. But since the terms Minor, Major and Full are widely used without proper definition, we will look at the explanation of all these types of garbage collection.

Minor Garbage Collection

The collection of garbage from the Young Generation space is called Minor Garbage Collection. This type of build is always triggered when the JVM cannot allocate space for a new object, that is, when Eden Space is full. Thus, the higher the selection rate, the more often Minor Garbage Collection occurs.

Major Garbage Collection

Major Garbage Collection cleans out Tenured (old space). Because the Old Generation is larger, assembly occurs less frequently than the Young Generation. When objects disappear from Old Generation, we say that a "major garbage collection" has occurred. The Old Generation collector will try to predict when he needs to start collecting to avoid promotion failures from Young Generation. Collectors monitor the Old Generation's fill threshold and begin collecting when that threshold is exceeded. If this threshold is not sufficient to meet promotion requirements, the "Full Garbage Collection" is launched.

Full Garbage Collection

Full Garbage Collection cleans out the whole bunch—both young and old spaces. Many people are confused between Major (OLD generation only) and Full GC (Young + OLD (Heap)). Full Garbage Collection includes the promotion of all living objects from young to old generation after assembling and compacting the old generation. A complete garbage collection will be a stop for Stop-the-World. It ensures that new objects are not allocated and that objects do not become unavailable while the collector is running.

15 important questions about Spring in a technical interview

Source: Dev.to Spring Framework is a universal framework for the Java platform. Its core features can be used by any Java application, and there are extensions for creating Java EE-based web applications. Here is a list of interview questions and answers related to Spring coding. We hope they help you prepare for your technical interview in 2021.Coffee break #61.  How garbage collection works in Java (JVM).  15 important questions about Spring in a technical interview - 6

1. What is Spring?

Ans: Spring is an open source framework for developing Java applications. The core features of the Spring Framework can be used to develop any Java application, and extensions are also available to create web applications based on the Java EE platform. The Spring framework aims to make J2EE easier to use in development and improve programming practices by incorporating a POJO (Plain Old Java Object) based model.

2. What is the default scope of a bean in the Spring framework?

Ans: The default scope of a bean is Singleton (design pattern).

3. What is Bean wiring?

Answer: Bean wiring is the act of creating associations between application components (beans) in a Spring container.

4. What is Spring Security?

Ans: Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods to Java applications. It also fixes most common security vulnerabilities such as CSRF attacks. To use Spring Security in web applications, you can start with a simple annotation: @EnableWebSecurity.

5. What is contained in a bean definition?

Answer: A bean definition contains information called configuration metadata that the container needs to know the following:
  • How to create a bean;
  • Bean lifecycle details;
  • bean dependencies.

6. What is Spring Boot?

Ans: Spring Boot is a project that provides a pre-configured set of frameworks to reduce boilerplate configuration so that you can get a Spring application up and running with minimal code.

7. What is DispatcherServlet and what is it used for?

Ans: DispatcherServlet is an implementation of the Front Controller design pattern that handles all incoming web requests to a Spring MVC application. The Front Controller pattern (Enterprise Application Design Pattern) is a common pattern in web applications whose job is to take the entire request and route it to various components of the application for actual processing. In Spring MVC DispatcherServlet is used to find the correct controller to handle the request. This is done using handler mappings: for example, the @RequestMapping annotation.

8. Is spring-mvc.jar needed on the classpath or is it part of spring-core?

Ans: Spring-mvc.jar is part of spring-core, which means that if you want to use the Spring MVC framework in your Java project, then you must include spring-mvc.jar in the classpath of your application. In a Java web application, spring-mvc.jar is usually placed in the /WEB-INF/lib folder.

9. What are the benefits of using Spring?

Answer: Below is a list of some benefits of using Spring Framework:
  • Lightweight – Spring is relatively lightweight when it comes to size and transparency. The basic version of Spring Framework is about 2 MB.
  • Inversion of control (IOC) - Loose coupling is achieved in Spring using the inversion of control technique. Objects provide their dependencies instead of creating or searching for dependent objects.
  • Aspect-Oriented - Spring supports aspect-oriented programming and ensures consistent development by decoupling application business logic from system services.
  • Containers - Spring Container creates objects, binds them together, configures them, and manages them from creation to disposal.
  • MVC Framework - Spring web framework is a well designed MVC web framework that provides an alternative to web frameworks like Struts or other overly designed or less popular web frameworks.
  • Transaction Management - Spring has a consistent transaction management interface that can scale to local transactions (eg using a single database) or global transactions (eg using JTA).
  • Exception Handling - Spring provides a convenient API for converting technology-specific exceptions (such as those thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

10. What are Spring beans?

Ans: Spring beans are instances of objects that are managed by Spring Container. They are created and connected by the framework and placed in an “object bag” (container) from where you can later retrieve them. Wiring is what constitutes dependency injection. This means that you can simply say, “I need this thing,” and the framework will follow certain rules to get that object.

11. What is the purpose of the Core Container module?

Ans: The kernel container provides the core functionality of the Spring framework. The primary component of the main container is the BeanFactory, an implementation of the Factory pattern. BeanFactory uses Inversion control to separate the configuration and application specification dependencies from the actual application code.

12. What is an Application Context?

Answer: At first glance, the application context is the same as the bean factory. Both load bean definitions, bundle the beans together, and distribute them on request. But it also provides:
  • A tool for resolving text messages, including support for internationalization.
  • A common way to download file resources.
  • Events for beans that are registered as listeners.

13. How to integrate Java Server Faces (JSF) with Spring?

Answer: JSF and Spring do share some of the same features, especially in the area of ​​Inversion of Control services. By declaring JSF managed beans in the faces-config.xml configuration file, you allow FacesServlet to instantiate that bean at startup. Your JSF pages have access to these beans and all their properties. JSF and Spring can be integrated in two ways: DelegatingVariableResolver : Spring comes with a JSF variable resolver that allows you to use JSF and Spring together. DelegatingVariableResolver first delegates the lookup of values ​​to the default interpreter of the underlying JSF implementation, and then to the "business context" of the Spring WebApplicationContext. This makes it easy to inject dependencies into JSF-managed beans. FacesContextUtils : A custom VariableResolver works well when mapping its properties to beans in faces-config.xml. But if you need to capture a bean, the FacesContextUtils class makes it easy. It is similar to WebApplicationContextUtils except that it accepts a FacesContext parameter rather than a ServletContext parameter.
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext (FacesContext.getCurrentInstance ());

14. What is Spring MVC framework?

Ans: The Spring Web MVC framework provides a model-view-controller architecture and pre-built components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in the separation of different aspects of an application (input logic, business logic, and UI logic) while allowing loose coupling between these elements.

15. How does event handling work in Spring?

Answer: Processing in the ApplicationContext is provided through the ApplicationEvent class and the ApplicationListener interface. That is, if a bean implements ApplicationListener , then every time an ApplicationEvent is published to the ApplicationContext , that bean is registered. Thanks for reading and good luck with your technical interview!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION