none
Strings and their memory utilization RRS feed

  • Question

  • I have two questions about strings

    1> When we pass string as a parameter, and if the same string value is getting passed to different methods, and since strings are immutable, will .Net run time creates new instance of string every time it passes through those methods.
    For example below

    public class Service
    {
        public void Run(string largeString)
    	{
    			logger.LogLargeStringToFileSystem(largeString);
    			
    			// call private method
    			DoPrepWork(largeString);
    			
    			validator.Validate(largeString);
    			
    			repository.Save(largeString);
    	}
    	
    	private void DoPrepWork(string largeString)
    	{
    	}
    }
    
    
    public class Validator
    {
       public void Validate(string largeString)
       {
       }
    }
    
    public class Repository
    {
       public void Save(string largeString)
       {
       }
    }
    
    public class Logger
    {
       public void LogLargeStringToFileSystem(string largeString)
       {
          // log string to file system
       }
    }

    2>Can we set string to null to free up the memory? I know garbage collector eventually collects the string but I think it happens when the control goes out of the scope.So in the code below, I don’t need string variable as soon it gets converted to object. How do I immediately free up the memory occupied by the string variable. I dont want to use GC.Collect() everywhere in the code

    public class Service
    {
        public void Run(string largeString)
    	{
    			
    			MyObject obj = ConvertToObject(largeString);
    			
    			//i dont need largeString anymore
    			// the long running task may take time, so i would like to free up memory as soon as possible
    
    			worker.DoLogRunningTask(obj);			
    	}
    }
    
    
    public class Worker
    {
       public void DoLongRunningTask(MyObject obj)
       {
       }
    }
    


    Tuesday, November 11, 2014 9:14 PM

Answers

  • >>1.
    No, since string is a reference type a new instance of the string will not be created when you pass it as an argument to a method. Only the reference is copied.

    >>2.Can we set string to null to free up the memory?

    No. There is no need to explicitly dispose any managed object that doesn't implement the IDisposable interface. Setting the string to NULL will have no effect at all.

    >>How do I immediately free up the memory occupied by the string variable.

    You can't. And you shouldn't. .NET applications are executed in a managed environment and the CLR (through the garbage collector) takes care of releasing memory back to the OS. Even when objects are collected by the GC, the associated memory segment may not be immediately released back to the OS.

    Please remember to mark helpful posts as answer/helpful and please only ask one question per thread. If you have a new question, you should start a new thread.

    Tuesday, November 11, 2014 9:30 PM