Sign in to follow this  
Kevinator

[java] Memory Problems

Recommended Posts

Hi, my problem is that I'm running out of heap space when I'm only about 20% complete with my problem, so increasing the space allocated to the JVM isn't exactly a valid solution. Here's what I'm basically doing:
String aHugeString="";
Foo[] foo=new Foo[12000];
for(int i=0; i<12000; i++)
{
  aHugeString=getHugeString(); //string is roughly 32kb
  foo[i]=new Foo(aHugeString.substring(1323,1335)); //each element of foo contains roughly 30 bytes
}





It can't be the array, can it? It seems to me that there should be plenty of heap space for the array. What I'm thinking is that the string isn't getting garbage collected or something.. I don't know why it wouldn't, though.. So please, if there is something obviously stupid I'm doing please set me straight. This really shouldn't be happening.. Progress update: I tested out a mockup array without all the other string processing and it does just fine. I'm currently testing a version with periodic manual garbage collection and we'll see how it goes. update 2: the manual gc doesn't make a difference.. I'm stuck. update 3: I've been scouring the internets for a solution, and came across THIS.
Quote:
As such, substring() calls are incredibly fast: you're just allocating a new object and copying a pointer and two int values into it. On the other hand, it means that if you use substring() to extract a small chunk from a large string and then throw the large string away, the full data of that large string will continue to hang around in memory until all its substrings have been garbage-collected. Which could mean you carrying around the complete works of Shakespeare in memory, even though all you wanted to hang on to was "What a piece of work is man!"
O_o Wow. That... that sucks. Trying to find a workaround, as I think this is what is plaguing my app. [Edited by - Kevinator on August 14, 2008 12:07:35 AM]

Share this post


Link to post
Share on other sites
Transfer the information with an array of char.


String aHugeString="";
Foo[] foo=new Foo[12000];
char temp[] = new char[12];
for(int i=0; i<12000; i++)
{
aHugeString=getHugeString(); //string is roughly 32kb
aHugeString.getChars(1323,1335, temp, 0);
foo[i]=new Foo(String.copyValueOf(temp)); //each element of foo contains roughly 30 bytes
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this