-1

In my app I have something like:

static void Main(string[] args)
{
  for(int i=0;i<1000;i++){
   MyObj mo=new MyObj();
   }
 }

When i=536 I'm getting: Unhandled Exception: OutOfMemoryException

I have tried to modify to:

 for(int i=0;i<1000;i++){
   MyObj mo=new MyObj();
   mo=null;
   }

How to handle this exception properly?

MyObj class looks roughly as follows:

    readonly string _url;
    readonly string _username;
    readonly string _password;
    //more properties here
    public MyObj(string username , string passowrd , string host )
    {
        _url = $"https://{host}";
        _username = username;
        _password = passowrd;

    }

    //upload file to server
    private void Upload(string path){
     //some code that upload the file
    }

    //get json string about htis file
     private void Info(string session){
      //some code here
     }
exe
  • 181
  • 1
  • 1
  • 10
  • 4
    What is MyObj? Instantiating 1000 objects shouldn't cause an OutOfMemoryException... – Stevo Jul 17 '18 at 11:01
  • 1
    What happens in the constructor of `MyObj`? – kedenk Jul 17 '18 at 11:02
  • Share `MyObj`'s constructor code please. I have the feeling the answer lies here. – Spotted Jul 17 '18 at 11:03
  • 1
    You can catch and handle it with `try{}catch{}`, but I dont really understand your question. What do you want to achieve? + What my obj is is irrelevant as long the question isn't "Why does it happen?". – Chrᴉz remembers Monica Jul 17 '18 at 11:03
  • Define the object outside the loop and just change the required properties. Implement IDisposable on MyObj and clean off any resource – NitinSingh Jul 17 '18 at 11:05
  • Look [here](https://stackoverflow.com/questions/4257372/how-to-force-garbage-collector-to-run). A object isnt automatically disposed once there's no pointer left pointing to it. It first needs to be collected by GC. After that the resources are free again. – Chrᴉz remembers Monica Jul 17 '18 at 11:07
  • Are you looking for a gracefully way to stop the loop? Or are you looking for a fix of the issue? Can you give more information about the intent of the app? – David Jul 17 '18 at 11:11
  • @David I want to dispose the object properly – exe Jul 17 '18 at 11:16
  • That's 500 lines of code.... – exe Jul 17 '18 at 11:18
  • I can assume that you need to free resources even if an OutOfMemory exception occurred. In this case, you must inherit your class from the [CriticalFinalizerObject](https://msdn.microsoft.com/en-us/library/system.runtime.constrainedexecution.criticalfinalizerobject(v=vs.110).aspx) class. – Alexander Petrov Jul 17 '18 at 11:25
  • 1
    Heres what id do, go and check every class you are using inside your class, check if it supports IDisposable, and put them in a using statement.. everything that can use a `using` statement use it. you obviously have a huge memory leak.. also look up memory profilers, to determine what is hanging around. – TheGeneral Jul 17 '18 at 11:33

1 Answers1

-1

With the few information that we have, I would advice to implement IDisposable on the MyObj and then adapt the for-loop:

for(int i=0;i<1000;i++)
{
    using(MyObj mo=new MyObj())
    {
        //Do something here
    }
}

The MyObj would look like:

class MyObj : IDisposable
{
    public void Dispose()
    {
       // Dispose of unmanaged resources.
       Dispose(true);
       // Suppress finalization.
       GC.SuppressFinalize(this);
    }   
}
Afonso
  • 323
  • 4
  • 14
  • let me try this – exe Jul 17 '18 at 11:30
  • I think this is the best bet and also, use a try-catch-finally outside the loop to catch the exception and handle any additional cleaning. – David Jul 17 '18 at 11:36
  • 1
    @David try catch doesn't solve the problem, that's only "escaping" the problem – exe Jul 17 '18 at 11:39
  • try-catch-finally is for additional finalization. For example, if there is a connection you need to close, or files you need to close. But I believe you should implement the IDisposable – David Jul 17 '18 at 11:49