I get an OutOfMemoryException while executing the following code, there are 584678 MyState entries:

var toRemove = Query.All<MyState>().Where(s => s.Id != s.Owner.CurrentState.Id);
Session.Current.Remove(toRemove);

The pseudo schema below:

public class MyEntity:Entity
{
    [Field, Key]
    public long Id { get; set; }

    [Field, Association(PairTo = "Owner", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)]
    public EntitySet<MyState> StateCollection { get; set; }

    [Field]
    public MyState CurrentState { get; set; }
}

public class MyState : Entity
{
    [Field, Key]
    public long Id { get; set; }

    [Field]
    public MyEntity Owner { get; set; }
}

Is that the expected behaviour ?

asked Jul 09 '13 at 11:08

Benoit%20Nesme's gravatar image

Benoit Nesme
43202024


2 Answers:

Hello Benoit!

I can't say exactly with provided information.

However here are some ideas:

Session.Remove operates on each entity separately, so your code loads all relavant MyState objects and then removes them by one. It might hit the memory limit dependending on size of your entities and other factors.

Our bulk operations extension supports server-side removal of entities. However it is incapable of processing paired assocation logic, so you will need to do this by manually (if you have paired assocations). Also executing bulk operation resets your session cache. This will degrade performance if you plan to access entities after that.

Alternatively you could try to remove entities in smaller batches:

bool done;
do {
   var toRemove = Query.All<MyState>()
     .Where(s => s.Id != s.Owner.CurrentState.Id)
     .OrderBy(s => s.Id)
     .Take(1024)
     .ToList();
   Session.Current.Remove(toRemove);
   done = toRemove.Count == 0;
} while (!done);

answered Jul 09 '13 at 11:58

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

Hello Denis,

Thanks for your quick answer.

As this is the expected behaviour and the removal using Session.Current.Remove processes every entity one by one, I am going to use the bulk operations extension.

answered Jul 09 '13 at 12:03

Benoit%20Nesme's gravatar image

Benoit Nesme
43202024

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA