Search code examples
javanetbeansout-of-memory

NetBeans is Showing Error file could cause outofmemoryerror netbeans


Hello I am using netbeans editor for my Java Desktop application and i am getting error i mean warning like this

opening this file could cause outofmemoryerror netbeans

This warning shows up because I have a file containing code which is >1MB. And after that I am not able to see error too its not pointing so how could i fix it?


Solution

  • What is the file size of the file you're trying to open? The only time I have seen this is when the file Netbeans is going to open is relatively large (not sure what threshold Netbeans uses to show this warning).

    If it's a large text file with data you're reading (or writing) with your software, open it outside of Netbeans with something like Notepad++.

    Update

    Okay, so it's clear now that a file that contains code is relatively large, and Netbeans is already warning you that it might be too large to be opened. I strongly recommend that you split that file into multiple files (multiple classes maybe)?

    Example:

    Old situation

    public class Class1
    {
        public void doSomething()
        {
            //do stuff (1)
            //do stuff (2)
        }
    }
    

    New situation

    public class Class1
    {
    
        private NewClass1 newClass1;
        private NewClass2 newClass2;
        private JComponent jComponent;
    
        public Class1()
        {
            newClass1 = new NewClass1();
            newClass2 = new NewClass2();
            //instantiating jComponent
        }
    
        public void doSomething()
        {
            newClass1.doSomethingSmaller(jComponent); //possibly with some return value
            newClass2.doSomethingSmallerToo(jComponent); //possibly with some return value
        }
    }
    
    public class NewClass1
    {
        public void doSomethingSmaller(JComponent jComponent)
        {
            //do stuff (1)
            jComponent.doStuff();
        }
    }
    
    public class NewClass2
    {
        public void doSomethingSmallerToo(JComponent jComponent)
        {
            //do stuff (2)
            jComponent.doOtherStuff();
        ]
    }
    

    Those new classes should then be in separate files, which reduces the size of your file. It also encourages reusing (parts of) your code in other places in your code.