locked
Optimize caching serialization RRS feed

  • Question

  • Hi

    I'm currently using the Azure Shared Caching (may be using dedicated cache roles later, but that's beyond the point).

    I'm putting a lot of objects into the Cache, frequently. I am suspecting that because of that (and by looking at memory profiling), I'm running into memory leaks.

    Is there any way to control how objects are serialized/deserialized to/from the DataCache?

    Ideally I'd like to pass a Stream and deserialize the Stream myself, since I'm using Json rather than Xml serialization.

    Currently I'm serializing my objects into (json) Strings and passing those as values to cache, which I suspect are then further serialized to XML, and same process the other way around, which is pretty inefficient (when done frequently).

    Thanks,

    Stevo

    PS: I've read this http://msdn.microsoft.com/en-us/library/windowsazure/hh916611.aspx and there is nothing on this.

    Friday, June 7, 2013 8:56 PM

Answers

  • Hi,

      >> The memory leaks are in my code due to high amount of relatively long Strings (de)serialized.

    Since you've found the cause of memory leak. Please try to use StringBuilder instead of string if possible.

      >> Hence I've swapped to the Dedicated Role cache and implemented Json Serialization

    For dedicated role cache, you can try to use binary serialization, which can be more efficient than either net data contract or json serialization. Json provides the capability of interoperability, which is usually not a requirement for cache.

      >> I'm using the default NetDataContractSerializer for SessionState and Json for the rest. 

    It is still needed to use net data contract for session states. As documented(provided in my previous reply), the ASP.NET providers for Caching do not support binary or custom serialization types.

    Best Regards,

    Ming Xu


    Ming Xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 12, 2013 9:20 AM

All replies

  • Hi,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 10, 2013 9:55 AM
  • Hi,

      >> Is there any way to control how objects are serialized/deserialized to/from the DataCache?

    Based on my understanding, you can use custom serialization. But before taking on that route, please try to use the binary serializer, which can improve performance. I'd like to suggest you to check http://msdn.microsoft.com/en-us/library/windowsazure/jj663548.aspx for more information.   

      >> I am suspecting that because of that (and by looking at memory profiling), I'm running into memory leaks.

    In addtion, even if you use the default NetDataContractSerializer, it would not encounter memory leak. It would be better if you can provide more information about the memory leaks(What makes you think you have memory leaks). Shared caching stores cached items on each instance in memory, then high memory usage makes sence.

    Best Regards,

    Ming Xu


    Ming Xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 11, 2013 8:01 AM
  • Hi Ming Xu,

    That option doesn't seem to be available on Azure Shared Caching, which is the one I was using. Hence I've swapped to the Dedicated Role cache and implemented Json Serialization.

    One gotcha with that is since I'm using the same cache in web role, I was having problems serializing the SessionState (json would not deserialize), so I'm using the default NetDataContractSerializer for SessionState and Json for the rest. 

    The memory leaks are in my code due to high amount of relatively long Strings (de)serialized. 

    Shared caching stores cached items on each instance in memory, then high memory usage makes sence.

    I think you mean in-role shared cache. I was using the original Azure Shared Cache, not in-role.

    Best Regards,

    Stevo

    Wednesday, June 12, 2013 8:40 AM
  • Hi,

      >> The memory leaks are in my code due to high amount of relatively long Strings (de)serialized.

    Since you've found the cause of memory leak. Please try to use StringBuilder instead of string if possible.

      >> Hence I've swapped to the Dedicated Role cache and implemented Json Serialization

    For dedicated role cache, you can try to use binary serialization, which can be more efficient than either net data contract or json serialization. Json provides the capability of interoperability, which is usually not a requirement for cache.

      >> I'm using the default NetDataContractSerializer for SessionState and Json for the rest. 

    It is still needed to use net data contract for session states. As documented(provided in my previous reply), the ASP.NET providers for Caching do not support binary or custom serialization types.

    Best Regards,

    Ming Xu


    Ming Xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 12, 2013 9:20 AM