在线程之间共享DLL [英] Sharing DLL between threads

查看:76
本文介绍了在线程之间共享DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


我有一个第三方DLL(不是.NET类库PE)我使用

我的DllImport属性在多个线程中运行的应用程序调用

同一DLL的不同方法。


应用程序的内存使用量在30分钟内就会降至1GB。 />
直到它达到1.4GB之后才得到OutOfMEmory异常。如果我不用
使用DLL,我的内存保持在100MB。


我尝试使用FreeLibrary释放DLL但几秒后我就是获取

对象引用未设置为对象的实例访问DLL的

方法时。


如何确保每个线程只有一个DLL副本在我的
$ b中$ b应用程序内存,当任何线程不再使用它时,它会被卸载吗?


BTW,我的应用程序是一个ASP.NET Web服务,但由于我正在应用程序级别在后台生成并运行

个线程,因此无法使用webmethod进行无状态处理。

。 />

谢谢,

Ram

解决方案

如果你使用标准的PInvokde机制:DON ''永远免费你的图书馆

amd它无法卸载。

但是不知道,已经有一个单一的DLL实例了在内存中

适用于所有线程(所有进程!!!)


如果内存增加,它很可能是一个bug在你的应用程序中


与标准机制w一起使用的DLL不要上传,不要在FREED图书馆中调用函数



这会立即导致你的程序崩溃,或者最糟糕的是,导致奇怪,神秘,

外来bug ...

另一方面,使用.NET 2.0,你可以将IntPtr(一个函数

指针)转换为代表。

所以你可以使用LoadLibrary / FreeLibrary& GetProcAddress。

但即使这样,也不要打电话到FREED库。

Ram P. Dash < RA **** @ hotmail.com>在消息中写道

新闻:oC **************** @ newsread2.news.pas.earthli nk.net ...

嗨:

我有一个第三方DLL(不是.NET类库PE),我在我的应用程序中运行DllImport属性,在多个线程中运行,调用
相同DLL的不同方法。

应用程序的内存使用量在短短30分钟内就会达到1GB。
直到它达到1.4GB之后才会出现OutOfMEmory异常。如果我不使用DLL,我的内存保持在100MB。

我尝试使用FreeLibrary释放DLL,但几秒钟之后我就得到了对象引用未设置为对象的实例同时
访问DLL的方法。

如何确保每个线程只有一个DLL副本在我的应用程序内存中,并且当它被卸载时,DLL任何线程都不再使用

顺便说一句,我的应用程序是一个ASP.NET Web服务,但这与无状态无关webmethod因为我正在应用程序级别生成并运行后台的线程。

谢谢,
Ram


谢谢,


但是当我在代码中的某处查看ProcessModuleCollection时,我看到同样的DLL在集合中出现了三次测试环境

(一次运行接近5个线程)。难道不是说我的应用程序内存中有三个相同DLL的
副本吗?


谢谢,

Ram


" Lloyd Dupont" < net.galador@ld>写在消息

新闻:呃************** @ tk2msftngp13.phx.gbl ...

如果你使用标准PInvokde机制:不要忘记你的图书馆
并且它不能被卸载。
但是不知道,已经有一个单独的DLL实例在
所有线程的内存(所有进程!!!)

如果你的内存那么高,它很可能是你的
应用程序中的一个错误
使用的DLL使用标准机制不会上传,也不要在FREED库中调用
函数。
这会立即导致程序崩溃,或者最糟糕的是导致奇怪,神秘,外星人错误...

另一方面,使用.NET 2.0,你可以将IntPtr(函数指针)转换为委托。
所以你可以使用LoadLibrary / FreeLibrary &安培; GetProcAddress。
但即便如此,也不要打电话到FREED图书馆。

Ram P. Dash < RA **** @ hotmail.com>在消息中写道
新闻:oC **************** @ newsread2.news.pas.earthli nk.net ...

嗨:

我有一个第三方DLL(不是.NET类库PE),我在我的应用程序中使用了多个线程运行的DllImport属性
调用不同的方法相同的DLL。

该应用程序的内存使用量仅需30美元b b分钟就可以达到1GB。直到它达到1.4GB之后才得到OutOfMEmory异常。如果我不使用DLL,我的内存保持在100MB。

我尝试使用FreeLibrary释放DLL,但几秒钟之后我就得到了对象引用未设置为对象的实例同时
访问DLL的方法。

