Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 675983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:52:41+00:00 2026-05-14T00:52:41+00:00

I am trying to benchmark some code. I am sending a String msg over

  • 0

I am trying to benchmark some code. I am sending a String msg over sockets. I want to send 100KB, 2MB, and 10MB String variables. Is there an easy way to create a variable of these sizes?

Currently I am doing this.

private static String createDataSize(int msgSize) {
    String data = "a";
    while(data.length() < (msgSize*1024)-6) {
        data += "a";
    }
    return data;
}

But this takes a very long time. Is there a better way?

UPDATE:
Thanks, I am doing this now.

/**
 * Creates a message of size @msgSize in KB.
 */
private static String createDataSize(int msgSize) {
    // Java chars are 2 bytes
    msgSize = msgSize/2;
    msgSize = msgSize * 1024;
    StringBuilder sb = new StringBuilder(msgSize);
    for (int i=0; i<msgSize; i++) {
        sb.append('a');
    }
    return sb.toString();
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:52:42+00:00Added an answer on May 14, 2026 at 12:52 am

    Java chars are 2 bytes (16 bits unsigned) in size. So if you want 2MB you need one million characters. There are two obvious issues with your code:

    1. Repeatedly calling length() is unnecessary. Add any character to a Java String and it’s length goes up by 1, regardless of what the character is. Perhaps you’re confusing this with the size in bytes. It doesn’t mean that; and
    2. You have huge memory fragmentation issues with that code.

    To further explain (2), the String concatenation operator (+) in Java causes a new String to be created because Java Strings are immutable. So:

    String a = "a";
    a += "b";
    

    actually means:

    String a = "a";
    String a = a + "b";
    

    This sometimes confuses former C++ programmers as strings work differently in C++.

    So your code is actually allocating a million strings for a message size of one million. Only the last one is kept. The others are garbage that will be cleaned up but there is no need for it.

    A better version is:

    private static String createDataSize(int msgSize) {
      StringBuilder sb = new StringBuilder(msgSize);
      for (int i=0; i<msgSize; i++) {
        sb.append('a');
      }
      return sb.toString();
    }
    

    The key difference is that:

    1. A StringBuilder is mutable so doesn’t need to be reallocated with each change; and
    2. The StringBuilder is preallocated to the right size in this code sample.

    Note: the astute may have noticed I’ve done:

    sb.append('a');
    

    rather than:

    sb.append("a");
    

    'a' of course is a single character, "a" is a String. You could use either in this case.

    However, it’s not that simple because it depends on how the bytes are encoded. Typically unless you specify it otherwise it’ll use UTF8, which is variable width characters. So one million characters might be anywhere from 1MB to 4MB in size depending on you end up encoding it and your question doesn’t contain details of that.

    If you need data of a specific size and that data doesn’t matter, my advice would be to simply use a byte array of the right size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The query you're using doesn't have total defined. Try: $result=mysql_query("SELECT… May 15, 2026 at 6:31 am
  • Editorial Team
    Editorial Team added an answer DirectX doesn't have functions for drawing just one point. It… May 15, 2026 at 6:31 am
  • Editorial Team
    Editorial Team added an answer You could try adding the CSS rule word-wrap: break-word; Though,… May 15, 2026 at 6:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.