locked
How to avoid memory groth by Entities RRS feed

  • Question

  • Hi,

    in my current application I am logging incomming udp messages to a database using the EF. This task needs to be fast, that is why I am keeping my ObjectContext open. My workflow looks as follows:

    1. create datagram entity
    2. context.AddTo<myDatagramTable>
    3. context.SaveChanges.

    I assume, that this way the context keeps a reference to each created entity, right?
    If so, what would be the best way to "release" those entities, since they from now on are no longer needed in the App?

    Thanks for hints and help.

    Regards
    Rainer

    Tuesday, June 12, 2012 8:55 AM

Answers

  • or better still:

    using (var context = new yourcontext())
    {
        create datagram entity
        context.AddTo<myDatagramTable>
        context.SaveChanges.
    }

    • Proposed as answer by Cryo75 Tuesday, June 12, 2012 12:28 PM
    • Unproposed as answer by Cryo75 Tuesday, June 12, 2012 12:28 PM
    • Proposed as answer by Cryo75 Tuesday, June 12, 2012 12:29 PM
    • Unproposed as answer by Rainer Queck Tuesday, June 12, 2012 12:36 PM
    • Marked as answer by Rainer Queck Tuesday, June 12, 2012 12:54 PM
    Tuesday, June 12, 2012 12:28 PM

