我有这个问题:
java.lang.OutOfMemoryError:物理内存使用率太高:org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:562) 处的physicalBytes = 1G > maxPhysicalBytes = 1G
即使我们解除分配每个指针对象并调用 GC,long Pointer.physicalBytes 仍在增加 - 我一直在监控 JVM 堆大小并且它处于控制之下,从未超过 20% 的使用率,这意味着释放执行得很好,但出于某种原因信息(真实的东西)没有被传递给 Poniter.physicalBytes (它永远不会减少)并且当它达到 Pointer.maxPhysicalBytes 的值时它会错误地抛出错误
看起来这是几周前修复的(https://github.com/bytedeco/javacpp-presets/issues/423),但即使在获得最新版本的JavaCPP(1.3.3)后我仍然遇到这个问题
这是我的代码:
import static org.bytedeco.javacpp.opencv_core.cvClearMemStorage;
import static org.bytedeco.javacpp.opencv_core.cvGetSeqElem;
import static org.bytedeco.javacpp.opencv_core.cvPoint;
import static org.bytedeco.javacpp.opencv_core.cvSize;
import static org.bytedeco.javacpp.opencv_imgproc.CV_AA;
import static org.bytedeco.javacpp.opencv_imgproc.cvRectangle;
import static org.bytedeco.javacpp.opencv_objdetect.cvHaarDetectObjects;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.CvMemStorage;
import org.bytedeco.javacpp.opencv_core.CvPoint;
import org.bytedeco.javacpp.opencv_core.CvRect;
import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.CvSeq;
import org.bytedeco.javacpp.opencv_core.CvSize;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_objdetect.CascadeClassifier;
import org.bytedeco.javacpp.opencv_objdetect.CvHaarClassifierCascade;
(...)
public class ObjectDetection {
private static CvMemStorage storage = CvMemStorage.create();
(...)
public static synchronized Detection detect(IplImage src, Configuration cfg) {
CvMemStorage storage = CvMemStorage.create();
CvSeq sign = cvHaarDetectObjects(src, cfg.cascade, storage, cfg.scale, cfg.neighbors, cfg.method.getVal(), cfg.minSize, cfg.maxSize);
int total_objs = sign.total();
for (int i = 0; i < total_objs; i++) {
BytePointer seqElem = cvGetSeqElem(sign, i);
CvRect r = new CvRect(seqElem);
CvPoint p1 = cvPoint(r.x(), r.y());
CvPoint p2 = cvPoint(r.width() + r.x(), r.height() + r.y());
cvRectangle(src, p1, p2, CvScalar.RED, 2, CV_AA, 0);
p1.deallocate();
p2.deallocate();
r.close();
r.deallocate();
seqElem.deallocate();
}
BufferedImage img = Images.toBufferedImage(src);
sign.deallocate();
src.deallocate();
storage.deallocate();
Pointer.deallocateReferences();
return new Detection(img, total_objs);
}
public static class Detection{
private BufferedImage img;
private int count;
private Detection(BufferedImage i, int c){
img = i; count = c;
}
public BufferedImage getImage(){
return img;
}
public int getObjectsCount(){
return count;
}
}
public static class Configuration{
private String configName;
private CascadeClassifier xmlFile;
private CvHaarClassifierCascade cascade;
private CvSize minSize;
private CvSize maxSize;
private double scale;
private int neighbors;
private Method method;
private Configuration(String configuration) throws JSONException, IOException{
configName = configuration;
JSONObject cfg = new JSONObject(new JSONTokener(new FileReader(new File(configuration+".cfg"))));
scale = cfg.getDouble("scale");
neighbors = cfg.getInt("neighbors");
method = Method.valueOf(cfg.getString("method"));
int min = cfg.getInt("min_size");
int max = cfg.getInt("max_size");
minSize = cvSize(min,min);
maxSize = cvSize(max,max);
xmlFile = new CascadeClassifier(configuration+".xml");
cascade = new CvHaarClassifierCascade(xmlFile.getOldCascade());
}
public void dealocate(){
xmlFile.deallocate();
cascade.deallocate();
minSize.deallocate();
maxSize.deallocate();
configs.remove(configName);
}
}
}
这是堆栈跟踪:
java.lang.OutOfMemoryError: Physical memory usage is too high: physicalBytes = 1G > maxPhysicalBytes = 1G
org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:576)
org.bytedeco.javacpp.Pointer.init(Pointer.java:121)
org.bytedeco.javacpp.opencv_core.cvPoint(Native Method)
br.com.irisbot.visualrecognition.haarcascade.ObjectDetection.detect(ObjectDetection.java:83)
br.com.irisbot.visualrecognition.haarcascade.ObjectDetectionServlet.doPost(ObjectDetectionServlet.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:500)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)