473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Objects from another class to decrease Mem Usage

I have been researching the correct way to organize my solution so that it
makes best use of VB.NET inherent ability to manage resources such as
objects. My solution contains 2 projects and the main problem is that the
mem usage continues to grow until the Service stops responding. I have
received advice to:

"create those objects at a class level; instantiate them when the service
starts, and dispose of them when the service ends. Then include only the
[object handling] code in the timer's elapsed event handler"

This seems simple enough, although I need further clarification. Would it
be best to put the Objects in the Objcls.vb NEW event and FINALIZE event, or
should they be created and finalized in the code class from where they are
being used?

Thanks for your help, a brief example would be appreciated.

Tom

Nov 22 '05 #1
2 1608
Fish wrote:
I have been researching the correct way to organize my solution so
that it makes best use of VB.NET inherent ability to manage resources
such as objects. My solution contains 2 projects and the main
problem is that the mem usage continues to grow until the Service
stops responding. I have received advice to:
The point is, you should use resources only when you need them and
release those resources as soon as possible. The recommendation to use
Dispose is only useful for objects that implement IDisposable, which
means objects that have access to unmanaged resources.

So if you access files, for example, create the FileStream object *just*
before you write the data, and then call Dispose *immediately* after you
have finished writing the data. Don't hold the FileStream object for
longer than you need to.

IDisposable has no effect on the memory usage if your resources are
entirely managed:

// IDisposable is pointless on this class
class Test : IDisposable
{
int[] data = new int [1000000];
void Dispose()
{
// cannot call 'dispose' on data
}
// other stuff
}

When a Test object is no longer used (not 'reachable') then it is a
candidate for the GC to release its memory, but you have no control when
this will happen.
"create those objects at a class level; instantiate them when the
service starts, and dispose of them when the service ends. Then
include only the [object handling] code in the timer's elapsed event
handler"
As you have stated it, this is very bad advice. You say your problem is
memory, and so creating the objects when the service starts and
disposing them when it ends means that the objects live all the time the
service lives - they are long lived and take up memory all of that time.
Plus the 'advice' of 'dispose when the service ends' is completely
wrong - if they have a Dispose method this means they should be disposed
as soon as possible, so you should not wait until the service ends.

Instead, the objects should only be created just before they are needed
and then disposed just after they have been used. In some cases it could
be argued that such an object could be created, *one* method called,
then disposed (this is how COM+ objects work and .NET single call client
activated remote objects work).

The only time when you want to have a long lived object is when the
initialization of the object takes a long time. But this is not the
problem you are reporting.
This seems simple enough, although I need further clarification. Would
it be best to put the Objects in the Objcls.vb NEW event and
FINALIZE event, or should they be created and finalized in the code
class from where they are being used?


This is bad advice in general. Ignore it.

You should profile your code. For example, use perf mon to look at
memory usage. Identify the code that allocates memory, and reduce those
memory allocations. The Test class above uses a lot of memory, the first
thing to do is to see if it really is necessary to use 1000000 items. If
it is, then adjust your usage of the object:

class App
{
Test test = new Test();// this now has a lifetime of the entire
process
static void Main()
{
App a = new App();
a.Run();
}
void Run()
{
DoSomething(test);
DoSomethingWithoutTest();
}
}

Here, if the GC runs after DoSomething it will not touch the test
object. An improvement would be:

void Run()
{
test = new Test(); // this now has a lifetime of the entire process
DoSomething(test);
test = null; // this allows the test object to be GC'd
DoSomethingWithoutTest();
}

Here, you are making the test object 'unreachable' so if the GC decides
to work after DoSomething it can free up the memory used by the test
object.

A much better solution in this case is not to use an instance field at
all, but create the object just for the specific use:

void DoSomething()
{
Test test = new Test(); //allocate memory
// use test here
test = 0; // just in case GC runs after this point, make test
'unreachable'
// other work that does not use test
} // test is automatically 'unreachable' here

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 23 '05 #2
Thanks for the reply, very good stuff. I got my component disposing
quite nicely now as a result.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
8
by: GeekBoy | last post by:
I understand the benefit of pushing the StateServer process onto another computer to "balance" the load and take some cpu and memory usage off the web server, but how much could it possibly help?...
12
by: baibaichen | last post by:
i know that pImpl idiom can decrease compile time, but Is there any good practice/idiom to decrease link time? any reference or any idea is appreciated thanks
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
6
by: =?Utf-8?B?bGlnaHRkb2xs?= | last post by:
Hello everyone. i want to know how to decrease the cpu usage in my program. i have to update quickly many data on gui, so i tried to decrease the cpu usage on gui but i couldn't solve the...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.