question

simplify3000-6685 avatar image
0 Votes"
simplify3000-6685 asked simplify3000-6685 answered

creating new objects in a static method

Hi all,

I have a static method as follows:

 public static class Helper
 {
 public static void WriteMessage (string message)
 {
    ///////////////
    var myObj = new MyObjectDetail();
    ///////////////
    myObj.message = message;
    myObj.TimeStamp = DateTime.UtcNow();
    Console.WriteLine(myObj.ToJsonFormat());
 }
 }

this will be called frequently as:
Helper.WriteMessage("method A started"); //etc.

My question is,
By creating a new object inside every call to the static "WriteMessage" (var myObj = new MyObjectDetail(); )
1. does that have a performance impact?
2. is there a possibility of data used incorrectly between threads or calls, e.g. call1 writing out a message meant for call2?

Thanks

dotnet-csharpdotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

does that have a performance impact?

Every code that runs has a performance impact. Since you've provided no relevant source code or your performance expectations, there is no way to answer this question.

is there a possibility of data used incorrectly between threads or calls, e.g. call1 writing out a message meant for call2?

Static methods are typically thread safe but you have not provided the MyObjectDetail source code so we have no idea if MyObjectDetail is thread safe.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

simplify3000-6685 avatar image
0 Votes"
simplify3000-6685 answered AgaveJoe edited

The core requirement is we no longer want to write out simple messages,
but format each message as a Json string and then write it to a log.

I meant is it "bad practice" to frequently call a static method that creates new instances of an object for every call.
Would it be better to not do this and whereever I have a call like Helper.WriteMessage("method A started");
- should I just create the new object there in each caller class, and forget about static methods.
(would be a lot of tedious code changes).

MyObjectDetails is a class that has about 20 properties, like below:
Most of the properties are of no use to my code, so we only set the message and TimeStamp.
There are no actual methods that do any processing other than getters and setters like below:

     public DateTime TimeStamp { get; set; }

     public EventId EventId { get; set; }

     public string ErrCode { get; set; } = string.Empty;

     public long Duration { get; set; }





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The core requirement is we no longer want to write out simple messages, but format each message as a Json string and then write it to a log.

As far as I can tell you're asking about performance specifications of code that has not been written. I cannot comment on performance without code and performance specifications. However, I recommend the using asynchronous programming to write the log.

Asynchronous programming with async and await

I meant is it "bad practice" to frequently call a static method that creates new instances of an object for every call.

It is impossible for a community member to answer this question. Keep in mind, you're asking this question as if the community knows how your application works. I assume instancing the object in the static method is what's needed to make the application work as expected. If the object has no bearing on functionality or has unused properties then remove the bit you do not need. Perhaps create a new type that only has the properties needed to accomplish the task.





0 Votes 0 ·
simplify3000-6685 avatar image
0 Votes"
simplify3000-6685 answered

Ok thank you for taking the time to answer, however I would like to ask other community members what they think.
I'm asking this question to see if anyone else has done anything similar in their own application and may have thoughts about it.
Other opinions are always welcomed.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

One idea is to use a singleton

Define a model

 public class Container
 {
     public string Message { get; set; }
     public DateTime DateTime { get; set; }
 }

Setup a method in a singleton class

 public sealed class Singleton
 {
     private static readonly Lazy<Singleton> Lazy = 
         new(() => new Singleton());
     public static Singleton Instance => Lazy.Value;
    
     public void ToJsonFormat(Container container)
     {
         Console.WriteLine(JsonConvert.SerializeObject(
             container, Formatting.Indented));
     }
    
 }


Use it

 Singleton.Instance.ToJsonFormat(new Container()
 {
     DateTime = DateTime.Now, 
     Message = "Hello"
 });



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

simplify3000-6685 avatar image
0 Votes"
simplify3000-6685 answered

great, thank you.
The main thing I wanted to see was if anyone had the opinion of "I tried it before and it caused major problems".
Good to see that was not reported, now I can try out the other things mentioned here.

Thanks


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited

Your analysis is wrong, while creating objects costs more than not creating objects, it is pretty cheap (depending on the object). in your code the ToJsonFormat() call is the big expense. For performance, using a JsonWriter would be a better improvement.

creating objects in static methods is no different than in instance methods. the main difference is a static method is called directly rather than thru an instance vtable. static methods are just as thread safe as instance methods.

note: do not confuse static methods (all methods are really static, instance methods just get passed the this pointer) with static variables, which generally are not thread safe.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

simplify3000-6685 avatar image
0 Votes"
simplify3000-6685 answered

ok thank you for the info, appreciate everyone's replies here.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.