• 12
• 12
• 9
• 10
• 13

# [java] Memory Problems

This topic is 3507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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=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]

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=new Foo(String.copyValueOf(temp)); //each element of foo contains roughly 30 bytes}