Java CLI Application Performance with StringBuilder

General:
I write a socket client that receives all the time (market) data / quotes (endless loop) from some side of the server (remote).
I am dividing the data into pieces so that I can use it.
each fragment contains about 200 characters and needs to be converted to an array.
After the fragment was split, it was parsed into a list (no problem here).

Problem:
In 10 minutes of operation, the processor load reaches 40%. I managed to isolate the problem.
Each fragment must be converted to json.
So I give you real code that makes problems.
this code executes every 300-400 ms.
Skipping this code will leave the entire system using a 1% -2% processor.

Note:
I read this topic, but I do not see any solution there.
Is it possible to reuse StringBuilder in a loop?

The code:

private static StringBuffer jsonVal = new StringBuffer();

    public static String toJson(List<QuotesData> quotesData) {
        // Empty variable
        jsonVal.delete(0, jsonVal.length());
        jsonVal.append("{");
        synchronized (quotesData) {
            for (QuotesData quote : quotesData) {

                jsonVal.append("\"").append(quote.getSymbol()).append("\":[{");
                jsonVal.append("\"ask\":\"").append(quote.getAsk()).append(
                        "\",");
                jsonVal.append("\"bid\":\"").append(quote.getBid()).append(
                        "\",");
                jsonVal.append("\"time\":\"").append(quote.getDateTime())
                        .append("\"}],");

            }
            jsonVal.append("}");
            String returnString = jsonVal.toString();
            return returnString.toString().replace("}],}", "}]}");
        }
    }
+3
source share
4 answers

JProfiler JConsole, JDK6, , .

, , synchronized. , append. , static jsonVal .

public static String toJson(final List<QuotesData> quotesData) {
    final List<QuotesData> theData = new ArrayList<QuotesData>(quotesData);
    StringBuffer jsonVal = new StringBuffer();
    jsonVal.append("{");
    for (QuotesData quote : quotesData) {
        jsonVal.append("\"").append(quote.getSymbol()).append("\":[{");
        jsonVal.append("\"ask\":\"").append(quote.getAsk()).append(
                "\",");
        jsonVal.append("\"bid\":\"").append(quote.getBid()).append(
                "\",");
        jsonVal.append("\"time\":\"").append(quote.getDateTime())
               .append("\"}],");

    }
    jsonVal.append("}");
    String returnString = jsonVal.toString();
    return returnString.toString().replace("}],}", "}]}");
}

JSON, Gson. . , :

private static final Gson gson = new Gson();
public static String toJson(final List<QuotesData> quotesData) {
    return gson.toJson(new ArrayList<QuoteData>(quotesData));
}
+2

:

  • , .
  • StringBuilder StringBuffer. StringBuffer , StringBuilder .
  • synchronized? , .
  • toString() return. .
  • , replace(), , returnString .
  • StringBuffer , .
  • interned i.e. return returnString.intern()
0

- , StringBuilder . ? StringBuilder for:

StringBuffer jsonVal = new StringBuffer(quotesData.size()*200); //the 200 is on top of my head. Do a few loop to see what is the average length of a QuotesData.
, StringBuilder? , StringBuffer, (StringBuffer , StringBuild - ).
0

, . , , 300-400 .

: 300 , 300 . , , , . , , . :

  • , , .
  • json , json 300 .

, , , , - , . , , :

public static final int JSON_LENGTH = 250; //you probably know this

public static String toJson(final List<QuotesData> quotesData) {
    jsonVal = new StringBuilder(JSON_LENGTH * quotesData.size());
    jsonVal.append("{");
    synchronized (quotesData) {
        for (QuotesData quote : quotesData) {

            jsonVal.append("\"").append(quote.getSymbol()).append("\":[{")
            .append("\"ask\":\"").append(quote.getAsk()).append("\",")
            .append("\"bid\":\"").append(quote.getBid()).append("\",")
            .append("\"time\":\"").append(quote.getDateTime()).append("\"}],");

        }
        // much much faster than replace
        jsonVal.setCharAt(jsonVal.length()-1, '}');
        return jsonVal.toString();
    }
}

, , JIT . , , StringBuilder , .replace(). , , ( ), , . .

0

Source: https://habr.com/ru/post/1758090/


All Articles