This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes OutOfMemoryError when creating array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "OutOfMemoryError when creating array" Watch "OutOfMemoryError when creating array" New topic
Author

OutOfMemoryError when creating array

Diego Klappenbach
Ranch Hand

Joined: Aug 29, 2003
Posts: 31
Hi all,

Here I was, having fun with assignment Java-4b... I had finally came up with a small method that I could use in increasingly bigger for loops to initialize all the numbers in the array. The method worked fine for numbers up to about 400,000 (tried it several times with different values in the for loop), then I got following complaint from the compiler:
Exception in thread "main" java.lang.OutOfMemoryError
( no stack trace available )
Obviously, my solution may work but it's clearly not very efficient, and therefore it crashes for large numbers.
Can someone give me a small pointer??
Thanks in advance,
Diego
Here's part of my code:

Please be careful when posting code snippets, Diego. If you post too much, it might affect other Drivees who are following behind you, and we want everyone to make their own mistakes! Someone will still be able to help you out with the little I've left, or they may think that is even too much and delete the rest. We'll see!
[ September 12, 2003: Message edited by: Diego Klappenbach ]
[ September 12, 2003: Message edited by: Carol Murphy ]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
Diego, you should know by now there are no pointers in java.

No, seriously, I've never seen this problem come up before. It doesn't mean it hasn't, it just means I haven't seen it. I'm sure one of the gurus around here can help you find a way through this tangle of briars! Hang on, help will arrive soon I'm sure. (Riding away into the sunset.....)
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
then I got following complaint from the compiler
Note that you probably didn't get an OutOfMemoryError from the compiler. It likely happened at runtimej, so the JRE was doing the complaining.
OutOfMemoryErrors can happen when something like a for loop never terminates.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Diego Klappenbach:
...
I had finally came up with a small method that I could use in increasingly bigger for loops to initialize all the numbers in the array. The method worked fine for numbers up to about 400,000...

I have a feeling that you're making an array that is waaaaaaaay too big. The arrays that you use for 4a and 4b could be initialized using a loop, but they're small enough to populate without a loop, element for element. Populating the arrays "manually", element for element, would be more readable too.
Think about what the output will be for the assignment and then try to nail down only the minimum of words that you need to cover all cases (0 to 99 for assignment 4a, for instance). Some words get used in more than one number. That's all you need to store in your array(s).
Maybe that helps?
[ September 13, 2003: Message edited by: Pauline McNamara ]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
P.S. If you can stand it, you might want to wait til you get through the nitpicking with 4a before attacking 4b. 4b is built on 4a so the tips you pick up in 4a will help make 4b just a little less painful.
Now that I think of it, I shouldn't make any promises about the pain level of 4b.
Greg Neef
Ranch Hand

Joined: Jun 16, 2003
Posts: 82
The pain level for 4a/4b is virtually infinite but your definitely barking up the wrong squirrel with your array population approach. 4a can be done with two arrays with no more than 20 elements in either... (Hopefully that isn't too much of hint to take all the fun out of it for you yet is more helpful than the kind of hints you get from the NitPickeres (who shall remain nameless) such as "There is a better way to do this" (I loved that one!) . :roll:


SCJP 1.4
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Greg Neef:
the kind of hints you get from the NitPickeres (who shall remain nameless) such as "There is a better way to do this" (I loved that one!) . :roll:

I'll have to remember that one! *evil laugh*
Diego Klappenbach
Ranch Hand

Joined: Aug 29, 2003
Posts: 31
Originally posted by Dirk Schreckmann:
then I got following complaint from the compiler
Note that you probably didn't get an OutOfMemoryError from the compiler. It likely happened at runtimej, so the JRE was doing the complaining.
OutOfMemoryErrors can happen when something like a for loop never terminates.

Dirk, of course you're right, it did happen at runtime (silly me).
Now, my first thought was that the loop went on initiating far more numbers than what I had intended, thus producing this error. But, I've checked and it does in fact only initiate the intended range of the array.
Could the OutOfMemoryError be thrown if a certain index in the array is initialized several times?
(My guess is "no". To initialize a variable several times does not mean that the computer assigns new memory; only that it overwrites the previous value. Correct??)
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20498
    ∞

Diego,
If you are still getting that error when you read this post, send me your latest version. paul javaranch.com
One of the things about the cattle drive is that we think there is a lot of education in the struggle to find the correct answer. And there is education in discovering that you can make the JRE crash. But struggling too much is a bad thing.
We normally don't help you with 4b until you are all done with 4a, but I think if you've managed to crash the JRE, that's an exception.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Diego Klappenbach
Ranch Hand

Joined: Aug 29, 2003
Posts: 31
Paul,
thanks a lot for your kind offer!
Yes, the error is still there.
Nevertheless, I will not take you up on your offer right now. Although I've already spent a considerable amount of time trying to solve the problem, I'm still not willing to throw in the towel. You see, I totally agree with you - the struggle for a solution IS the education. That's why I only asked for "a small pointer" and not the solution!
I'll wait for the nitpicks on 4a and try to cool down a notch. It's just that I'm eager to learn more, so usually I start with the next assignment before the first one has been approved...
(Moreover, if it wasn't for this error the difference between compile-time and runtime errors would not have been as clear to me! Also, I would not have thought about using several arrays.)
(Thanks Dirk and Greg!)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Now, my first thought was that the loop went on initiating far more numbers than what I had intended, thus producing this error. But, I've checked and it does in fact only initiate the intended range of the array.
Are you sure this is true for all three loops (the ones shown above)?
[ September 13, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Amy Phillips
Ranch Hand

Joined: Apr 02, 2003
Posts: 280
Diego,
Just a hint that might help you with this problem. Take a loop at previous posts on 4b I know I managed to find one in this forum that gave me the exact start I needed to eventually pass this....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutOfMemoryError when creating array
 
Similar Threads
Is it a safe assumption when it comes to THREADS?
UBB Code eye strain
Reflection & Interospection
Usefulness of this code....
Please Help