All replies

  • context = null;
    try
    {
    	context = get new context;
    	context.Connection.Open();
    	
    	//your code
    	1.create datagram entity
    	2.context.AddTo<myDatagramTable>
    	3.context.SaveChanges.
    }
    catch (Exception ex)
    {
    }
    finally{
    	context.Connection.Close();
    }
    	

    How about this;
    • Marked as answer by Rainer Queck Tuesday, June 12, 2012 12:44 PM
    • Unmarked as answer by Rainer Queck Tuesday, June 12, 2012 1:59 PM
    Tuesday, June 12, 2012 10:18 AM
  • or better still:

    using (var context = new yourcontext())
    {
        create datagram entity
        context.AddTo<myDatagramTable>
        context.SaveChanges.
    }

    • Proposed as answer by Cryo75 Tuesday, June 12, 2012 12:28 PM
    • Unproposed as answer by Cryo75 Tuesday, June 12, 2012 12:28 PM
    • Proposed as answer by Cryo75 Tuesday, June 12, 2012 12:29 PM
    • Unproposed as answer by Rainer Queck Tuesday, June 12, 2012 12:36 PM
    • Marked as answer by Rainer Queck Tuesday, June 12, 2012 12:54 PM
    Tuesday, June 12, 2012 12:28 PM
  • Hello tinmgaye,

    thanks for your effort. I have tried your solution, but that still keeps memory growing.
    I also tired Cryo75 solution, that is working.

    Below you find a list showing the effects....

    Regards
    Rainer

    5 Min Open/Close context (with GarbageCollection before Report)
    =======================================================
    12.06.2012 15:45:22 : MaxUsage = 64057344; Current = 60063744
    12.06.2012 15:45:32 : MaxUsage = 64581632; Current = 61341696
    12.06.2012 15:45:42 : MaxUsage = 64581632; Current = 59252736
    12.06.2012 15:45:52 : MaxUsage = 66805760; Current = 61386752
    12.06.2012 15:46:02 : MaxUsage = 68931584; Current = 62500864
    12.06.2012 15:46:13 : MaxUsage = 70807552; Current = 62672896
    12.06.2012 15:46:23 : MaxUsage = 76660736; Current = 66297856
    12.06.2012 15:46:33 : MaxUsage = 77303808; Current = 69668864
    12.06.2012 15:46:43 : MaxUsage = 79138816; Current = 67985408
    12.06.2012 15:46:53 : MaxUsage = 79556608; Current = 68632576
    12.06.2012 15:47:03 : MaxUsage = 80404480; Current = 69480448
    12.06.2012 15:47:13 : MaxUsage = 81985536; Current = 71548928
    12.06.2012 15:47:23 : MaxUsage = 84811776; Current = 73007104
    12.06.2012 15:47:33 : MaxUsage = 86212608; Current = 81633280
    12.06.2012 15:47:43 : MaxUsage = 88928256; Current = 82108416
    12.06.2012 15:47:54 : MaxUsage = 89772032; Current = 82636800
    12.06.2012 15:48:04 : MaxUsage = 94171136; Current = 83652608
    12.06.2012 15:48:14 : MaxUsage = 94171136; Current = 84291584
    12.06.2012 15:48:24 : MaxUsage = 94171136; Current = 84971520
    12.06.2012 15:48:34 : MaxUsage = 95834112; Current = 85766144
    12.06.2012 15:48:44 : MaxUsage = 97038336; Current = 86847488
    12.06.2012 15:48:54 : MaxUsage = 97542144; Current = 85958656
    12.06.2012 15:49:04 : MaxUsage = 98803712; Current = 88092672
    12.06.2012 15:49:14 : MaxUsage = 99672064; Current = 87298048
    12.06.2012 15:49:24 : MaxUsage = 100642816; Current = 88940544
    12.06.2012 15:49:34 : MaxUsage = 101560320; Current = 88182784
    12.06.2012 15:49:45 : MaxUsage = 102780928; Current = 89858048
    12.06.2012 15:49:55 : MaxUsage = 102928384; Current = 88834048
    12.06.2012 15:50:05 : MaxUsage = 104136704; Current = 90509312
    ->> Memory keeps growing

    5 Min with "Using" (create/dispose) with GarbageCollection befor Report
    ============================================
    12.06.2012 15:54:25 : MaxUsage = 60456960; Current = 55894016
    12.06.2012 15:54:35 : MaxUsage = 60518400; Current = 57597952
    12.06.2012 15:54:45 : MaxUsage = 60579840; Current = 55959552
    12.06.2012 15:54:55 : MaxUsage = 60579840; Current = 57520128
    12.06.2012 15:55:05 : MaxUsage = 60579840; Current = 57651200
    12.06.2012 15:55:15 : MaxUsage = 60579840; Current = 57585664
    12.06.2012 15:55:25 : MaxUsage = 60579840; Current = 57532416
    12.06.2012 15:55:35 : MaxUsage = 60579840; Current = 57675776
    12.06.2012 15:55:45 : MaxUsage = 60579840; Current = 55848960
    12.06.2012 15:55:55 : MaxUsage = 60579840; Current = 57487360
    12.06.2012 15:56:05 : MaxUsage = 60579840; Current = 57745408
    12.06.2012 15:56:15 : MaxUsage = 60579840; Current = 57548800
    12.06.2012 15:56:25 : MaxUsage = 60579840; Current = 55713792
    12.06.2012 15:56:35 : MaxUsage = 60579840; Current = 57561088
    12.06.2012 15:56:45 : MaxUsage = 60579840; Current = 57556992
    12.06.2012 15:56:55 : MaxUsage = 60579840; Current = 57491456
    12.06.2012 15:57:05 : MaxUsage = 60579840; Current = 57491456
    12.06.2012 15:57:15 : MaxUsage = 60579840; Current = 57491456
    12.06.2012 15:57:26 : MaxUsage = 60579840; Current = 57434112
    12.06.2012 15:57:36 : MaxUsage = 60579840; Current = 57573376
    12.06.2012 15:57:46 : MaxUsage = 60579840; Current = 56483840
    12.06.2012 15:57:56 : MaxUsage = 60579840; Current = 57430016
    12.06.2012 15:58:06 : MaxUsage = 60579840; Current = 55803904
    12.06.2012 15:58:16 : MaxUsage = 60579840; Current = 57503744
    12.06.2012 15:58:26 : MaxUsage = 60579840; Current = 57716736
    12.06.2012 15:58:36 : MaxUsage = 60579840; Current = 57511936
    12.06.2012 15:58:46 : MaxUsage = 60579840; Current = 55820288
    12.06.2012 15:58:56 : MaxUsage = 60579840; Current = 57589760
    12.06.2012 15:59:06 : MaxUsage = 60579840; Current = 57720832
    -->> stays quite constant.

    Tuesday, June 12, 2012 2:05 PM