473,394 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Memory problem with my VB code.

Here is my dilemma. I have a subroutine runs every 100mS. At the first, I
found that the system hangs after a few hours when I run this program. I
checked the memory usage under XP Task Manager. I found that memory used by
my program keep growing in a rate of 12KB per second.

Here is what I think the program is doing. The 100mS subroutine could not
complete in 100mS. Then the second comes, then third..... This makes the
stack used by the code keep growing until crashes the system.

I am thinking this is because the code did not get enough CPU time. Is a way
I could increase the priority level of the code within VB so that the system
will assign more CPU time to the process?

BTW, I use VB .NET 2003.

Thanks,

Dave
Nov 21 '05 #1
7 1379
You don't say so, so I will assume, for the purposes of the argument that
you are using a Timer control with the Interval property set to 100.

The first question you need to ask is:

Is it important that the subroutine runs EVERY 100ms?

If the answer is no, then I would be inclined to stop the Timer control
immediately the subroutine starts and start the Timer immediately befor the
subroutine ends. This will ensure that the problem you describe will not
occur, BUT, it will also mean that the subroutine will run 100ms after it
finishes.

If the answer is yes, then the next question is:

If the subroutine is executing when the Timer 'fires', can it be
'skipped'?

If the answer to this is yes, then setting a flag when the subroutine starts
and setting it when it ends gives you the ability to check to see if the
subroutine should be run. If not, then ignore it this time around.

I hope that gives you some food for thought.
"Deng Zhang" <de********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Here is my dilemma. I have a subroutine runs every 100mS. At the first, I
found that the system hangs after a few hours when I run this program. I
checked the memory usage under XP Task Manager. I found that memory used
by my program keep growing in a rate of 12KB per second.

Here is what I think the program is doing. The 100mS subroutine could not
complete in 100mS. Then the second comes, then third..... This makes the
stack used by the code keep growing until crashes the system.

I am thinking this is because the code did not get enough CPU time. Is a
way I could increase the priority level of the code within VB so that the
system will assign more CPU time to the process?

BTW, I use VB .NET 2003.

Thanks,

Dave

Nov 21 '05 #2
"Deng Zhang" <de********@yahoo.com> schrieb:
Here is my dilemma. I have a subroutine runs every 100mS. At the first, I
found that the system hangs after a few hours when I run this program. I
checked the memory usage under XP Task Manager. I found that memory used
by my program keep growing in a rate of 12KB per second.

Here is what I think the program is doing. The 100mS subroutine could not
complete in 100mS. Then the second comes, then third..... This makes the
stack used by the code keep growing until crashes the system.
How do you start the routine every 100 ms? Are you using a timer? Do you
start the instances of the method in separate threads? Are you sure that
all resources are disposed when they are not needed any more?
I am thinking this is because the code did not get enough CPU time. Is a
way I could increase the priority level of the code within VB so that the
system will assign more CPU time to the process?


This may solve the problem for one machine, but there is still no guarantee
that your application will run as expected all the time.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
Here is what I think the program is doing. The 100mS subroutine could not
complete in 100mS. Then the second comes, then third..... This makes the
stack used by the code keep growing until crashes the system.


How do you start the routine every 100 ms? Are you using a timer? Do you
start the instances of the method in separate threads? Are you sure that
all resources are disposed when they are not needed any more?


I would suggest that any process that expects to continiously repeat for
the life of the application should have all of its needed objects created prior
to starting the timer. While the GC may be good at disposing short lived
objects, (such as those created and destroyed inside a Tick event) there is
no advantage in requiring it to do so, if it can be avoided.

As you stated, a priority fix might work on one system, but that is not the
going to be the best route to take. Applications are supposed to 'play well'
with other applications so stealing CPU cycles is a bit too presumptious
about what the user really wants to do.

It would be better to clean up the design, rather than grab more resources.
A peek at the actual timer code would help to find other possible suggestions....

LFS
LFS
Nov 21 '05 #4
All ,

Well in the MS cirucular reference example there is still a object=Nothing
line in it and with hanging resource claiming objects ( like database
connections ) it is recomended to implement your own dispose method to
"help" the GC

