Question: I have following Java method and it returns String depending on time interval. It works fine when the intervals are like 6-14h. 

```
private String shiftCount()
{
  Calendar time = Calendar.getInstance();
  if ((time.get(Calendar.HOUR_OF_DAY) >=  0) && (time.get(Calendar.HOUR_OF_DAY) <  6)) return "-S1";
  if ((time.get(Calendar.HOUR_OF_DAY) >=  6) && (time.get(Calendar.HOUR_OF_DAY) < 14)) return "-S2";
  if ((time.get(Calendar.HOUR_OF_DAY) >= 14) && (time.get(Calendar.HOUR_OF_DAY) < 22)) return "-S3";
  if ((time.get(Calendar.HOUR_OF_DAY) >= 22) && (time.get(Calendar.HOUR_OF_DAY) < 24)) return "-S1";   
  return null;
}

```

But what if i need e.g. 6:10-14:10 interval?

Answer: 
Memory will probably stay increased until the GC runs, which won't correspond to when data is returned to the client.

Have you tried adding a breakpoint or logging of some sort to your service method to make sure that the method is being called on each request? I don't think WCF does any caching on its own; at least, I've never had it do any in my applications that use it.

---

Edit:

Memory will remain in use until the garbage collector runs. If your process still has plenty of free space in its heap, then there really isn't a reason for the GC to run.

According to MSDN: <http://msdn.microsoft.com/en-us/library/ee787088.aspx#conditions_for_a_garbage_collection>

> 
> Garbage collection occurs when one of
>  the following conditions is true:
> 
> 
> * The system has low physical memory.
> * The memory that is used by allocated objects on the managed heap
>  surpasses an acceptable threshold.
>  This means that a threshold of
>  acceptable memory usage has been
>  exceeded on the managed heap. This
>  threshold is continuously adjusted as
>  the process runs.
> * The GC.Collect method is called. In almost all cases, you do not have
>  to call this method, because the
>  garbage collector runs continuously.
>  This method is primarily used for
>  unique situations and testing.
> 
> 
> 

It is likely that you are allocating objects in the heap, but they are not being GC'd because the GC sees no reason to run (there is still open space in the heap generation, and no reason to spend time clearing it out).

However, if you can repeat your WCF call over and over and eventually get an Out of Memory exception, then that would indicate that you do indeed have a problem with references being held somewhere. In that case, I would use a memory profiler to determine what is being held onto, and by what.

---

Edit #2:

See also this thread: [C# Thread not releasing memory](https://stackoverflow.com/questions/6331389/c-thread-not-releasing-memory)
It sounds more like garbage collector hasn't run yet and because of that the memory wasn't released. Moreover when hosting WCF service in console application the `GC` runs in Workstation mode which can be less effective in such case.