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 Essential Questions About Spring in a Technical Interview

Published in the Random EN group

How garbage collection works in Java (JVM)

Source: DZone

Java garbage collection process

Garbage collection in Java allocates and deallocates memory automatically, so developers don't have to write a separate program to manage memory, which is one of the main advantages of Java programming. Every time a Java program is run on the JVM, objects are created on the heap and represent a portion of the program's memory. 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 Essential Spring Tech Interview Questions - 1The garbage collector, by evaluating the heap of memory, determines which objects are in use and which are not, and removes unused objects. An object that is used or referenced means that some part of your program still maintains a pointer to that object. If the object is no longer in use or is not 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 a garbage collection. Coffee break #61.  How garbage collection works in Java (JVM).  15 Essential 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 in use and not in use by the garbage collector. Usually labeling is the first step. Normal deletion is the process of deleting objects that are not referenced, leaving in free space the objects and pointers that are referenced. Compact Deletion - In addition to deleting unreferenced objects, it compresses the remaining referenced objects, moving the objects together to make re-allocation much easier and faster.

JVM Heap Memory

Coffee break #61.  How garbage collection works in Java (JVM).  15 Essential Questions About Spring in a Technical Interview - 3

young generation

Newly created objects start in Young Generation. It is also called a nursery, as new objects begin to live here. Young Generation is subdivided into Eden Space, where all new objects start, and two Survivor spaces, where objects are moved out of Eden after being saved (surviving) in one garbage collection cycle. They cause 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 trigger when it reaches a threshold defined by the JVM. The mentioned objects are moved from Eden Space to the space of the first save ('Eden' and 'from' —> 'to'). Unreferenced objects are removed when Eden Space is cleared. Survivor 0 (S0) and Survivor 1 (S1) Both fields survivor (From and to) start empty. When garbage collection occurs, all referenced objects are moved into the remaining space. When the garbage collection ends, 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 a minor garbage collection, when stale objects reach a certain age threshold (by default, the threshold for modern JVMs is set to 15 garbage collection cycles), they, along with long-lived objects, pass from the young generation to the old one. As the secondary garbage collectors continue to spawn, objects continue to move into the Old Generation space, it will start to fill up and a 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 Essential Spring Tech Interview Questions - 4

Permanent generation

Metadata such as classes and methods are stored in the 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 Essential Questions About Spring in a Technical Interview - 5

Types of garbage collection

Garbage collections that clean up various parts within the heap are often referred to as Minor, Major, and Full garbage collections. But, since the terms Minor, Major, and Full are widely used and without a proper definition, we will go over an explanation of all these types of garbage collection.

Minor Garbage Collection

Garbage collection from Young Generation space is called Minor Garbage Collection. This build type is always triggered when the JVM cannot allocate space for a new object, i.e. when Eden Space fills up. Thus, the higher the selection rate, the more often the Minor Garbage Collection occurs.

Major Garbage Collection

Major Garbage Collection cleans up Tenured (old space). Because the Old Generation is larger, assembly is less frequent than that of the Young Generation. When objects disappear from the Old Generation, we say that a "big garbage collection" has occurred. The Old Generation builder will try to anticipate when it needs to start building to avoid promotion failures from the Young Generation. Builders keep track of the fill threshold for Old Generation and start building when that threshold is exceeded. If this threshold is not enough to meet the requirements of the promotion, the "Full Garbage Collection" is launched.

Full Garbage Collection

The Full Garbage Collection cleans up the whole pile - both young and old spaces. Many people get confused between Major (only OLD generation) and Full GC (Young + OLD (Heap)). The Full Garbage Collection includes the advancement of all living objects from young to old generation after the assembly and compaction of the old generation. A full garbage collection will be a stop for Stop-the-World. It ensures that no new objects are allocated and no objects become unavailable while the collector is running.

15 Essential Questions About Spring in a Technical Interview

Source: Dev.to Spring Framework is a general-purpose framework for the Java platform. Its core functionality can be used by any Java application, and there are extensions to build web applications based on Java EE. Here is a list of interview questions and answers related to Spring coding. We hope they will help you prepare for your technical interview in 2021.Coffee break #61.  How garbage collection works in Java (JVM).  15 Essential Questions About Spring in a Technical Interview - 6

1. What is Spring?

Answer: 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 there are extensions for building web applications based on the Java EE platform. The Spring framework aims to simplify the use of J2EE in development and improve programming practices by including a POJO (Plain Old Java Object) based model.

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

Answer: The default bean scope 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?

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

5. What is contained in a bean definition?

Answer: The 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?

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

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

Answer: 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 it is to take the entire request and route it to various application components for actual processing. In Spring MVC, the DispatcherServlet is used to find the right controller to handle the request. This is done through handler mapping: for example, the @RequestMapping annotation.

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

Answer: 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 your application's classpath. 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: The following is a list of some of the benefits of using the Spring Framework:
  • Lightweight - Spring is relatively lightweight when it comes to size and transparency. The base version of the Spring Framework is about 2MB.
  • Inversion of control (IOC) - Loose coupling is achieved in Spring using the inversion of control technique. Objects provide their dependencies instead of creating or looking up dependent objects.
  • Aspect Oriented − Spring supports aspect oriented programming and ensures consistent development by separating application business logic from system services.
  • Containers - Spring Container creates objects, binds them together, configures 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 such as Struts or other over-engineered or less popular web frameworks.
  • Transaction Management - Spring has a consistent transaction management interface that can scale to local transaction (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?

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

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

Answer: The core 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 beans together, and distribute them on demand. But it also provides:
  • Tool for resolving text messages, including support for internationalization.
  • General way to load 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 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 this bean on startup. Your JSF pages have access to these beans and all of 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. The DelegatingVariableResolver first delegates the value lookup 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: Custom VariableResolver works well when mapping its properties to beans in faces-config.xml. But if you need to capture a bean, then the FacesContextUtils class makes it easy. It is similar to WebApplicationContextUtils except that it takes a FacesContext parameter rather than a ServletContext parameter.
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext (FacesContext.getCurrentInstance ());

14. What is Spring MVC framework?

Answer: The Spring Web MVC framework provides a model-view-controller architecture and out-of-the-box components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in a separation of the various aspects of an application (input logic, business logic, and user interface logic) while maintaining loose coupling between these elements.

15. How is event handling done in Spring?

Answer: Handling 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