Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How efficient is Spring AOP in case of memory consumption

In my pet project i have a long running job, i want to show the status to the user about the process and how far it went. So am pushing the status objects to JMS topic from there am picking up the and feeding to WS app to stream them to a valid client. I have written point cuts in spring AOP(namely @Before, @AfterReturn) and calling my service to send the message to topic. Now i want to log the status of the service not on the method start or after return inside the method also. So i called the service(which has jmsTamplete injected and took status object). Is there any way to minimize these calls so that i can not repeat the service calls. here is my sudo code.

public class Myservice{

UserDao userDao;
LegService legservice;
ProviderDao providerDao;
....
StatusServive statusServie;

//aop will call the same service to send info to JMS topic
fetchandCalculateLeg(){
    // here i called statusServie.senStatus(StatusObject);
    List<Users> = userDao.fetchUserInfo();
    // here i called statusServie.senStatus(StatusObject);
    ....
    loop: #forEachUser
    // here i called statusServie.senStatus(StatusObject);
    someList  = legservice.fecthLegInfoForEachUser();
    // here i called statusServie.senStatus(StatusObject);
    :endloop;
    ....
} }

At present i have 3 long running tasks, and i am calling the same in each and every method class. i want to minimize calls.

EDIT1: I can apply AOP on the calling methods too, but what is the performance in this case? How to measure the AOP performance(how much memory it consumed during application Up or to create proxy objects). Sorry for asking so many questions.

like image 390
mallikarjun Avatar asked May 13 '16 19:05

mallikarjun


People also ask

Does Spring AOP affect performance?

Spring AOP is a proxy-based framework, so there is the creation of proxies at the time of application startup. Also, there are a few more method invocations per aspect, which affects the performance negatively.

What are the advantages of Spring AOP?

Advantages of AOPYour service/domain classes get advised by the aspects (cross cutting concerns) without adding any Spring AOP related classes or interfaces into the service/domain classes. Allows the developer to concentrate on the business code, instead the cross cutting concerns.

How does Spring boot application reduce memory usage?

You can get a simple Spring Boot app down to around 72M total by using the following JVM options. With -XX:MaxRAM=72m This will restrict the JVM's calculations for the heap and non heap managed memory to be within the limits of this value.


2 Answers

If you use AOP performance will be better than this as it is well tested and optimized framework to do such operations. Anyways you can check by subtracting start and end time.

You can use your current code and AOP implemented code to check performance. It would be very interesting job. Do something like below to set start and end time and subtract.

System.currentTimeMillis()

Above syntax , Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).

There are many AOP Opensource Frameworks available.

  1. Spring AOP: AOP with Spring Framework. Advertisements. One of the key components of Spring Framework is the Aspect oriented programming (AOP) framework. Aspect Oriented Programming entails breaking down program logic into distinct parts called so-called concerns.

  2. AspectJ: AspectJ is a seamless aspect-oriented extension to the Java programming language, Java platform compatible and easy to learn and use. AspectJ enables the clean modularization of crosscutting concerns such as: error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, multi-object protocols.

  3. AspectWerkz: AspectWerkz is a dynamic, lightweight and high-performant AOP framework for Java. AspectWerkz offers both power and simplicity and will help you to easily integrate AOP in both new and existing projects. AspectWerkz utilizes runtime bytecode modification to weave your classes at runtime. It hooks in and weaves classes loaded by any class loader except the bootstrap class loader. It has a rich and highly orthogonal join point model. Aspects, advices and introductions are written in plain Java and your target classes can be regular POJOs. You have the possibility to add, remove and re-structure advice as well as swapping the implementation of your introductions at runtime. Your aspects can be defined using either an XML definition file or using runtime attributes.

EDIT 1

Spring also supports AspectJ. I did test it with AspectJ 1.7.2 it doesn't show memory leak. You, can replicate same in your application by creating lots of beans then taking heap dump after some time as garbage collection requires some time it is not instant.

As, per my testing. Performance is decent with huge no of calls.

Simple Example is avaliable in MKYong : http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/

NOTE: You should modify your question performance is measured time and space. And, as per your comment it looks like you are asking about more of memory optimization or management stuff. Also, your question subject is different from question.

like image 78
Pratiyush Kumar Singh Avatar answered Sep 23 '22 22:09

Pratiyush Kumar Singh


IF you have such long-lasting job, why don't you put it into a Spring-Batch runtime? Implement custom reader -> processor -> writer and the system will do everything for you. And to show details of such job executions, I have created such DTO https://gist.github.com/idyoshin/b035317db8c61b1b49ccb8898848171e and collector https://gist.github.com/idyoshin/74ad80841a51e1be62208f3fd58eeb6a. This utilities, are work-arounds, because I didn't wanted to embed spring-batch admin application into my main app. I use them to show details of active/archive execution of specific jobs (based on names) of my system.

Also using such utilities you can implement custom timer-based data collector, which will be pushing this data to JMS or Websocket. Spring-batch also has nice listeners, which you can implement, for-example custom org.springframework.batch.core.ChunkListener will allow you to make "progress-bar" like updates - you will know moment when your chunk was processed, and thus update on progress appeared.

In contrast to your initial AOP solution, approach with Spring batch will be strait-forward, (pure java code), and also will solve you additional problem: transactions of long-lasting large updates: it will cut the whole dataset into chunks, and process each chunk in single transaction.

like image 29
Ilya Dyoshin Avatar answered Sep 20 '22 22:09

Ilya Dyoshin