无法分配小内存“java.lang.OutOfMemoryError:无法分配3000字节”

时间:2017-06-15 03:01:26

标签: java out-of-memory

public static void sendScenarioToEdc(String connectionType, String ipOrBaudrate, String portOrCom, String timeout, String profile) {
    loadEcrLib();     
    File file =  new File (ApplicationConstans.PROFILE_FOLDER +profile+ ".csv");
    // create parameter for the dll
    String param = createParameter(connectionType, ipOrBaudrate, portOrCom, timeout);
    List<Transaction> transactions = new ArrayList<Transaction>();
    String[] messages = CsvUtil.readRequestMessage(file);
        for (String message : messages) {

            Transaction t = new Transaction();
            t.setRequestMsg(message);
            EcrInterface ecrLib = EcrInterface.INSTANCE;
            Pointer stringMem = new Memory(3000);
            stringMem.setString(0, " ");
            Pointer response = stringMem.getPointer(0);

            // notify the user before hand
            System.out.println("Request: "+ t.getRequestMsg());
            long time = System.currentTimeMillis();
            Integer status = ecrLib.procEdcTrans(message, response, param); //send message here
            t.setResponseStatus(status.toString());
            t.setResponseTime(System.currentTimeMillis() - time);
            t.setResponseMsg(response.getString(0).replace("ÿ", ""));   // trim 0x255 characters

            System.out.println("Response: "+ t.getResponseMsg());
            System.out.println("Time: "+ (t.getResponseTime() / 1000) + "." + (t.getResponseTime() % 1000) + " s");
            System.out.println("Status: "+ t.getResponseStatus());

返回错误java.lang.OutOfMemoryError:无法分配3000字节

为什么程序不能分配如此小的内存? 我还使用了这个参数“java -Xmx2048m -Xms512m”来配置内存堆,但这并没有解决问题。

这是跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Cannot allocate 3000 bytes
at com.sun.jna.Memory.<init>(Memory.java:70)
at com.kartuku.utils.SenderUtil.sendScenarioToEdc(SenderUtil.java:98)
at com.kartuku.ui.Main1.btnRunMouseClicked(Main1.java:581)
at com.kartuku.ui.Main1.access$000(Main1.java:31)
at com.kartuku.ui.Main1$1.mouseClicked(Main1.java:123)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

程序无法为指针stringmem

分配如此小的内存

1 个答案:

答案 0 :(得分:0)

此错误通常表示您的计算机内存不足。尝试让任务管理器同时打开并查看你的ram使用情况,如果它达到3mb以内,那就是问题。如果没有,那么Java很可能没有足够的空间进行操作。