Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to improve my Java APP for better performance?

cmlonder:

I have some questions about performances for those kind of stations (using Spring Boot for my project now). As you will understand soon, I'm a bit confused while learning lots of informations at once. So i will share my different kind of questions and some of my tests to understand the flow and help others who are confused like me:

1) Reading application.properties like this:


@Value("${foo.name}")
private String name;

vs reading properties starting with poo into the Class and getting its value with getters.


@ConfigurationProperties(prefix = "foo")
public class FooBean {
private String name;
private String age;
...

// setters and getters
}

1a) So which approach is faster and why? Can you explain it with memory performances if there is a difference 1b) Also lets say i use FooBean approach and in the Example1 class i need only FooBean's name. Should i inject FooBean to the Example1 class while i need only 1 field of it or should i use @Value style and get only specific field, which has better performance now?

2) Should i declare fields of FooBean as static? Lets see an example:

Configuration class:


@Component
@ConfigurationProperties(prefix = "openstack")
@Data
@Validated
public class OpenStackBean {

@NotNull
@Valid
private String container;

@NotNull
@Valid
private String keystoneEndpoint;

@NotNull
@Valid
private String password;

@NotNull
@Valid
private String swiftEndpoint;

@NotNull
@Valid
private String tenantName;

@NotNull
@Valid
private String userName;
}

And lets say this class only will be used by its Service:


@Service
public class OpenStackService implements IOpenStackService {

private OpenStackBean openStackBean;

@Autowired
public OpenStackService(OpenStackBean openStackBean) {
this.openStackBean = openStackBean;
}

...
...


private OSClientV2 authenticate(Facing perspective) throws AuthenticationException {
return OSFactory
.builderV2()
.endpoint(openStackBean.getKeystoneEndpoint())
.credentials(openStackBean.getUserName(), blowfish.decryptString(openStackBean.getPassword()))
.tenantName(openStackBean.getTenantName())
.perspective(perspective)
.withConfig(Config
.newConfig()
.withConnectionTimeout(applicationBean.getConnectionTimeout())
.withReadTimeout(applicationBean.getReadTimeout())
.withMaxConnections(10)
.withMaxConnectionsPerRoute(2))
.authenticate();
}

If i am correct the scenario will be like this:

1) In this case I will be injecting those configurations as instance. 2) So every class (lets say i have 10.000 users in a minute and this class will be created 10.000 times) will create their own configuration in the stack, will it and when it completes, deletes it, so memory is free again. 3) But because there will be so many request, doesn't it make memory problems?

If this flow is correct, than should i create OpenStackBean's field as static and inject it as private static OpenStackBean ? This will be hold in the heap once and all users will get it from there. Which performance is better?

3) Here is my some test results which makes me start thinking about String manipulations' and injection's performance. Here is sample stress test using JMeter and visualizing with Java VisualVM

Sending 10.000 requests

But i don't understand why heap usage always increases. Here is how APP works:


@RequestMapping(method = RequestMethod.GET)
public ResponseEntity all(@ApiIgnore GameParam param) {
try {
// get games by categories by default
if (param.getPageNo() == null) {
return new ResponseEntity(gameService.getGamesByCategories(), HttpStatus.OK);
}

All requests entered this condition and called:

2) This function is called. Since categorizedGameList is static (which is initializied on the APP startup, and will be updated from the remote server daily by a Scheduler), all requests directly return with 2 steps. GameList is in the Heap, so no new instance will be created.

GameList --> 14 KB (14173 byte)


@Override
public List getGamesByCategories() {
if (categorizedGameList != null) {
return categorizedGameList;
} else {
return getAndCategorizeGames();
}
}

3) So why heap increases 300MB (from 100 to 400) after 10000 requests? This means 30 KB is created in the memory for every requests.

3a) What exactly happens when a request comes to an endpoint? Are all instance variables of the APP will be created or only visited classes will be created? I was thinking like this:

  • User creates a connection to my endpoint
  • User will be routed to my Controller class, so this class is created in the memory. (Not all the classes of the application). Its instance variables is created for this user only, static fields will be served from the memory for all users.
  • Controller's Service is called. So this Service is created for this user too.
  • Return static list to this user. This will not increase my memory 14KB, because it is already in the heap.
  • But my memory is increasing 30 KB for each request.

I know this is too long for read but i tried to give all my experiences during this path. I'm a bit confused about improving my performance, since i realized that i have never thought about it. Thanks for your patience.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to improve my Java APP for better performance?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×