Concat two string without 'losing' memory...

Hi,
I am using this code
public class Test{
     Test(){
          String s = "a";
          for (int i = 0 ; i < 23 ; i++)
               s = s.concat(s);
               //s += s;
          System.out.println(s.length() / 1048576);
System.out.println("MAX MEMORY = " + Runtime.getRuntime().maxMemory() / 1048576 + "MB");
System.out.println("TOTAL MEMORY = " + Runtime.getRuntime().totalMemory() / 1048576 + "MB");
System.out.println("FREE MEMORY = " + Runtime.getRuntime().freeMemory() / 1048576 + "MB");
               //s += s;
               s = s.concat(s);
          System.out.println(s.length() / 1048576);
System.out.println("MAX MEMORY = " + Runtime.getRuntime().maxMemory() / 1048576 + "MB");
System.out.println("TOTAL MEMORY = " + Runtime.getRuntime().totalMemory() / 1048576 + "MB");
System.out.println("FREE MEMORY = " + Runtime.getRuntime().freeMemory() / 1048576 + "MB");
     public static void main(String args[]){
          Test t = new Test();
}I get on console this
8
MAX MEMORY = 508MB
TOTAL MEMORY = 508MB
FREE MEMORY = 483MB
16
MAX MEMORY = 508MB
TOTAL MEMORY = 508MB
FREE MEMORY = 451MB
It took 32MB which is very much. Can I do something, and what I should do, to take less memory? It is logical if I had string of size 8 MB and to the same string I add 8 MB, to be FREE MEMORY 8 MB less then before, but not 32 MB.
Thank you.

Don't use the method concat on strings. Use a
StringBuffer or StringBuilder instead. If I instead
String s = "a"; put
StringBuffer s = new StringBuffer();
s.append("a");and instead
s = s.concat(s);put
s.append(s.toString());
FREE MEMORY becomes 472MB and 428MB, before was 483MB and 451MB.
>
Do also note that the GC executes when it needs to,
and the memory that has been allocated for the old
strings can be collected later on.And my aplication breaks :(
>
Kaj

Similar Messages

  • Can I combine two partitions without losing data on one?

    So if I have two partitions on one hard drive can I combine them without losing the data that is on one of them?  If not, could I just do a time machine backup, combine the two partitions, and then restore the time machine backup?  Would that work?

    If the other partition was created with BootCamp it can be deleted in Bootcamp and merged back into OS X.
    It might be possible to rename the partition "BOOTCAMP" in Disk Utility and formatted MSDOS to trick Boot Camp into thinking it is a Bootcamp parititon and merging it back into OS X. (haven't tried this so I don't know if it works or not yet) I would first make a clone below before attempting this method.
    TimeMachine restores are fraught with problems on these forums.
    What I would do in your case is use another equal or larger sized (than your primary boot drive), blank, Disk Utility HFS+Journaled formatted, external powered drive to Carbon Copy Clone your OS X boot partition to it, then Disk Utility > Repair Permissions on both (you can ignore the list that shows).
    What this does is gives you a "hold the option key" bootable clone, which while booted from it, check it out first and then use Disk Utility to completey (select the hard drive name) format the entire primary boot drive (HFS+ Journaled of course).
    Then simply CCC back onto the primary boot drive. Then run ALL of OnyX's maintainence and cleaning aspects and reboot. Speedy machine here we come.   (note: Cloning optimizes the drive by the order of the alphabetical order files in the Root Level, leaving "Users" last where the most change and slowdown in drive performance occurs, OnyX cleans out the caches and rebuilds Spotlight, more performance)
    http://www.titanium.free.fr/
    The new version of CCC has gotten a bit more complicated, including a archiving of files changed from between updated clones, however the default settings are fine for your first time clone to a blank drive. So all you really have to do is select the "From" and "To" drives and click Clone.
    Clones are wonderful, it's a exact copy of whats on your primary boot drive, so far in the many years I've used clones everything has worked on the same machine, everything.
    TimeMachine drives are not bootable, leaving you stuck if your hard drive dies or you can't boot from the OS X install disks.
    You can first make a clone so you have that method, then c boot off the 10.6 installer disk and use TimeMachine that way to restore to the newly formatted drive (use Disk Utility under the Utilities menu, second screen in) I haven't tried this myself, prefering clones instead as it's less headaches.
    Remmeber to ALWAYS have two backups of your data at all times, (erasing your drive leaves you with only one copy) in this case you would have a clone and a TM backup, in case one method fails (less likely the clone), then you got the other method.

  • How can I get my email to show on two machines without losing my folders.

    My husband and I just got new laptops. We each have two email accounts -- one gmail and one comcast. We also share a third gmail account. With all four computers (the old two and the new two running), for some of the accounts email only shows on one or another machine not both. It turns out my husband had the shared account set up as IMAP and all the others, including my set-up of the shared account are set-up as POP. In researching, it looks like we need to switch to POP. I tried it on the shared account and my emails up to about two weeks ago showed up. Before I do anything with our individual accounts, I want to be sure we won't get a bunch of duplication and that I won't lose all of the folders I have saved. The old machines are Vista and the new Windows 8.1. Thanks in advance for any help you can provide.

    Thank you so much. This is my last step moving to my new computer and I am weary of solving the problems. To clarify ----
    If I have my email set-up so that I have a filter that automatically moves a new email to a local folder, once I get it, it will then go there and I can access it even if I'm not connected to the network? The local folder and sub-folders are on my hard drive and the [email protected] is on the server?

  • Concatenation two strings or variables in EL

    <p>
    Hi,
    </p>
    <p>
    I'd like to display for all rows strings from resouce bundle:
    </p>
    <p>
    <font face="courier new,courier" size="2" color="#0000ff">#{row.Comm == null ? res['test.nocomm'] : res['test.hascomm']}</font> - it works.
    </p>
    <p>
    But I need concat string from resource bundle with another variable like bellow:
    </p>
    <p>
    <font face="courier new,courier" size="2" color="#0000ff">#{row.Comm == null ? res['test.nocomm'] <font color="#ff0000">sessionScope.UserName</font> : res['test.hascomm'] <font color="#ff0000">'Any String'</font>}</font>
    </p>
    <p>
    I get an exception: <font face="courier new,courier" size="2">javax.faces.el.ReferenceSyntaxException</font>
    </p>
    <p>
    How can I concat two strings or variables in EL
    </p>
    <p>
    Kuba
    </p>

    Hi,
    this doesn't work because EL doesn't concatinate strings bu evaluate them. The work around is to reference a managed bean that then returns the string you like
    #{row.Comm == null ? managed_bean.username_string : managed_bean.any_string'}
    Frank

  • I have a new iPod Touch and one apple ID. I want to use My laptop aswell as my desktop computer for the one ipod, using the same apple ID. I have music on both computers and want to merge the two without losing any music or apps. How do I do this?

    I have a new iPod Touch and one apple ID. I want to use My laptop aswell as my desktop computer for the one ipod, using the same apple ID. I have music on both computers and want to merge the two without losing any music or apps. How do I do this?
    My iPod has music and apps from my apple ID that I downloaded to my iPod via iCloud and some apps that I got free from the store, straight onto my iPod, but my laptop has some music on it aswell, separate from my other library. I want to put the music from my laptop into my iTunes library which I want to use on both computers. Is it possible to do this?
    Thanks.

    - You can only sync an iPod to one iTunes library/computer. You can however, manually manage music and videos among different libraries.
    Go to iTunes>Help>iTunes Help>Sync your iPod....>Sync You Device>Set up Syncing>Sync your device manually and follow the instructions.
    - If y wnat to switch syncing libraries/computer see:
    go to iTunes>Help>iTunes Help>Sync your iPod....>Sync You Device>Set up Syncing>Sync your device manually and follow the instructions.
    Syncing to a "New" Computer or replacing a "crashed" Hard Drive: Apple Support Communities
    If all the synced media like music is in the iTunes library of the second computer it is very easy.

  • I share an itunes account with my sister on two different computors. I want to create my own seperate account. Will I be able to sync my ipod with my new account without losing anything?

    I share an iTunes account with my sister, though on two seperare computors. I want to create my own account. Will I be able to sync my iPod with my newly created account without losing anything?

    as long as you make sure your iTunes is authorized for both Apple ID's, yes.

  • Can I use two different devices with different apple ID for backing up data on one machine without losing any data that was backed up with one device earlier?

    Can I use two different devices with different apple ID for backing up data on one machine without losing any data that was backed up with one device earlier?

    The link is to a discussion started on 12/18 in the FiOS Internet forum.  Here's the link I get now, however it is different than the link I pulled a few hours ago.  If this system changes the link again, it may not work.
    http://forums.verizon.com/t5/FiOS-Internet/Frustration-and-just-confused-with-internet-help-please/t...
    You can also look for the topic, "frustration and just confused" in the FiOS Internet forum.
    Here's a link that is in the thread that gives more detail.
    https://secure.dslreports.com/faq/15984
    Good Luck!
    P.S.  "Copper Contributor" is my "rank."  gs0b is my user name.
    If a forum member gives an answer you like, give them the Kudos they deserve. If a member gives you the answer to your question, mark the answer as Accepted Solution so others can see the solution to the problem.

  • Recently, I discovered I have two iCloud accounts. Is it possible to change the Apple ID from one account to match the second account, without losing the information on each account?

    Recently, I discovered I have two iCloud accounts. Is it possible to change the Apple ID for one of the accounts to match the other account, without losing the information from either account.
    thanks

    Welcome to the Apple Community.
    It's a little tricky to move data from one iCloud account to another but it can be done. But what makes you think you have 2 iCloud accounts.

  • HT4061 How do I consolidate purchases from two different ids that are on the same bank account, and eliminate the extra id without losing any purchases?

    How do I consolidate purchases from two different ids that are on the same bank account, and eliminate the extra id without losing any purchases? I want my music to match on both ipad and computer.

    Purchases are forever tied to the AppleID they were bought with. There is currently no way to consolidate AppleIDs. Sorry.

  • I am moving from PC to Mac.  My PC has two internal drives and I have a 3Tb external.  What is best way to move the data from the internal drives to Mac and the best way to make the external drive read write without losing data

    I am moving from PC to Mac.  My PC has two internal drives and I have a 3Tb external.  What is best way to move the data from the internal drives to Mac and the best way to make the external drive read write without losing data

    Paragon even has non-destriuctive conversion utility if you do want to change drive.
    Hard to imagine using 3TB that isn't NTFS. Mac uses GPT for default partition type as well as HFS+
    www.paragon-software.com
    Some general Apple Help www.apple.com/support/
    Also,
    Mac OS X Help
    http://www.apple.com/support/macbasics/
    Isolating Issues in Mac OS
    http://support.apple.com/kb/TS1388
    https://www.apple.com/support/osx/
    https://www.apple.com/support/quickassist/
    http://www.apple.com/support/mac101/help/
    http://www.apple.com/support/mac101/tour/
    Get Help with your Product
    http://docs.info.apple.com/article.html?artnum=304725
    Apple Mac App Store
    https://discussions.apple.com/community/mac_app_store/using_mac_apple_store
    How to Buy Mac OS X Mountain Lion/Lion
    http://www.apple.com/osx/how-to-upgrade/
    TimeMachine 101
    https://support.apple.com/kb/HT1427
    http://www.apple.com/support/timemachine
    Mac OS X Community
    https://discussions.apple.com/community/mac_os

  • How do I combine two iPhoto libraries into one without losing pictures or creating duplicates?

    How do I combine two iPhoto (iPhoto 09) librairies into one without losing pictures or creating duplicates?

    Two ways - use Aperture or use  iPhoto Library Manager - http://www.fatcatsoftware.com/iplm/ -
    LN

  • I have two identical Mail accounts. How can I delete one without losing all the mail in both accounts?

    I have two identical Mail accounts.  Don't know how that happened -- the second one just showed up one day. If I try to delete one account, I get a warning that all mail in the accounts will be permanently lost.  How can I delete one of the duplicate accounts without losing messages in both accounts?

    Download the yahoo email app, if you already deleted and reinstalled the email account and still have issue.
    This is a known issue with Yahoo email account.

  • If i have downloaded ( different ) apps in two iphones how can i add new apps in either of them without losing the older ones?

    i have two iphones 3g one at 4.1 and other at 3.1.3.....i have downloaded through itunes 30 apps in one and 55 different apps in other .......now how can i add 10 more apps in either of them without losing the previous apps .....if i don't have the previous apps downloaded now in my laptop..................?

    You can download the Apps on other devices by logging into the App Store with the ID you downloaded the App with

  • HT4914 If my phone's memory is full, mostly due to music, how can I make it to where my IPad holds all the music and remove all of it from my phone without losing any of it??

    If my phone's memory is full, mostly due to music, how can I make it to where my IPad holds all the music and remove all of it from my phone without losing any of it? I recently got an iPad so I now use that as my main source as opposed to my Dell. Yes, I have a Dell that failed me. Miserably.

    Sync your music from your computer to your iPad.  Then delete the music from your iPhone.

  • HT1476 I have problem with my Iphone 4S, only after updating the OS 7.X. My Iphone battery charge is losing down in a minute. My battery charge is just decreasing from 100% to 10% in less than two hours, without any application works. pls do help me out i

    I have problem with my Iphone 4S, only after updating the OS 7.X. My Iphone battery charge is losing down in a minute. My battery charge is just decreasing from 100% to 10% in less than two hours, without any application works. pls do help me out in this.

    Hi melvin victor,
    Welcome to the Support Communities!
    The information below may help you troubleshoot your iPhone battery issue:
    iPhone Battery and Power - Apple Support
    http://www.apple.com/support/iphone/repair/battery-power/
    Battery & power
    Is your iPhone battery draining too quickly or not holding a charge? Try restarting it first. You can also adjust several settings to extend the battery life of your iPhone.
    I hope this information helps ....
    - Judy

Maybe you are looking for

  • How can i make the dynamic image link field access camera

    This is for Windows 8 only and needs to use the built in camera of the tablet they will be using. I am not a developerso this seems incredibly difficult to me.

  • I have creative suite 5.5 design standard. I used to have a pc, however it totally died. I've now got a macbook pro. How do I transfer to mac?

    I purchased Creative Suite 5.5 design standard. I had a pc at the time. My PC laptop just died and i've now got a macbook. how to I transfer?

  • Broken SIM tray

    hi guys, i managed to insert the SIM tray in the wrong way (don't ask how... i was on a bus, wasn't paying attention etc). it got stuck, i pulled it out and part of it broke off. what should i do? i guess i should call apple, as there could be bits o

  • Setting nonProxyHosts property not working

    How do you set the nonProxyHosts property for Jrockit 1.5.0_11. We are trying to set this property for Aqualogic ESB which is using Jrockit by setting this property in net.properties under Jrockit but it does not seem to honor it. It seems to pick up

  • Psp help

    I ve just written a psp code using notepad text editor and saved it as a psp file ,i tried loading it with the loadpsp command line tool,but received the "unable to open file error" .I dont know what this is all all about .I am new to oracle applicat