简体   繁体   English

Java字节数组内存未释放

[英]java byte array memory not released

Why each time I press the button the memory usage increased since I have set the pointer to NULL? 为什么每次将按钮设置为NULL时,每次按下按钮都会增加内存使用量? (code attached) (附加代码)

This makes my program continues to increase in memory usage. 这使我的程序继续增加内存使用量。

Thanks. 谢谢。

package newHashFunction;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Memory_not_released extends JFrame{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Memory_not_released memory_not_released=new Memory_not_released();
    }

    Memory_not_released(){
        JButton button1=new JButton("create bytes");

        button1.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                byte[] byte1=new byte[10000000];
                byte1=null;
            }});

        add(button1);

        this.pack();
        setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

Setting your variable to null does not release the memory. 将变量设置为null不会释放内存。 Memory is only released when the garbage collector decides the memory is necessary for some other operation and must be recovered. 仅当垃圾回收器确定内存对于某些其他操作是必需的并且必须恢复时才释放内存。

It is entirely possible that the memory will never be released, even when the garbage collector has run multiple times. 即使垃圾回收器已经运行了多次,也很可能永远不会释放内存。

Update 更新资料

I think I understand where your concern for releasing memory is coming from. 我想我知道您对释放内存的担忧来自何处。 In Java, memory is allocated on the heap . 在Java中,内存是在堆上分配的。 Whether you realize it or not, Java is configured with a maximum heap size (you can specify this maximum on the command line). 不管您是否意识到,Java都配置有最大堆大小(您可以在命令行上指定此最大大小)。 Suppose Java is running with a maximum heap size of 100MB. 假设Java正在运行,最大堆大小为100MB。 Now, suppose your program has caused 5MB of that to be allocated. 现在,假设您的程序已分配了5MB。 The operating system sees Java using (approximately 5MB) because that's all Java has requested from the operating system at the point. 操作系统看到的Java使用了(大约5MB),因为这是此时Java向操作系统请求的全部内容。 As your program runs, more and more memory will continue to be allocated until the maximum heap size (100MB) is reached . 在程序运行时,将继续分配越来越多的内存, 直到达到最大堆大小(100MB)为止 At that point, most garbage collectors will be more aggressive in their memory releasing. 届时,大多数垃圾收集器将更加积极地释放内存。 Until then, many garbage collectors may simply not bother releasing the memory. 在此之前,许多垃圾收集器可能根本不会理会释放内存。

Therefore, you do not need to worry about Java consuming more and more memory from your operating system. 因此,您不必担心Java从操作系统中消耗越来越多的内存。

Every button press allocates 10,000,000 bytes. 每按一次按钮分配10,000,000字节。 Setting the variable to null will allow those bytes to be freed, but not until the next time the garbage collector runs . 将变量设置为null将允许释放这些字节,但要等到下一次垃圾收集器运行时才能释放。 When that happens may be arbitrary, and it is certainly not instant. 发生这种情况可能是任意的,并且肯定不是即时的。 You can try to force the VM to run the garbage collector by doing: 您可以尝试通过以下方式强制VM运行垃圾回收器:

byte[] byte1=new byte[10000000];
byte1=null;
System.gc();

这是因为在垃圾回收器启动之前,字节数组不会被破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM