garbage-collection

Why call GC.KeepAlive in the end, and not in the beginning? [duplicate]

为君一笑 提交于 2021-02-04 15:22:05
问题 This question already has answers here : Understanding garbage collection in .NET (2 answers) Closed 7 years ago . From GC.KeepAlive() on MSDN: Code this method at the end, not the beginning, of the range of instructions where obj must be available. Why does it have such non-intuitive behavior? 回答1: Because otherwise technically the JIT and CLI could determine that the value isn't used after that point, and consider the object viable for collection. Heck, the compiler could decide to remove

Why does the JVM consume less memory than -Xms specified?

坚强是说给别人听的谎言 提交于 2021-02-04 14:43:35
问题 My question is as the title, and I got some knowledge by searching: linux has shared memory How to measure actual memory usage of an application or process? JVM will reserve the amount of memory setted in Xms What does the -Xms JVM mean in reference to heap memory? But still don't know why, some on can offer some help? Here is my test run on Ubuntu12.04(64bit) | JDK 1.7.0_04. and top shows below: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4067 brian 20 0 5316m **262m** 7496 S 0 3.3

Gen2 collection not always collecting dead objects?

被刻印的时光 ゝ 提交于 2021-02-04 10:39:50
问题 By monitoring the CLR #Bytes in all Heaps performance counter of a brand new .NET 4.5 server application over the last few days, I can notice a pattern that makes me think that Gen2 collection is not always collecting dead objects, but I am having trouble understanding what exactly is going on. Server application is running in .NET Framework 4.5.1 using Server GC / Background. This is a console application hosted as a Windows Service (with the help of Topshelf framework) The server

Set null to Thread to prevent memory leak in onDestroy Android

僤鯓⒐⒋嵵緔 提交于 2021-01-29 10:50:20
问题 I am using Thread for some Async operations. Now primarily I have 3 Threads like : private lateinit var horse1Thread: Thread private lateinit var horse2Thread: Thread private lateinit var timerThread: Thread Calling stop() in onDestroy() of activity causes a UnsupportedOperationException and I thought of setting these values to null to allow GC collection and prevent memory leak. Since my fields are non-null types I cant set them to null . So is this my only option? Or does kotlin provide a

how to force garbage collector to move an object in memory

烈酒焚心 提交于 2021-01-28 17:43:52
问题 In order to test some scenarios that interact with unmanaged code, I need to force the GC to move an object in memory. I have the following code to test object movement. What code should be written in the section somehow force gc to move mc so that two different addresses are printed to the console? [StructLayout(LayoutKind.Sequential)] class MyClass { public int i; } class Program { static void Main(string[] args) { MyClass mc = new MyClass(); // print address of mc var handle = GCHandle

C# out of scope objects not getting collected

余生颓废 提交于 2021-01-28 11:45:47
问题 I'm trying to build my first unit tests and have a class that increments and decrements an instance counter in it's constructor and destructor respectively. I have a test to make sure this works but it fails, it seems that other instances of the class from my other tests aren't having their destructor called when they go out of scope. public class Customer { public Customer() { ++InstanceCount; } public Customer(int customerId) { CustomerId = customerId; ++InstanceCount; } ~Customer() { -

Why does the weak reference not get collected in this simple F# example?

落花浮王杯 提交于 2021-01-28 08:12:04
问题 open System let WeakReferenceExample() = let mutable obj = new Object(); let weak = new WeakReference(obj); GC.Collect(); Console.WriteLine("IsAlive: {0}\nobj <> null is {1}\n---", weak.IsAlive, obj <> null); obj <- null; GC.Collect(); Console.WriteLine("IsAlive: {0}", weak.IsAlive); WeakReferenceExample() Console.ReadKey() Translated from the Rx In Action book sample. The above when run gives the following output which is different than what I get when I compile it in C# and run it. IsAlive:

Garbage Collection of Spring Beans

为君一笑 提交于 2021-01-28 07:50:18
问题 How the the objects managed by spring are garbage collected? Java garbage collector can find the normal objects (I mean I have not injected them via dependency injection). How does the garbage collector collect spring beans? Since it cannot track them via stack. I would like the gc to garbage collect the spring beans that not being used. 来源: https://stackoverflow.com/questions/45757225/garbage-collection-of-spring-beans

Is inline created InputStream closed automatically by GC?

ぃ、小莉子 提交于 2021-01-28 07:35:31
问题 I've found several similar questions, but I still can't find the answer to my question. I know that it is enough to close the outer stream and it will close inner stream which is created in line. BufferedInputStream br = new BufferedInputStream(new FileInputStream(file)); br.close(); Consider next example Properties props = new Properties(); props.load(new FileInputStream(configPath)); Should the FileInputStream be assigned to a variable and then closed explicitly (or with try-with-resource

Using Chronicle Map producing garbage while using Streams API

杀马特。学长 韩版系。学妹 提交于 2021-01-28 03:36:02
问题 Today I was experimenting with Chronicle Map. Here is a code sample: package experimental; import net.openhft.chronicle.core.values.IntValue; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.values.Values; public class Tmp { public static void main(String[] args) { try (ChronicleMap<IntValue, User> users = ChronicleMap .of(IntValue.class, User.class) .name("users") .entries(100_000_000) .create();) { User user = Values.newHeapInstance(User.class); IntValue id =