As nowaday`s a lot of programmers see the GC as a free ticket to write
garbage code that they do not clean up , however the garabage collector is
not so very stipt as one might think before it detects circular references.

So it is always good coding practice to clean up your own mess ( put it in
bags and set it ready to take away with the garbage truck ) by disposing
and setting objects to Nothing so they are marked to take away from memory .

I think that this was more what Herfried was mentioning
If this really was thread related i would say implement a thread lock and
clean up your mess in the lock correct me if i am wrong but i think that
this would also solve the reentry problem

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
Here is what I think the program is doing. The 100mS subroutine could not complete in 100mS. Then the second comes, then third..... This makes the stack used by the code keep growing until crashes the system.
How do you start the routine every 100 ms? Are you using a timer? Do you start the instances of the method in separate threads? Are you sure that all resources are disposed when they are not needed any more?


I would suggest that any process that expects to continiously repeat for
the life of the application should have all of its needed objects created

prior to starting the timer. While the GC may be good at disposing short lived
objects, (such as those created and destroyed inside a Tick event) there is no advantage in requiring it to do so, if it can be avoided.

As you stated, a priority fix might work on one system, but that is not the going to be the best route to take. Applications are supposed to 'play well' with other applications so stealing CPU cycles is a bit too presumptious
about what the user really wants to do.

It would be better to clean up the design, rather than grab more resources. A peek at the actual timer code would help to find other possible suggestions....
LFS
LFS

Nov 21 '05 #5
Stephany

I have exactly the same idea

:-)

Cor
Nov 21 '05 #6

"M. Posseth" <mi*****@nohausystems.nl> wrote
So it is always good coding practice to clean up your own mess (...)

I think that this was more what Herfried was mentioning

I think so too, but I was just attemtping to say they shouldn't be
making a mess, in the first place! <g>

If they don't create any objects (in the Tick event) then there won't
be any that need disposing.

LFS
Nov 21 '05 #7
Thanks for everybody's help.

Yes, I was using a control with Interval value set to 100mS. The subroutine
I mentioned is the timer handler. Maybe I should give a brief description
about what the program doing. Program continuously read data from a COM port
(9600-baud rate) and parses the data with the protocol. The data packages
come from a microprocessor and could be any of following: four channels of
A/D, front panel key strokes, rotary encoder actions and life ticks. All
these are graphically displayed on the screen and refresh with the incoming
data. Every thing worked great except that memory problem. I thought that
was a priority issue I tried to change the process priority to High or even
Real Time. There was no any difference.

Today I tried to change the interval value to 250mS and 500mS. I also
changed the Comm port maximum read count from 20 to 200. That seemed fix the
problem. I will try to use the flag on Tuesday. It is for sure that error
will not appear if I set interval greater than 500mS. But the screen
response would be too slow. I am trying to figure out the minimum Interval
value for the program.

Btw, the program is for hardware testing. It will only run on the platform
we designed. It is a 1GHz Intel compatible VIA processor. I did not have any
threading in my program although I can see process has 11 threads under the
task manager.

I did not create any objects. The program is in old C style because I
thought that is the most efficient way for this type of real time display. I
will keep learning from your suggestions.

I do appreciate your response.

Dave

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"M. Posseth" <mi*****@nohausystems.nl> wrote
So it is always good coding practice to clean up your own mess (...)

I think that this was more what Herfried was mentioning

I think so too, but I was just attemtping to say they shouldn't be
making a mess, in the first place! <g>

If they don't create any objects (in the Tick event) then there won't
be any that need disposing.

LFS

Nov 21 '05 #8

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

Similar topics

5
by: Jason Callas | last post by:
I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
12
by: ira2402 | last post by:
Hi All, We are developing sw for a small embedded OS and we have limited memory. We are looking for algorithms, links, and articles about this. The goal is efficient utilization of small amount...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
81
by: Peter Olcott | last post by:
It looks like System::Collections::Generic.List throws and OUT_OF_MEMORY exception whenever memory allocated exceeds 256 MB. I have 1024 MB on my system so I am not even out of physical RAM, much...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.