java.lang.OutOfMemoryError:Java 堆空间,如何释放内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10675806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:03:14  来源:igfitidea点击:

java.lang.OutOfMemoryError: Java heap space, How can i free memory?

javaout-of-memoryheap-memory

提问by AR89

Here is the error

这是错误

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.JavaSoundAudioClip.readStream(JavaSoundAudioClip.java:328)
at com.sun.media.sound.JavaSoundAudioClip.loadAudioData(JavaSoundAudioClip.java:307)
at com.sun.media.sound.JavaSoundAudioClip.<init>(JavaSoundAudioClip.java:93)
at sun.applet.AppletAudioClip.createAppletAudioClip(AppletAudioClip.java:108)
at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:49)
at java.applet.Applet.newAudioClip(Applet.java:279)
at tetris.TetrisAudioPlayer.<init>(TetrisAudioPlayer.java:31)
at tetris.GamePanel.<init>(GamePanel.java:181)
at tetris.TetrisPanelsController.actionPerformed(TetrisPanelsController.java:155)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6375)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6140)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4737)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
at java.awt.EventQueue.access
import java.applet.*;
import javax.swing.*;
import java.net.*;


public class TetrisAudioPlayer extends JApplet {

private AudioClip song; // Sound player

private URL songPath; // Sound path

private boolean playing;

public TetrisAudioPlayer(String filename) {

    try {

        //songPath = new URL(getCodeBase(),filename); // Get the Sound URL

        songPath= this.getClass().getResource(filename);

        song = Applet.newAudioClip(songPath); // Load the Sound
    }

    catch(Exception e){} // Satisfy the catch

}

public void playSound() {
    song.loop(); // Play
    playing=true;
}

public void stopSound() {
    song.stop(); // Stop
    playing=false;
}

public void playSoundOnce() {
    song.play(); // Play only once
    playing=true;
}

public boolean playing(){
    return playing;
}
}
0(EventQueue.java:85) at java.awt.EventQueue.run(EventQueue.java:643) at java.awt.EventQueue.run(EventQueue.java:641)

Description of the problem: in the same frame I switch between JPanels, one of them has an Audioclip, if I switch back and forth between this JPanel and another I get this error. I want to free the memory from the JPanel with the Audioclip when I quit it, but seems that assigning null to it doesn't work, any idea? PS The AudioClip class extends JApplet, maybe it is the cause of the problem?

问题描述:在同一帧中,我在 JPanel 之间切换,其中一个具有 Audioclip,如果我在此 JPanel 和另一个之间来回切换,则会出现此错误。我想在退出时使用 Audioclip 从 JPanel 中释放内存,但似乎为它分配 null 不起作用,知道吗?PS AudioClip 类扩展了 JApplet,也许这是问题的原因?

As requested here is the portion of code that "leaks": This is the AudioClip

根据这里的要求是“泄漏”的代码部分:这是 AudioClip

        tetrisFrame.getContentPane().removeAll();
        tetrisFrame.getContentPane().invalidate();

        gamePanel=null;

        mainMenuPanel= new MainMenuPanel(this);
        tetrisFrame.getContentPane().add(mainMenuPanel);
        tetrisFrame.getContentPane().validate();
        tetrisFrame.getContentPane().repaint();

And this is how I change the JPanels, gamePanel is the JPanel wich contains the AudioClip, the error occur only with the AudioClip, without it there is no problem with this code.

这就是我更改 JPanel 的方式,gamePanel 是包含 AudioClip 的 JPanel,错误仅发生在 AudioClip 上,没有它,这段代码就没有问题。

-Xms512m 
-Xmx1024m

Java -Xmx1024m ......

回答by Siva Charan

Have you tried by setting the JVM arguments with
1. -Xmsas the initial size of the heap
2. -Xmxwhich sets the maximum size of the heap

您是否尝试过将 JVM 参数设置为
1.-Xms作为堆的初始大小
2.-Xmx设置堆的最大大小

Increase the heap value based on your RAM

根据您的 RAM 增加堆值

Example:

例子:

Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
System.gc();

回答by JHS

You might have to assign more memory. Assigning an object to null just marks it to be cleared by the gc.

您可能需要分配更多内存。将对象分配给 null 只是将其标记为由 gc 清除。

Have a look at this link

看看这个链接

回答by Yogesh Rathi

Call below method in onDestroy() or onPause()

在 onDestroy() 或 onPause() 中调用以下方法

public void freememory(){
    Runtime basurero = Runtime.getRuntime(); 
    basurero.gc();
}

But java internally call System.gc() method this is not correct way to free memory , but using above code memory was clear

但是java内部调用System.gc()方法这不是释放内存的正确方法,但是使用上面的代码内存很清楚

回答by emg

How to free memory in java:

java中如何释放内存:

##代码##

Call this twice to get better results.

调用两次以获得更好的结果。