Memory Issues

K

kempshall

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.
 
M

Madroadie

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
 
J

James McGill

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.
 
K

kempshall

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.
 
K

kempshall

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top