Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 367615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:44:43+00:00 2026-05-12T13:44:43+00:00

BACKGROUND: In running my app through a profiler, it looks like the hotspots are

  • 0

BACKGROUND:

In running my app through a profiler, it looks like the hotspots are all involved in allocating a large number of temporary new byte[] arrays.

In one run under CLR Profiler, a few short (3-5 seconds worth of CPU time outside the profiler) produced over a gigabyte of garbage, the majority of it byte[] allocation, and this triggered over 500 collections.

In some cases it appears that the application is spending upwards of 10% of its CPU time performing collections.

Clearly a rewrite is in order.

So, I am thinking of replacing the new byte[] allocations with a pool class that could reuse the buffer at a later time.

Something like this …

{
 byte[] temp = Pool.AllocateBuffer(1024);
 ...
 }

QUESTION:

How can I force the application to call code in the routine Pool.deAllocate(temp) when temp is no longer needed.

In the above code fragment, when temp is a Pool allocated byte[] buffer, but when it goes out of scope it gets deleted. Not a real problem, but doesn’t get reused by the pool.

I know I could replace the “return 0;” with “Pool.deAllocate(temp); return 0”, but I’m trying to force the recovery to occur.

Is this even remotely possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:44:43+00:00Added an answer on May 12, 2026 at 1:44 pm

    You could implement a Buffer class which implements IDisposable and returns the buffer to the pool when it’s disposed. You can then give access to the underlying byte array, and so long as everyone plays nicely you can take advantage of reuse.

    Be warned though:

    • Your buffers will quickly end up in gen 2, which may not be ideal for other reasons
    • If a malicious piece of code keeps a reference to the byte array, they could spy on data used by other code
    • You need to remember to dispose of buffers at the right time.

    I actually have some code in MiscUtil to do this – see CachingBufferManager, CachedBuffer etc. I can’t say I’ve used it much, mind you… and from what I remember, I made it a bit more complicated than I really needed to…

    EDIT: To respond to the comments…

    • You can’t force application code to release buffers, no. There’s no automatic release mechanism in C# – a using statement is the closest we’ve got.
    • You could implement an implicit conversion to byte[] in your buffer class to allow you to call methods which have byte array parameters. Personally I’m not much of a fan of implicit conversions, but it’s certainly available as an option.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app is a background service running in the foreground. In low memory situations
I have a BlackBerry app running in the background that needs to know when
I have a BlackBerry app running in the background that needs to know when
With iOS 5 is it possible to keep the app running in the background
Before going all the hassle coding a generic asynchronous, queued, background running file download
I have a TableModel that is populated from a background running thread. I am
I have a background thread running that fires events, but how can I ensure
I am running into OutOfMemoryError on a long running background process on Glassfish. Memory
I want to trigger a long running background process (coded as a service) from
Background: I'm running multiple simulations on a set of data. For each session, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.