[ad_1]
I’ve attended interview session and I was asked about ways of optimization of such pseudo-code. It works under the load. It utilize 100% CPU and 100% Memory. After some discussion it was mentioned that it is because of GC.
public class MyString processor {
....
public String process(InputStream inputStream) {
String input = getString(inputStream);
int position = input.size() / 2;
return str.substring(0, position) + "some_constant_string_inside" + str.substring(position);
}
}
As we can see here we create 2 strings during this call. and if this method is invoked 1000 times then 2000 String will be created. So it is a reason of memory consumption which is a root cause of unstopable GC work.
We agreed that it is a bad idea to create so many Strings.
First step was reading char array from socket and then we can modify this array inserting some string in the bigining. (Here I still have aт unanswered question how can we create char_array with size = char_array_size_from_socket + some_constant_space_for_sting_in_the_beginning – from my view there are 2 arrays will be created)
In the end we decided that it is better to have char array buffer per thread and store it in thread local to avoid race conditions and concurrent reads.
Do you have any other ideas? Could you please provide pseudocode for a such solution taking into account that string size is unknown value and could be different for each string
[ad_2]