File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Out of memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Out of memory" Watch "Out of memory" New topic

Out of memory

Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I have an applet that I am starting, that is eating all of the memory on my computer!!!
The applet consists of about 6 panels, with Checkboxes and Labels, and buttons to flip between the Panels.
One of the Panels has 5 more Panels within it, with more Labels and buttons to flip between those. When I view the applet it appletviewer, I get out of memory messages. When I view just the 1 Panel that contains the 5 panels and Labels, it eats up about 60% of the resources on my computer. I'm not sure exactly why... The full code for my applet is less than 100kb, and my computer has 64MB of RAM. Are Lables and Panels real memory hogs? I probably have about 100 Labels all told in the applet.
Could anyone help??
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
Without having written an applet as complex as you describe, I would still be surprised if your memory problems had anything to do with the layout.
My guesses would be:
a) That you're creating lots of new objects in a loop
b) The objects are probably strings, which can get created without an explicit call to 'new'.
c) These objects are being created in a loop.
For example, how many new objects are created by the following:

Just one - s? Wrong - 1000 String objects have been created.
A fourth guess of mine would be that the loop is implicit rather than explicit. For example, do you have a listener to display current mouse coordinates? If so, you could be creating a new String every time the mouse moves from pixel to pixel; Now that WOULD eat up memory.
The solution would be to track down where these Strings are being created, and then perhaps replace the culprits with StringBuffer.
Hope this helps.
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I'm not creating strings in a loop. What I am doing is creating arrays of strings and passing thoose into one of the Panels and using it to create Labels in a loop.
Also in that same loop I am setting the constraints for the Label, and adding it to the layout (using GridBagConstraints, don't know if this might part be it). Everything does appear correctly, so I know I'm not creating more Labels then I want. The 1 thing I can think of that I can try, is that after I use the strings to create the Labels, I still have the strings floating around. Maybe I can set them to null, and then the garbage collector will get them? I will try that and see if it helps.
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
Let us know if it works!

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I tried that, but it didn't seem to help, or at least if it did, not very much. Basically what I'm doing is making more or less a chart with Labels and GridBagLayout to be able to change the column widths depending on the data. I re-calculated my Labels, and I guess I have 200-300 of them. (whew! didn't know I had that many...)
Is there a better way of doing this, maybe? I don't know much about swing, and I was trying to avoid it to make my applet more compatible with older browsers, but if it's going to make a computer crash, I could learn swing to make it more efficient.
I still can't quite understand why my little app could take more system resources than a Microsoft app!!
I guess I can't bash them for being memory hogs until I can fix this!
[This message has been edited by Matthew Jones (edited January 16, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Feel free to bash Microsoft as much as you want. They have armies of programmers working on memory allocation, and you have just you (and JavaRanch of course).
Perhaps a JTable would solve your problem.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
I'm not sure going into Swing would make that much difference, if the problem is caused by excessive Strings being created within your setup code. Also, you can't rely on the garbage collector (as you've found out), or force the garbage-collector to run.
Nope, I reckon the only way is to track down WHERE the huge numbers of objects are being created.
OK, lets fix up the labels/constraints as you've indicated, and see how that fares. Again, offhand, I wouldn't expect 200-300 occurences to clobber your machine so much, but it's a start
Looking at GridBagConstraints first. The GridBag class copies the constraints you give it at every 'add', which means in your code you need only create a single instance of the constraints and then re-use it for each component (probably not a huge saving, but every little bit helps).
Now, lets look at the labels. How do you create the labels? Is it just like :

or more complex? Can you post the code?
One possible alternative to pre-storing the labels is to generate them on-the-fly in a StringBuffer. So, instead of generating the array of strings in your calling routine, you could instead set the single StringBuffer inside your panel, so:

Let me know how you go.
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I was looking at the API for JTable, and it looks really complex! Now I know why I haven't learned swing yet!
Unfortunatly, I can see myself having to completely re-write my code if I switch to swing, so lets see what else can be done...

I am creating just 1 GridBagConstraints object, and modifing it for every Label, so that shouldn't be the problem.
What I have is 1 class that extends Panel that is more or less a template for a bunch of info I want to display.
I pass in 8 string arrays in the constructor, and the values in the arrays are used to create the new Lables, then set the contraints, and add the Labels to the extended Panel.
Each of the arrays is basically 1 'column' and the number of rows in detemined by the length of the 1st array, which is the same as the rest of the arrays.
Here is the code for the loop that does most of the work:

I was thinking... a String takes up 32 bytes of data in memory, correct? So even if I did have 300 extra Strings floating around, it would only take up less than 10kb, that definatly shouldn't be causing a problem!! Does anyone know how much memory a Label takes? Or a Panel?
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
I agree - SOMETHING is really chewing up your memory. I still suspect string concatenation is somehow the problem (simply because it is the easiest to miss). How sre your string arrays setup?
I've also been following the thread "Most common costliest Elements of Java " in the performance group, and someone there mentioned a product called OptimizeIt (
Looking at the site, it's a product that tracks memory allocation/leaks, etc. Looks like a really good product, and you can download a full version for 2-week evaluation.
I agree. Here's the link:
subject: Out of memory
It's not a secret anymore!