如何确保每个线程只有一个DLL副本在我的应用程序内存中,并且当它被卸载时,DLL任何线程都不再使用

顺便说一句,我的应用程序是一个ASP.NET Web服务,但这与无状态无关webmethod因为我正在应用程序级别生成并运行后台的线程。

谢谢,
Ram



谢谢,


但是当我在代码中的某个地方查看ProcessModuleCollection时,我看到了
看到了同样的情况DLL在我的测试环境中出现三次

(一次运行接近5个线程)。难道不是说我的应用程序内存中有三个相同DLL的
副本吗?


谢谢,

Ram


" Lloyd Dupont" < net.galador@ld>写在消息

新闻:呃************** @ tk2msftngp13.phx.gbl ...

如果你使用标准PInvokde机制:不要忘记你的图书馆
并且它不能被卸载。
但是不知道,已经有一个单独的DLL实例在
所有线程的内存(所有进程!!!)

如果你的内存那么高,它很可能是你的
应用程序中的一个错误
使用的DLL使用标准机制不会上传,也不要在FREED库中调用
函数。
这会立即导致程序崩溃,或者最糟糕的是导致奇怪,神秘,外星人错误...

另一方面,使用.NET 2.0,你可以将IntPtr(函数指针)转换为委托。
所以你可以使用LoadLibrary / FreeLibrary &安培; GetProcAddress。
但即便如此,也不要打电话到FREED图书馆。

Ram P. Dash < RA **** @ hotmail.com>在消息中写道
新闻:oC **************** @ newsread2.news.pas.earthli nk.net ...

嗨:

我有一个第三方DLL(不是.NET类库PE),我在我的应用程序中使用了多个线程运行的DllImport属性
调用不同的方法相同的DLL。

该应用程序的内存使用量仅需30美元b b分钟就可以达到1GB。直到它达到1.4GB之后才得到OutOfMEmory异常。如果我不使用DLL,我的内存保持在100MB。

我尝试使用FreeLibrary释放DLL,但几秒钟之后我就得到了对象引用未设置为对象的实例同时
访问DLL的方法。

如何确保每个线程只有一个DLL副本在我的应用程序内存中,并且当它被卸载时,DLL任何线程都不再使用

顺便说一句,我的应用程序是一个ASP.NET Web服务,但这与无状态无关webmethod因为我正在应用程序级别生成并运行后台的线程。

谢谢,
Ram



Hi:

I''ve a third party DLL (not a .NET class library PE) which I use using
DllImport attribute in my application running in multiple threads invoking
different methods of the same DLL.

The memory usage of the application is shooting to 1GB in just 30 minutes.
It goes till it''s 1.4GB after which I get OutOfMEmory exceptions. If I don''t
use the DLL, my memory stays put at 100MB.

I tried freeing the DLL using FreeLibrary but after few seconds I am getting
"Object reference not set to an instance of an object" while accessing the
methods of the DLL.

How can I ensure that only one copy of the DLL per thread is in my
application memory and that the DLL is unloaded when it''s no longer used by
any of the threads?

BTW, my application is an ASP.NET web service but this has nothing to do
with the statelessness of a webmethod because I am spawning and running the
threads in the background at application level.

Thanks,
Ram

解决方案

if you use standart PInvokde mechanism: DON''T EVER FREE your LIBRARY
amd it can''t be unloaded.
but don''t wory, there is, already, one single instance of the DLL in memory
for all thread (of all process!!!)

if you memory goes that up, it''s very likely to be a bug in your application

DLL used with standart mechanism won''t upload and DON''T EVER call a function
in a FREED Library.
That will crash your program instantly or, worst, cause weird, mysterious,
alien bugs...
On the other hand, with .NET 2.0 you could transform IntPtr (of a function
pointer) to a delegate.
So you could used LoadLibrary/FreeLibrary & GetProcAddress.
But even then, NEVER call into a FREED library.
"Ram P. Dash" <ra****@hotmail.com> wrote in message
news:oC****************@newsread2.news.pas.earthli nk.net...

Hi:

I''ve a third party DLL (not a .NET class library PE) which I use using
DllImport attribute in my application running in multiple threads invoking
different methods of the same DLL.

The memory usage of the application is shooting to 1GB in just 30 minutes.
It goes till it''s 1.4GB after which I get OutOfMEmory exceptions. If I
don''t use the DLL, my memory stays put at 100MB.

I tried freeing the DLL using FreeLibrary but after few seconds I am
getting "Object reference not set to an instance of an object" while
accessing the methods of the DLL.

How can I ensure that only one copy of the DLL per thread is in my
application memory and that the DLL is unloaded when it''s no longer used
by any of the threads?

BTW, my application is an ASP.NET web service but this has nothing to do
with the statelessness of a webmethod because I am spawning and running
the threads in the background at application level.

Thanks,
Ram



Thanks,

However when I looked at the ProcessModuleCollection somewhere in my code, I
saw the same DLL appear three times in the collection my test environment
(where close to 5 threads run at a time). Doesn''t that tell that three
copies of the same DLL is in my application memory?

Thanks,
Ram

"Lloyd Dupont" <net.galador@ld> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...

if you use standart PInvokde mechanism: DON''T EVER FREE your LIBRARY
amd it can''t be unloaded.
but don''t wory, there is, already, one single instance of the DLL in memory for all thread (of all process!!!)

if you memory goes that up, it''s very likely to be a bug in your application
DLL used with standart mechanism won''t upload and DON''T EVER call a function in a FREED Library.
That will crash your program instantly or, worst, cause weird, mysterious,
alien bugs...
On the other hand, with .NET 2.0 you could transform IntPtr (of a function
pointer) to a delegate.
So you could used LoadLibrary/FreeLibrary & GetProcAddress.
But even then, NEVER call into a FREED library.
"Ram P. Dash" <ra****@hotmail.com> wrote in message
news:oC****************@newsread2.news.pas.earthli nk.net...

Hi:

I''ve a third party DLL (not a .NET class library PE) which I use using
DllImport attribute in my application running in multiple threads invoking different methods of the same DLL.

The memory usage of the application is shooting to 1GB in just 30 minutes. It goes till it''s 1.4GB after which I get OutOfMEmory exceptions. If I
don''t use the DLL, my memory stays put at 100MB.

I tried freeing the DLL using FreeLibrary but after few seconds I am
getting "Object reference not set to an instance of an object" while
accessing the methods of the DLL.

How can I ensure that only one copy of the DLL per thread is in my
application memory and that the DLL is unloaded when it''s no longer used
by any of the threads?

BTW, my application is an ASP.NET web service but this has nothing to do
with the statelessness of a webmethod because I am spawning and running
the threads in the background at application level.

Thanks,
Ram




Thanks,

However when I looked at the ProcessModuleCollection somewhere in my code, I
saw the same DLL appear three times in the collection my test environment
(where close to 5 threads run at a time). Doesn''t that tell that three
copies of the same DLL is in my application memory?

Thanks,
Ram

"Lloyd Dupont" <net.galador@ld> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...

if you use standart PInvokde mechanism: DON''T EVER FREE your LIBRARY
amd it can''t be unloaded.
but don''t wory, there is, already, one single instance of the DLL in memory for all thread (of all process!!!)

if you memory goes that up, it''s very likely to be a bug in your application
DLL used with standart mechanism won''t upload and DON''T EVER call a function in a FREED Library.
That will crash your program instantly or, worst, cause weird, mysterious,
alien bugs...
On the other hand, with .NET 2.0 you could transform IntPtr (of a function
pointer) to a delegate.
So you could used LoadLibrary/FreeLibrary & GetProcAddress.
But even then, NEVER call into a FREED library.
"Ram P. Dash" <ra****@hotmail.com> wrote in message
news:oC****************@newsread2.news.pas.earthli nk.net...

Hi:

I''ve a third party DLL (not a .NET class library PE) which I use using
DllImport attribute in my application running in multiple threads invoking different methods of the same DLL.

The memory usage of the application is shooting to 1GB in just 30 minutes. It goes till it''s 1.4GB after which I get OutOfMEmory exceptions. If I
don''t use the DLL, my memory stays put at 100MB.

I tried freeing the DLL using FreeLibrary but after few seconds I am
getting "Object reference not set to an instance of an object" while
accessing the methods of the DLL.

How can I ensure that only one copy of the DLL per thread is in my
application memory and that the DLL is unloaded when it''s no longer used
by any of the threads?

BTW, my application is an ASP.NET web service but this has nothing to do
with the statelessness of a webmethod because I am spawning and running
the threads in the background at application level.

Thanks,
Ram




这篇关于在线程之间共享DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