Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Memory Issues

Thread view: 
kempshall - 17 Feb 2006 20:36 GMT
Is there any way to find out how the JVM is allocating memory?

In particular, I have a method that takes an open InputStream and an
open OutputStream as parameters and copies the contents of the
InputStream to the OutputStream. It looks something like this, with the
exception handling omitted for clarity:

byte[] buffer = new byte[1024];
int len;

while((len = inputStream.read(buffer)) >= 0) {
  outputStream.write(buffer, 0, len);
  outputStream.flush();
}

out.flush();

The problem is that when the InputStream is connected to a huge data
source, like inputStream = new FileInputStream("largeFile.dat"), the
method throws an OutOfMemoryError after processing somewhere between 37
and 38 megabytes. I'm trying to find the memory "leak," but I'm out of
ideas. Does anybody have some suggestions?

Thanks.
Madroadie - 17 Feb 2006 22:16 GMT
Here is how you find our how much memory you have at runtime

public void printMem()
{
    Runtime r = Runtime.getRuntime();
    long total = r.totalMemory();
    long used = total - r.freeMemory();
    proportion = ((double)used) / total;
    Double _total = new Double(((double)total) / 1024 / 1024);
    Double _used = new Double(((double)used) / 1024 / 1024);
    String text = f.format(new Object[] { _used, _total });
    System.out.println(text);
}

If you keep getting out of memory exceptions, give the Runtime engine
more memory when you start your app.
java -Xms96m -Xmx512m -jar myjar.jar
kempshall - 19 Feb 2006 22:36 GMT
Thanks for helping. I tried giving the JVM more memory when it started
up, but that didn't seem to work. Of course, there's always the strong
possibility that I didn't do that correctly -- I'm working with an
applet and the Java control panel, and sometimes its behavior is a
little unpredictable.
James McGill - 18 Feb 2006 01:09 GMT
> It looks something like this, with the
> exception handling omitted for clarity:

I think you've edited out the part that leaks, or there's something
memory intensive about whatever OutputStream you're using.  Your I/O
method doesn't use the heap proportionally to the number of iterations
of the loop, or to the size of the file.  
kempshall - 19 Feb 2006 22:34 GMT
Not exactly. I tried the code again, stripping out everything except
the code above, and I still ran out of memory. However, I tried
redirecting the data to System.out instead of the usual OutputStream,
and everything worked fine.... so I'm guessing that even though the
exception is thrown in my copyInputStream method, the actual issue is
in the calling method:

URL u = new URL('upload.asp');
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
OutputStream os = conn.getOutputStream();

copyInputStream(inputStream, os, inputStream.length);

and that 'upload.asp' file is, as best as I can determine, part of some
third-party uploading software. I don't speak ASP or VB and I can't
really make heads or tails out of it, but it looks like this:

<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")

path = Server.MapPath("/data")
Count = Upload.Save( path )
%>
</BODY>
</HTML>

Thanks for the help.
Roedy Green - 18 Feb 2006 13:58 GMT
>Is there any way to find out how the JVM is allocating memory?
see http://mindprod.com/jgloss/profiler.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.