我设置了一个服务器,当用户打开我的应用程序时,它会检查文件服务器的更新,并在必要时下载它们。
但是,我注意到随着时间的推移,当人们连接和下载时,服务器的内存使用量会逐渐增加,只有当我重新启动打开套接字来托管文件的Java程序时,服务器的内存使用量才会下降。
该计划的大部分内容如下:
var parameters: [String:Any]?
//fill your parameters with data. Image is stored as Data and other values are string in this case.
Alamofire.upload(multipartFormData: { (multipartFormData:MultipartFormData) in
for (key, value) in parameters! {
if key == "userImage" {
multipartFormData.append(
value as! Data,
withName: key,
fileName: "profileImage.jpg",
mimeType: "image/jpg"
)
} else {
//multipartFormData
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
}
}, usingThreshold: 1, to: "yourServiceURL", method: .post, headers: ["yourHeaderkey":"value"]) { (encodingResult:SessionManager.MultipartFormDataEncodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if response.result.error != nil {
return
}
if let data = response.result.value {
let json = JSON(data)
}
}
break
case .failure(let encodingError):
debugPrint(encodingError)
break
}
}
变量定义如下:
@Override
public void run() {
try {
int archiveCount = in.readByte();
while(archiveCount-- > 0) {
int nameLength = in.readInt();
byte[] name = new byte[nameLength];
in.read(name);
int size = in.readInt();
clientLengths.put(new String(name).toLowerCase().trim(), size);
}
if(!clientLengths.isEmpty()) {
int count = 0;
for(String fileName : lengths.keySet()) {
if(!clientLengths.containsKey(fileName) ||
(long)clientLengths.get(fileName) != (long)lengths.get(fileName)) {
missing.add(fileName);
count += lengths.get(fileName);
}
}
if(count == 0) {
out.writeByte(0);
} else {
out.writeByte(1);
byte[] archive = readFile(archiveZip);
out.writeInt(count);
readFully(new ByteArrayInputStream(archive), out, archive.length);
}
} else { //hit if cache doesnt exist
out.writeByte(2);
byte[] cache = readFile(cacheZip);
out.writeInt(cache.length);
readFully(new ByteArrayInputStream(cache), out, cache.length);
}
destroy();
} catch(Exception e) {
e.printStackTrace();
}
}
private void destroy() throws IOException {
System.out.println("Finished cache handling from: " + socket.getInetAddress());
missing.clear();
clientLengths.clear();
socket.close();
}
首先打开套接字的单独类中的主循环如下:
private final Socket socket;
private final DataInputStream in;
private final DataOutputStream out;
private final List<String> missing = new ArrayList<>();
private final HashMap<String, Integer> clientLengths = new HashMap<>();
private final static HashMap<String, Integer> lengths = new HashMap<>();
private final static File dir = new File("./Data/Archive");
private final static File archiveZip = new File("./Data/Archive.zip");
private final static File cacheZip = new File("./Data/Cache.zip");
有没有人知道为什么内存使用率会持续上升?任何提示将不胜感激。谢谢!
答案 0 :(得分:0)
所有关闭/销毁操作应该在finally
子句中执行。那个肯定会被执行。