CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2006
    Posts
    52

    Managing many objects in memory

    I am creating an application that goes through about 400 objects one by one calling functions in a loop. This executes every 10 minutes. When the program is initially started the object properties are collected from a database and then there is no further interaction with the database, until the program terminates and data from the objects is written to the database.

    At the moment I am holding these 400 objects in an array in memory and calling and modifying each one at whatever stage in the loop I am at.

    I was wondering if there was a more efficient way to do this. Should I keep reading and writing results to the database instead of keeping them in memory or is there another way?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Managing many objects in memory

    that's fine,. There's no need to to optimise something like that unless you *need* it to be faster and each individual object takes a long time to process or each individual object is massive (>500kB).
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Managing many objects in memory

    Quote Originally Posted by markyoung1984 View Post
    until the program terminates and data from the objects is written to the database.
    Not really an answer to your question, but I doubt this is a 'safe' way of handling your data. What happens when the computer/server crashes, then the data is not written to the database, and you possibly lost changes made to the data.

  4. #4
    Join Date
    Jul 2006
    Posts
    297

    Re: Managing many objects in memory

    My suggestion is that you save every modification to the database when it is made. Don't wait till the application shuts down to save them, like danny said, you will loose data at some point otherwise.

    Whether or not to store them in memory is a different story. That depends on how long it takes to load the required data to perform the modifications you're making. Make sure that each time you run it you need to modify all 400 objects, if you're only modifying 40 of the 400 then you could probably optimize it there.

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Managing many objects in memory

    Better to write to the database once every 100 objects are processed instead of waiting till the application ends to be 'safer' as told earlier by others.

  6. #6
    Join Date
    Jul 2006
    Posts
    297

    Re: Managing many objects in memory

    Quote Originally Posted by vcdebugger View Post
    Better to write to the database once every 100 objects are processed instead of waiting till the application ends to be 'safer' as told earlier by others.
    Why wait every 100? Nothing is stopping it from crashing at item 50, you're no better off doing it every 100 objects than you are updating at item 400.
    Last edited by monalin; July 22nd, 2009 at 11:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured