aspose file tools*
The moose likes Applets and the fly likes OutOfMemory Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "OutOfMemory Error" Watch "OutOfMemory Error" New topic
Author

OutOfMemory Error

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I've been working on a project that uses Servlets on the server-side and an Applet on the client-side. The applet contains a JList. When loading, the applet sends a request to a servlet for data from a database. The servlet obtains the necessary records and loads a 2D array. I made a serialized class named TableData which wraps the 2D array. The servlet then sends the TableData object out the wire using the writeObject() method. The applet then receives the TableData object with the readObject() method and extracts the 2D array in order to load the JList.

I rudely discovered that this approach only works if the data being sent consists of only 15 - 20 records from the database. Otherwise, anything greater than 20 records, I wind up with this OutOfMemory Error message. This is not good! I hate these kind of gotchas!

I'm sure there must be many out there who have loaded JLists or JTables with data in applets before. Is there a better approach to this problem than the one I just described that avoids this OutOfMemory error?

Alan
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Alan,

Here are my thoughts on this,

- To resolve OutOfMemory error you need to analyse your memory requirements in details and figure out any extra memory not being used and try to reduce usage of memory via some simple things like making sure you don't end up creating unnecessary objects and all.

- Try to use -Xmx and -Xms JVM parameters for client JVM (applet jre). Though at the moment I don't recall how to do it for JRE running in the browser ...long time did any applets..

- If your data is reallly huge that comes to applet it won't be sufficient to use -Xmx params to increase the maximum heap size as ultimately you will have machine's RAM restriction anyways..so I would suggest that you should try to see if you can fetch data on demand like Previous , Next functionality or something...

OutOfMemoryError is really program and probably a particular process specific issue. You might see this error at different points in execution at different times..so we have to look for our memory requirements/constraints and take up some apporach or compromise certain things to get things going...

Regards,
Maulin
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

Originally posted by Maulin Vasavada:

- Try to use -Xmx and -Xms JVM parameters for client JVM (applet jre). Though at the moment I don't recall how to do it for JRE running in the browser ...long time did any applets..


For JRE 1.5, open up the Java Control Panel (On Windows: Start->Settings->Control Panel->Java), click on the tab marked "Java", in the box marked "Java Applet Runtime Settings" click on "View". Enter your parameters in the table cell marked "Java Runtime Parameters". The default maximum heap for an applet is 16 Megabytes.
Keep in mind that changing memory parameters will not cure real memory leaks (i.e. allocating tons of objects and keeping references to the so they cannot be freed). It will only delay their appearance.


[How To Ask Questions On JavaRanch]
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
I already tried changing the Java Runtime Parameters property to -Xlx128m but that had no effect. I'm currently trying another approach. Instead of sending all records at once as a 2D array. I'm going to attempt to send the data in the same manner but only one record at a time. This will require a looping structure at each end (server-side and client-side). I've decided on the following code. What do you think?

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

Originally posted by Alan Shiers:
I already tried changing the Java Runtime Parameters property to -Xlx128m but that had no effect.


I hope that's a typo.

Originally posted by Alan Shiers:

I'm going to attempt to send the data in the same manner but only one record at a time.

That should make no difference. You are still sending the same amount of data. I'll bet you are doing something in your code to exhaust memory. Try putting some System.out.println's in there. Watch those loop variables. Print out some id to indicate each record being processed. I bet your code doesn't do what you think it does (i.e. in the above code you declare RecordData to be a 2D array, but you only read the first record out of it).
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237

That should make no difference. You are still sending the same amount of data. I'll bet you are doing something in your code to exhaust memory. Try putting some System.out.println's in there. Watch those loop variables. Print out some id to indicate each record being processed. I bet your code doesn't do what you think it does (i.e. in the above code you declare RecordData to be a 2D array, but you only read the first record out of it).


Yes, RecordData is declared as a 2D array because the getData() method of the TableData object returns a 2D array. That's not a biggy! On the server-side I use the setData(String[][] data) method of the TableData object to load one record. Before I was using it to load all the records. But we all know what happened when I did that.

I can certainly put some println statements inside the looping structure, but all that'll prove is that, if it prints, I got the record. Where she cacks is on the readObject() method. What I'm realizing so far is that when I send TableData over the wire fully loaded with lots of records from the applet to the server using the writeObject() method, that doesn't generate any problems on the way out of the applet. It only seems that when I use readObject() to retrieve a TableData object that is fully loaded that the applet complains about OutOfMemory. This is why I'm thinking of trying things this way, with the looping structure, to receive the TableData object multiple times (containing one record at a time) hopefully without causing an OutOfMemory error. I haven't tried it yet, but I'll let you know how it turns out.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Well, I'm happy to report that sending a TableData object for every record worked. I no longer get any OutOfMemory errors from the applet. In going through this process, I kept thinking if there is even a better way to do all this. Then the idea of making use of xml came to mind. What do you think? Would it be better to send xml files back and forth with the data?

As it stands now, here's is the code that seems to be working now:

***********Method from the applet***********************


***********************Server-side Servlet******************
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutOfMemory Error