How common is the use of Boost's Shared Pointer class for a high scale architecture critical service?
-
I'm building a sort of middleware application (a caching server) in C++ and am not well versed in C++ best practices (I'm a long time .Net developer, C++ is more a "Side language" for me). Should I use shared_ptr (http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/shared_ptr.htm) to help prevent memory leaks or will it effect performance too much?
-
Answer:
It's a tough question to answer without profiling your code. I am generally weary of premature optimization. I would be surprised if the most significant bottleneck in your code is due to using shared_ptr instead of a raw pointer. I find that boost::shared_ptr is an (almost) stress-free way to utilize pointers without worrying about memory leaks or other unwanted behavior. Also, I'd think about how your code will be modified/maintained/used in the future -- will the added complexity of using raw pointers be a liability in the future when you have other people try to use or modify the code you're writing now? Generally, I am pro boost (I use it in production code all the time) - and I'd be surprised if it hurt your performance significantly, although I do not know your exact use case.
Alex Shvartser at Quora Visit the source
Other answers
To be able to answer this question completely, one would have to be able to do some analysis on performance characteristics of boost::shared_ptr. But in general, using boost::shared_ptr does not critically impede the performance of an application. Although, there have been cases (in my previous projects) wherein using boost::shared_ptr too much within a single server component resulted in significant performance impacts (concluded with performance analysis on the server component). This also depends on the criticality, availability and estimated-time-of-response requirements for that component. In such a scenario, it is safer to use boost::make_shared(). One of the major complains in using boost::shared_ptr is the time takes during construction. It allocates separate memory allocations for the object and and its corresponding control block. With boost::make_shared(), it provides a single allocation. Having said that, it is not too optimistic a statement to say that it is better to use boost::make_shared() most of the times. Also, you can refer http://herbsutter.com/gotw/_103/ Oh, yes! and using boost::make_shared() among other things did help with performance improvements in that project.
Nilesh Kumar
There are many considerations in designing and building a high-performance service. But I can speak from first-hand experience that Boost shared_pointer, and the other ptr classes in Boost, will make it easier to manage memory to the point that you'll probably not even miss the GC you're used to having.
Rick Gordon
Related Q & A:
- How To Use the Graph API to Upload Photos to a user’s profile?Best solution by Stack Overflow
- How to use an object created in Main class with another created in another class?Best solution by stackoverflow.com
- What colors should i use for a project of architecture?Best solution by Yahoo! Answers
- What's the difference between having a "high socioeconomic status" and being rich?Best solution by Yahoo! Answers
- How to join a high school's network on Facebook?Best solution by facebook.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.