2

Why each time I press the button the memory usage increased since I have set the pointer to 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);
    }

}
5
  • How do you know the memory is not released sometimes? Memory usage as seen from the OS should go back after a full GC, but chances are that a full gc has not yet felt the need to perform. Commented Mar 27, 2011 at 11:52
  • byte[] byte1=new byte[10000000]; byte1=null; System.gc(); Oh, it works. Though it doesn't collect until the 3rd click, it can stop my program from running out of memory. Thanks! Commented Mar 27, 2011 at 12:00
  • I don't quite understand. But I would like to use System.gc(). Commented Mar 27, 2011 at 12:02
  • @michali123 There are few reasons to manually call System.gc() since it should run whenever it needs to. Commented Mar 27, 2011 at 12:08
  • @Kurru Oh,you are right. I have tried again at another computer which has less memory. It turns out to stop increasing the memory after the third click without System.gc(). Commented Mar 28, 2011 at 3:15

3 Answers 3

6

Setting your variable to null does not release the memory. 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. Whether you realize it or not, Java is configured with a maximum heap size (you can specify this maximum on the command line). Suppose Java is running with a maximum heap size of 100MB. Now, suppose your program has caused 5MB of that to be allocated. The operating system sees Java using (approximately 5MB) because that's all Java has requested from the operating system at the point. As your program runs, more and more memory will continue to be allocated until the maximum heap size (100MB) is reached. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

What happens if your program exceeds the max heapsize? Does it crash?
@Kurru: Well, if the garbage collector cannot find any memory to release, then your program crashes with a java.lang.OutOfMemoryError. If your program reaches such a limit, it usually means that you are allocating massive objects or that you have a memory leak (yes, Java can have memory leaks). If the problem is massive objects, then you can just restart Java with a larger maximum heap size. For example, this command runs a Java class named Foo with 100MB of heap: java -Xmx100m Foo
3

Every button press allocates 10,000,000 bytes. Setting the variable to null will allow those bytes to be freed, but not until the next time the garbage collector runs. 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:

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

2 Comments

It works. Though it doesn't collect until the 3rd click, it can stop my program from running out of memory. Thanks!
Try the same without the System.gc() - you should not run out of memory either (but your program may consume more of its "max heap" size).
0

It is because the byte array will not be destroyed until Garbage Collector starts.

2 Comments

That's partially correct. You are right that the decision ultimately belongs to the garbage collector. However, the garbage collector may never release that memory if it doesn't need to.
I just didn't want to go deep in details. But of course, you are right, GC behavior fully depends on JVM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.