garbage-collection

How do i explicitly clear the byte[]

耗尽温柔 提交于 2019-12-23 08:54:30
问题 I am creating new byte arrays which are not being collected by GC and are living in memory and increasing the private bytes. The code below gets executed every 10 seconds. How do I explicitly clear the variable after I am done with it? byte[] outputMessage = new byte[10000]; //Do some work here 回答1: How do you know they are not being collected? The code you provide is fine, and it should become eligible for collection if you don't have any dangling references to it. Explicitly, you can clear

Pass zero-sized array, save allocation?

孤人 提交于 2019-12-23 08:47:04
问题 In this code sample from page 114 of The Well-Grounded Java Developer, the last line: Update[] updates = lu.toArray(new Update[0]); contains the note: Pass zero-sized array, save allocation List<Update> lu = new ArrayList<Update>(); String text = ""; final Update.Builder ub = new Update.Builder(); final Author a = new Author("Tallulah"); for (int i=0; i<256; i++) { text = text + "X"; long now = System.currentTimeMillis(); lu.add(ub.author(a).updateText(text).createTime(now).build()); try {

Use of Garbage Collection?

假如想象 提交于 2019-12-23 07:51:14
问题 I want to know what action is performed when we call Dispose() method. Is Object frees all resources quickly on Dispose() call or Dispose() marks the Object is ready for garbage collection. And What happened when we set Object reference to NULL. Actually I have Windows form application in .NET 2.0. And I want to call garbage collector after certain time is passed(For Example after 5 mins) to collect all unreferenced Object. 回答1: There is nothing magic about the Dispose method, it's just like

Why does the c# garbage collector not keep trying to free memory until a request can be satisfied?

六月ゝ 毕业季﹏ 提交于 2019-12-23 07:38:40
问题 Consider the code below: using System; namespace memoryEater { internal class Program { private static void Main(string[] args) { Console.WriteLine("alloc 1"); var big1 = new BigObject(); Console.WriteLine("alloc 2"); var big2 = new BigObject(); Console.WriteLine("null 1"); big1 = null; //GC.Collect(); Console.WriteLine("alloc3"); big1 = new BigObject(); Console.WriteLine("done"); Console.Read(); } } public class BigObject { private const uint OneMeg = 1024 * 1024; private static int _idCnt;

Lifetime of JavaScript variables

别来无恙 提交于 2019-12-23 07:38:08
问题 What is the lifetime of a variable in JavaScript, declared with "var". I am sure, it is definitely not according to expectation. <script> function(){ var a; var fun=function(){ // a is accessed and modified } }(); </script> Here how and when does JavaScript garbage collect the variable a ? Since a is a part of the closure of the inner function, it ideally should never get garbage collected, since the inner function fun , may be passed as a reference to an external context. So fun should still

Is GC.KeepAlive required here, or can I rely on locals and arguments keeping an object alive?

人走茶凉 提交于 2019-12-23 07:12:03
问题 I have a bunch of methods that take the WPF's WriteableBitmap and read from its BackBuffer directly, using unsafe code. It's not entirely clear whether I should use GC.KeepAlive whenever I do something like this: int MyMethod(WriteableBitmap bmp) { return DoUnsafeWork(bmp.BackBuffer); } On the one hand, there remains a reference to bmp on MyMethod 's stack. On the other, it seems like relying on implementation detail - this could compile to a tail call, for example, keeping no reference to

In WPF: Children.Remove or Children.Clear doesn't free objects

旧巷老猫 提交于 2019-12-23 07:05:57
问题 Update : I tried this on another, more cleanly installed, machine. I could not reproduce this on that machine. If I find out what offending (VSStudio) component causes this, I will let you know. I create some UIElements from code behind and was anticipating the garbage collection to clear up stuff. However, the objects are not free-ed at the time I expected it. I was expecting them to be freeed at RemoveAt(0), but they are only freed at the end of the program. How can I make the objects be

Determining where object allocations for objects on the heap occurred

一曲冷凌霜 提交于 2019-12-23 07:05:36
问题 Is there any tool such that it can get a heap dump from a running application and determine/group objects by where in source code they were created? With no changes to source code and ideally something free. 回答1: As others suggested memory profilers, Memprofiler is definitely the most advanced one (I've tried all existing .NET profilers). It has a 14 day trial. 回答2: What about .NET Memory Profiler from ANTS for example. Maybe CLR Profiler. 回答3: The information is not available if you create a

Why will a full gc on small heap takes 5 seconds?

牧云@^-^@ 提交于 2019-12-23 05:33:16
问题 I am running an J2EE application on 3 year old Solaris system with a used heap that is about 300 MB. From the gc logs I have seen that the full gc that is triggered a few times a day takes about 5 seconds and recovers about 200 MB every time. What could be the reason for a full gc to take such a long time on such a small heap? I run java 1.6.0_37. 回答1: A slow full GC (and minor GC for that matter) is primary a result of a poor hardware setup and secondly software configuration (i.e. GC

Understanding how to memcpy with TheUnsafe

孤者浪人 提交于 2019-12-23 04:44:32
问题 I read stuff about TheUnsafe, but I get confused by the fact that, unlike C/C++ we have to work out the offset of stuff, and there's also the 32bits VM vs the 64bits VM, which may or may not have different pointers sizes depending on a particular VM setting being turned on or off (also, I'm assuming all offsets to data are actually based on pointer arithmetic this would influence them to). Unfortunately, it seems all the stuff ever written about how to use TheUnsafe stems from one article