gpt4 book ai didi

java - Java Applet 的内存消耗

转载 作者:行者123 更新时间:2023-12-02 01:50:31 26 4
gpt4 key购买 nike

在我的小程序中,我有 GET 调用来从远程位置下载文件。当我尝试下载一些大约 13MB 的大文件时,我的 Applet 内存消耗增加了 50MB 以上。我使用下面的代码来获取我的内存消耗:

public static long getMemoryUsage()
{
long memory = 0;
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
memory = runtime.totalMemory() - runtime.freeMemory();
return memory;
}

我的 get 调用代码是

public  void getFiles(String filePath, long fileSize)throws MyException
{
InputStream objInputStream = null;
HttpURLConnection conn = null;
BufferedReader br = null;
try
{
URL fileUrl=new URL(filePath);
final String strAPICall=fileUrl.getPath();
final String strHost="some.test.com";
final int iPort=1000;
URL url = null;
url = new java.net.URL
( "https",
strHost, iPort , "/" + strAPICall,
new myHandler() );

conn = (HttpURLConnection)new HttpsURLConn(url);
conn.setRequestMethod("GET");

conn.connect();

if (conn.getResponseCode() != 200) {

objInputStream=conn.getInputStream();

br = new BufferedReader(new InputStreamReader(
(objInputStream)));

String output;
while ((output = br.readLine()) != null) {
System.out.println(output);

}
throw new MyException("Bad response from server",
MyError.BAD_RESPONSE_ERROR);

}
else
{

notifyProgressToObservers(0);
System.out.println("conn.getResponseCode()"+conn.getResponseCode());
System.out.println("conn.getResponseMessage()"+conn.getResponseMessage());
objInputStream = conn.getInputStream();
int count=objInputStream.available();

System.out.println("Stream size: "+count);
System.out.println("fileSize size: "+fileSize);
byte []downloadedData = getBytesFromInputStream
(objInputStream, count,fileSize);
notifyChunkToObservers(downloadedData);
notifyIndivisualFileEndToObservers(true, null);

}

}
catch (MyException pm)
{
throw new MyException
(pm, MyError.CONNECTION_TIMEOUT);
}
catch (IOException pm)
{
throw new MyException
(pm, MyError.CONNECTION_TIMEOUT);
}
catch (Exception e)
{

notifyIndivisualFileEndToObservers(false,new MyException(e.toString()));
}
finally
{
System.out.println("Closing all the streams after getting file");
if(conn !=null)
{
try
{
conn.disconnect();
}
catch(Exception e)
{

}
}
if(objInputStream != null)
{
try {
objInputStream.close();
} catch (IOException e) {

}
}
if (br != null)
{
try {
br.close();
} catch (IOException e) {

}
}
}

}

在上面的方法中,我尝试将内存消耗的日志放在每一行后面,发现在conn.connect();之后,小程序的内存消耗至少增加了50MB,即使文件我尝试下载的大小只有 13MB。

有没有内存泄漏的地方?

编辑:添加了 getBytesFromInputStream() 的实现

public byte[] getBytesFromInputStream(InputStream is, int len, long fileSize)
throws IOException
{
byte[] readBytes= new byte[8192];
ByteArrayOutputStream getBytes= new ByteArrayOutputStream();

int numRead = 0;

while ((numRead = is.read(readBytes)) != -1) {
getBytes.write(readBytes, 0, numRead);
}


return getBytes.toByteArray();
}

最佳答案

这是因为这一行:

 byte []downloadedData = getBytesFromInputStream(objInputStream, count,fileSize);

这里您正在将文件的完整字节数读入堆中。之后,您需要跟踪该数组发生了什么。也许您正在将其复制到某个地方,并且即使您不再使用对该对象的引用,GC 也需要一些时间来启动。

大文件不应该完全读取到内存中,而应该直接流式传输到数据的某些处理器。

关于java - Java Applet 的内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com